1
+ package software .amazon .lambda .powertools .testsuite ;
2
+
3
+
4
+ import com .amazonaws .services .lambda .runtime .Context ;
5
+ import com .amazonaws .services .lambda .runtime .RequestHandler ;
6
+ import com .amazonaws .services .lambda .runtime .events .SQSEvent ;
7
+ import com .amazonaws .services .lambda .runtime .events .models .s3 .S3EventNotification ;
8
+ import com .amazonaws .services .s3 .AmazonS3 ;
9
+ import com .amazonaws .services .s3 .model .S3Object ;
10
+ import com .fasterxml .jackson .core .JsonProcessingException ;
11
+ import com .fasterxml .jackson .databind .ObjectMapper ;
12
+ import org .apache .logging .log4j .Level ;
13
+ import org .apache .logging .log4j .ThreadContext ;
14
+ import org .json .JSONException ;
15
+ import org .junit .jupiter .api .Assertions ;
16
+ import org .junit .jupiter .api .BeforeEach ;
17
+ import org .junit .jupiter .api .Test ;
18
+ import org .mockito .Mock ;
19
+ import software .amazon .lambda .powertools .core .internal .LambdaHandlerProcessor ;
20
+ import software .amazon .lambda .powertools .logging .internal .LambdaLoggingAspect ;
21
+ import software .amazon .lambda .powertools .sqs .internal .SqsLargeMessageAspect ;
22
+ import software .amazon .lambda .powertools .testsuite .handler .LoggingOrderMessageHandler ;
23
+
24
+ import java .io .BufferedReader ;
25
+ import java .io .ByteArrayInputStream ;
26
+ import java .io .IOException ;
27
+ import java .io .InputStreamReader ;
28
+ import java .lang .reflect .InvocationTargetException ;
29
+ import java .lang .reflect .Method ;
30
+ import java .nio .channels .FileChannel ;
31
+ import java .nio .file .Files ;
32
+ import java .nio .file .Paths ;
33
+ import java .nio .file .StandardOpenOption ;
34
+ import java .util .Map ;
35
+
36
+ import static java .util .Collections .emptyMap ;
37
+ import static java .util .Collections .singletonList ;
38
+ import static java .util .stream .Collectors .joining ;
39
+ import static org .apache .commons .lang3 .reflect .FieldUtils .writeStaticField ;
40
+ import static org .assertj .core .api .Assertions .fail ;
41
+ import static org .mockito .Mockito .when ;
42
+ import static org .mockito .MockitoAnnotations .openMocks ;
43
+ import static org .skyscreamer .jsonassert .JSONAssert .assertEquals ;
44
+
45
+ public class LoggingOrderTest {
46
+
47
+ private static final String BUCKET_NAME = "ms-extended-sqs-client" ;
48
+ private static final String BUCKET_KEY = "c71eb2ae-37e0-4265-8909-32f4153faddf" ;
49
+
50
+ @ Mock
51
+ private Context context ;
52
+
53
+ @ Mock
54
+ private AmazonS3 amazonS3 ;
55
+
56
+ @ BeforeEach
57
+ void setUp () throws IllegalAccessException , IOException , NoSuchMethodException , InvocationTargetException {
58
+ openMocks (this );
59
+ writeStaticField (SqsLargeMessageAspect .class , "amazonS3" , amazonS3 , true );
60
+ ThreadContext .clearAll ();
61
+ writeStaticField (LambdaHandlerProcessor .class , "IS_COLD_START" , null , true );
62
+ setupContext ();
63
+ //Make sure file is cleaned up before running full stack logging regression
64
+ FileChannel .open (Paths .get ("target/logfile.json" ), StandardOpenOption .WRITE ).truncate (0 ).close ();
65
+ resetLogLevel (Level .INFO );
66
+ }
67
+
68
+ /**
69
+ * The SQSEvent payload will be altered by the @SqsLargeMessage annoation. Logging of the event should happen
70
+ * after the event has been altered
71
+ *
72
+ * @throws IOException
73
+ * @throws JSONException
74
+ */
75
+ @ Test
76
+ public void testThatLoggingAnnotationActsLast () throws IOException , JSONException {
77
+ S3Object s3Response = new S3Object ();
78
+ s3Response .setObjectContent (new ByteArrayInputStream ("A big message" .getBytes ()));
79
+
80
+ when (amazonS3 .getObject (BUCKET_NAME , BUCKET_KEY )).thenReturn (s3Response );
81
+ SQSEvent sqsEvent = messageWithBody ("[\" software.amazon.payloadoffloading.PayloadS3Pointer\" ,{\" s3BucketName\" :\" " + BUCKET_NAME + "\" ,\" s3Key\" :\" " + BUCKET_KEY + "\" }]" );
82
+
83
+ LoggingOrderMessageHandler requestHandler = new LoggingOrderMessageHandler ();
84
+ requestHandler .handleRequest (sqsEvent , context );
85
+
86
+ // Map<String, Object> log = parseToMap(Files.lines(Paths.get("target/logfile.json")).collect(joining()));
87
+ String loglines = Files .lines (Paths .get ("target/logfile.json" )).collect (joining ());
88
+
89
+ Assertions .assertEquals ("A big message" , loglines );
90
+ // String event = (String) log.get("message");
91
+
92
+ // String expectEvent = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream("/s3EventNotification.json")))
93
+ // .lines().collect(joining("\n"));
94
+
95
+ // assertEquals(expectEvent, event, false);
96
+ }
97
+
98
+ private void setupContext () {
99
+ when (context .getFunctionName ()).thenReturn ("testFunction" );
100
+ when (context .getInvokedFunctionArn ()).thenReturn ("testArn" );
101
+ when (context .getFunctionVersion ()).thenReturn ("1" );
102
+ when (context .getMemoryLimitInMB ()).thenReturn (10 );
103
+ when (context .getAwsRequestId ()).thenReturn ("RequestId" );
104
+ }
105
+
106
+ private void resetLogLevel (Level level ) throws NoSuchMethodException , IllegalAccessException , InvocationTargetException {
107
+ Method resetLogLevels = LambdaLoggingAspect .class .getDeclaredMethod ("resetLogLevels" , Level .class );
108
+ resetLogLevels .setAccessible (true );
109
+ resetLogLevels .invoke (null , level );
110
+ writeStaticField (LambdaLoggingAspect .class , "LEVEL_AT_INITIALISATION" , level , true );
111
+ }
112
+
113
+ // private Map<String, Object> parseToMap(String stringAsJson) {
114
+ // try {
115
+ // return new ObjectMapper().readValue(stringAsJson, Map.class);
116
+ // } catch (JsonProcessingException e) {
117
+ // fail("Failed parsing logger line " + stringAsJson);
118
+ // return emptyMap();
119
+ // }
120
+ // }
121
+
122
+ private SQSEvent messageWithBody (String messageBody ) {
123
+ SQSEvent .SQSMessage sqsMessage = new SQSEvent .SQSMessage ();
124
+ sqsMessage .setBody (messageBody );
125
+ SQSEvent sqsEvent = new SQSEvent ();
126
+ sqsEvent .setRecords (singletonList (sqsMessage ));
127
+ return sqsEvent ;
128
+ }
129
+ }
0 commit comments