|
21 | 21 | import static org.assertj.core.api.Assertions.assertThat; |
22 | 22 | import static org.junit.jupiter.api.Assertions.assertFalse; |
23 | 23 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 24 | +import static org.mockito.Answers.CALLS_REAL_METHODS; |
24 | 25 | import static org.mockito.Mockito.mock; |
| 26 | +import static org.mockito.Mockito.mockStatic; |
25 | 27 | import static org.mockito.Mockito.when; |
| 28 | +import static org.mockito.Mockito.withSettings; |
| 29 | +import static software.amazon.opentelemetry.javaagent.providers.AwsAttributeKeys.AWS_LAMBDA_LOCAL_OPERATION_OVERRIDE; |
26 | 30 | import static software.amazon.opentelemetry.javaagent.providers.AwsAttributeKeys.AWS_LOCAL_OPERATION; |
27 | 31 | import static software.amazon.opentelemetry.javaagent.providers.AwsSpanProcessingUtil.MAX_KEYWORD_LENGTH; |
28 | 32 | import static software.amazon.opentelemetry.javaagent.providers.AwsSpanProcessingUtil.getDialectKeywords; |
|
36 | 40 | import java.util.List; |
37 | 41 | import org.junit.jupiter.api.BeforeEach; |
38 | 42 | import org.junit.jupiter.api.Test; |
| 43 | +import org.mockito.MockedStatic; |
39 | 44 |
|
40 | 45 | public class AwsSpanProcessingUtilTest { |
41 | 46 | private static final String DEFAULT_PATH_VALUE = "/"; |
@@ -123,6 +128,49 @@ public void testGetIngressOperationInvalidNameAndValidTargetAndMethod() { |
123 | 128 | assertThat(actualOperation).isEqualTo(validMethod + " " + validTarget); |
124 | 129 | } |
125 | 130 |
|
| 131 | + @Test |
| 132 | + public void testGetIngressOperationLambdaOverride() { |
| 133 | + try (MockedStatic<AwsApplicationSignalsCustomizerProvider> providerStatic = |
| 134 | + mockStatic( |
| 135 | + AwsApplicationSignalsCustomizerProvider.class, |
| 136 | + withSettings().defaultAnswer(CALLS_REAL_METHODS))) { |
| 137 | + // Force Lambda environment branch |
| 138 | + providerStatic |
| 139 | + .when(AwsApplicationSignalsCustomizerProvider::isLambdaEnvironment) |
| 140 | + .thenReturn(true); |
| 141 | + // Simulate an override attribute on the span |
| 142 | + when(attributesMock.get(AWS_LAMBDA_LOCAL_OPERATION_OVERRIDE)).thenReturn("MyOverrideOp"); |
| 143 | + |
| 144 | + String actualOperation = AwsSpanProcessingUtil.getIngressOperation(spanDataMock); |
| 145 | + assertThat(actualOperation).isEqualTo("MyOverrideOp"); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void testGetIngressOperationLambdaDefault() throws Exception { |
| 151 | + try ( |
| 152 | + // Mock the AWS environment check |
| 153 | + MockedStatic<AwsApplicationSignalsCustomizerProvider> providerStatic = |
| 154 | + mockStatic( |
| 155 | + AwsApplicationSignalsCustomizerProvider.class, |
| 156 | + withSettings().defaultAnswer(CALLS_REAL_METHODS)); |
| 157 | + // Mock only getFunctionNameFromEnv, leave all other util logic untouched |
| 158 | + MockedStatic<AwsSpanProcessingUtil> utilStatic = |
| 159 | + mockStatic( |
| 160 | + AwsSpanProcessingUtil.class, withSettings().defaultAnswer(CALLS_REAL_METHODS))) { |
| 161 | + // force lambda branch and no override attribute |
| 162 | + providerStatic |
| 163 | + .when(AwsApplicationSignalsCustomizerProvider::isLambdaEnvironment) |
| 164 | + .thenReturn(true); |
| 165 | + when(attributesMock.get(AWS_LAMBDA_LOCAL_OPERATION_OVERRIDE)).thenReturn(null); |
| 166 | + // Provide a deterministic function name |
| 167 | + utilStatic.when(AwsSpanProcessingUtil::getFunctionNameFromEnv).thenReturn("MockFunction"); |
| 168 | + |
| 169 | + String actual = AwsSpanProcessingUtil.getIngressOperation(spanDataMock); |
| 170 | + assertThat(actual).isEqualTo("MockFunction/FunctionHandler"); |
| 171 | + } |
| 172 | + } |
| 173 | + |
126 | 174 | @Test |
127 | 175 | public void testGetEgressOperationUseInternalOperation() { |
128 | 176 | String invalidName = null; |
|
0 commit comments