|
| 1 | +package com.microsoft.azure.functions.worker.broker; |
| 2 | + |
| 3 | +import com.microsoft.azure.functions.rpc.messages.RpcException; |
| 4 | +import com.microsoft.azure.functions.spi.inject.FunctionInstanceInjector; |
| 5 | +import com.microsoft.azure.functions.worker.WorkerLogManager; |
| 6 | +import com.microsoft.azure.functions.worker.binding.BindingDataStore; |
| 7 | +import com.microsoft.azure.functions.worker.binding.ExecutionContextDataSource; |
| 8 | +import com.microsoft.azure.functions.worker.binding.ExecutionRetryContext; |
| 9 | +import com.microsoft.azure.functions.worker.binding.ExecutionTraceContext; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 13 | +import org.mockito.Mock; |
| 14 | +import org.mockito.MockedStatic; |
| 15 | +import org.mockito.Mockito; |
| 16 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 17 | + |
| 18 | +import java.lang.reflect.Method; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.logging.Logger; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.*; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | + |
| 26 | + |
| 27 | +@ExtendWith(MockitoExtension.class) |
| 28 | +public class ParameterResolverTest { |
| 29 | + |
| 30 | + private ExecutionContextDataSource executionContextDataSource; |
| 31 | + @Mock |
| 32 | + private MethodBindInfo methodBindInfo; |
| 33 | + |
| 34 | + @BeforeEach |
| 35 | + public void setup() { |
| 36 | + String invocationId = "testInvocationId"; |
| 37 | + ExecutionTraceContext traceContext = new ExecutionTraceContext("traceParent", "traceState", new HashMap<>()); |
| 38 | + ExecutionRetryContext retryContext = new ExecutionRetryContext(1, 2, RpcException.newBuilder().build()); |
| 39 | + String functionName = "ParameterResolverTest"; |
| 40 | + BindingDataStore dataStore = new BindingDataStore(); |
| 41 | + dataStore.setBindingDefinitions(new HashMap<>()); |
| 42 | + try (MockedStatic<WorkerLogManager> workerLogManagerMockedStatic = Mockito.mockStatic(WorkerLogManager.class)) { |
| 43 | + workerLogManagerMockedStatic.when(() -> WorkerLogManager.getInvocationLogger(invocationId)) |
| 44 | + .thenReturn(Logger.getAnonymousLogger()); |
| 45 | + executionContextDataSource = new ExecutionContextDataSource(invocationId, |
| 46 | + traceContext, retryContext, functionName, dataStore, methodBindInfo, |
| 47 | + this.getClass(), new ArrayList<>(), new FunctionInstanceInjector() { |
| 48 | + @Override |
| 49 | + public <T> T getInstance(Class<T> functionClass) throws Exception { |
| 50 | + return null; |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testResolveArgumentsHasImplicitOutputTrue() throws Exception { |
| 59 | + Method testMethod = this.getClass().getDeclaredMethod("testMethod"); |
| 60 | + when(methodBindInfo.hasImplicitOutput()).thenReturn(true); |
| 61 | + when(methodBindInfo.getMethod()).thenReturn(testMethod); |
| 62 | + when(methodBindInfo.getParams()).thenReturn(new ArrayList<>()); |
| 63 | + ParameterResolver.resolveArguments(executionContextDataSource); |
| 64 | + assertTrue(executionContextDataSource.getDataStore().getDataTargetTypedValue(BindingDataStore.RETURN_NAME).isPresent()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testResolveArgumentsHasImplicitOutputFalse() throws Exception { |
| 69 | + Method testMethod = this.getClass().getDeclaredMethod("testMethod"); |
| 70 | + when(methodBindInfo.hasImplicitOutput()).thenReturn(false); |
| 71 | + when(methodBindInfo.getMethod()).thenReturn(testMethod); |
| 72 | + when(methodBindInfo.getParams()).thenReturn(new ArrayList<>()); |
| 73 | + ParameterResolver.resolveArguments(executionContextDataSource); |
| 74 | + assertFalse(executionContextDataSource.getDataStore().getDataTargetTypedValue(BindingDataStore.RETURN_NAME).isPresent()); |
| 75 | + } |
| 76 | + |
| 77 | + public void testMethod() {} |
| 78 | +} |
0 commit comments