1616
1717import static org .assertj .core .api .Assertions .assertThat ;
1818import static org .junit .jupiter .api .Assertions .assertNotNull ;
19- import static org .mockito .Mockito .mock ;
20- import static org .mockito .Mockito .when ;
2119
22- import java .io .InputStream ;
23- import java .io .OutputStream ;
2420import java .util .Optional ;
2521
2622import org .aspectj .lang .ProceedingJoinPoint ;
27- import org .aspectj .lang .Signature ;
2823import org .junit .jupiter .api .Test ;
2924import org .junitpioneer .jupiter .ClearEnvironmentVariable ;
3025import org .junitpioneer .jupiter .SetEnvironmentVariable ;
3328import com .amazonaws .services .lambda .runtime .RequestHandler ;
3429import com .amazonaws .services .lambda .runtime .RequestStreamHandler ;
3530
36- class LambdaHandlerProcessorTest {
31+ import software .amazon .lambda .powertools .common .stubs .TestLambdaContext ;
32+ import software .amazon .lambda .powertools .common .stubs .TestInputStream ;
33+ import software .amazon .lambda .powertools .common .stubs .TestOutputStream ;
34+ import software .amazon .lambda .powertools .common .stubs .TestProceedingJoinPoint ;
35+ import software .amazon .lambda .powertools .common .stubs .TestSignature ;
3736
38- private Signature signature = mock (Signature .class );
39- private ProceedingJoinPoint pjpMock = mock (ProceedingJoinPoint .class );
37+ class LambdaHandlerProcessorTest {
4038
4139 @ Test
4240 void isHandlerMethod_shouldRecognizeRequestHandler () {
43- Object [] args = {new Object (), mock (Context .class )};
41+ Context context = new TestLambdaContext ();
42+ Object [] args = { new Object (), context };
4443 ProceedingJoinPoint pjpMock = mockRequestHandlerPjp (RequestHandler .class , args );
4544
4645 assertThat (LambdaHandlerProcessor .isHandlerMethod (pjpMock )).isTrue ();
4746 }
4847
4948 @ Test
5049 void isHandlerMethod_shouldRecognizeRequestStreamHandler () {
51- Object [] args = {mock ( InputStream . class ), mock ( OutputStream . class ), mock ( Context . class ) };
50+ Object [] args = { new TestInputStream ( ), new TestOutputStream ( ), new TestLambdaContext () };
5251 ProceedingJoinPoint pjpMock = mockRequestHandlerPjp (RequestStreamHandler .class , args );
5352
5453 assertThat (LambdaHandlerProcessor .isHandlerMethod (pjpMock )).isTrue ();
@@ -65,23 +64,23 @@ void isHandlerMethod_shouldReturnFalse() {
6564
6665 @ Test
6766 void placedOnRequestHandler_shouldRecognizeRequestHandler () {
68- Object [] args = {new Object (), mock ( Context . class ) };
67+ Object [] args = { new Object (), new TestLambdaContext () };
6968 ProceedingJoinPoint pjpMock = mockRequestHandlerPjp (RequestHandler .class , args );
7069
7170 assertThat (LambdaHandlerProcessor .placedOnRequestHandler (pjpMock )).isTrue ();
7271 }
7372
7473 @ Test
7574 void placedOnStreamHandler_shouldRecognizeRequestStreamHandler () {
76- Object [] args = {mock ( InputStream . class ), mock ( OutputStream . class ), mock ( Context . class ) };
75+ Object [] args = { new TestInputStream ( ), new TestOutputStream ( ), new TestLambdaContext () };
7776 ProceedingJoinPoint pjpMock = mockRequestHandlerPjp (RequestStreamHandler .class , args );
7877
7978 assertThat (LambdaHandlerProcessor .placedOnStreamHandler (pjpMock )).isTrue ();
8079 }
8180
8281 @ Test
8382 void placedOnRequestHandler_shouldInvalidateOnWrongNoOfArgs () {
84- Object [] args = {new Object ()};
83+ Object [] args = { new Object () };
8584 ProceedingJoinPoint pjpMock = mockRequestHandlerPjp (RequestHandler .class , args );
8685
8786 boolean isPlacedOnRequestHandler = LambdaHandlerProcessor .placedOnRequestHandler (pjpMock );
@@ -91,7 +90,7 @@ void placedOnRequestHandler_shouldInvalidateOnWrongNoOfArgs() {
9190
9291 @ Test
9392 void placedOnRequestHandler_shouldInvalidateOnWrongTypeOfArgs () {
94- Object [] args = {new Object (), new Object ()};
93+ Object [] args = { new Object (), new Object () };
9594 ProceedingJoinPoint pjpMock = mockRequestHandlerPjp (RequestHandler .class , args );
9695
9796 boolean isPlacedOnRequestHandler = LambdaHandlerProcessor .placedOnRequestHandler (pjpMock );
@@ -101,7 +100,7 @@ void placedOnRequestHandler_shouldInvalidateOnWrongTypeOfArgs() {
101100
102101 @ Test
103102 void placedOnStreamHandler_shouldInvalidateOnWrongNoOfArgs () {
104- Object [] args = {new Object ()};
103+ Object [] args = { new Object () };
105104 ProceedingJoinPoint pjpMock = mockRequestHandlerPjp (RequestStreamHandler .class , args );
106105
107106 boolean isPlacedOnStreamHandler = LambdaHandlerProcessor .placedOnStreamHandler (pjpMock );
@@ -111,7 +110,7 @@ void placedOnStreamHandler_shouldInvalidateOnWrongNoOfArgs() {
111110
112111 @ Test
113112 void placedOnStreamHandler_shouldInvalidateOnWrongTypeOfArgs () {
114- Object [] args = {new Object (), new Object (), new Object ()};
113+ Object [] args = { new Object (), new Object (), new Object () };
115114 ProceedingJoinPoint pjpMock = mockRequestHandlerPjp (RequestStreamHandler .class , args );
116115
117116 boolean isPlacedOnStreamHandler = LambdaHandlerProcessor .placedOnStreamHandler (pjpMock );
@@ -121,7 +120,7 @@ void placedOnStreamHandler_shouldInvalidateOnWrongTypeOfArgs() {
121120
122121 @ Test
123122 void placedOnStreamHandler_shouldInvalidateOnTypeOfArgs_invalidOutputStreamArg () {
124- Object [] args = {mock ( InputStream . class ), new Object (), mock ( Context . class ) };
123+ Object [] args = { new TestInputStream ( ), new Object (), new TestLambdaContext () };
125124 ProceedingJoinPoint pjpMock = mockRequestHandlerPjp (RequestStreamHandler .class , args );
126125
127126 boolean isPlacedOnStreamHandler = LambdaHandlerProcessor .placedOnStreamHandler (pjpMock );
@@ -131,7 +130,7 @@ void placedOnStreamHandler_shouldInvalidateOnTypeOfArgs_invalidOutputStreamArg()
131130
132131 @ Test
133132 void placedOnStreamHandler_shouldInvalidateOnTypeOfArgs_invalidContextArg () {
134- Object [] args = {mock ( InputStream . class ), mock ( OutputStream . class ), new Object ()};
133+ Object [] args = { new TestInputStream ( ), new TestOutputStream ( ), new Object () };
135134 ProceedingJoinPoint pjpMock = mockRequestHandlerPjp (RequestStreamHandler .class , args );
136135
137136 boolean isPlacedOnStreamHandler = LambdaHandlerProcessor .placedOnStreamHandler (pjpMock );
@@ -144,9 +143,9 @@ void placedOnStreamHandler_shouldInvalidateOnTypeOfArgs_invalidContextArg() {
144143 void getXrayTraceId_present () {
145144 String traceID = "Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1\" " ;
146145
147- Optional xRayTraceId = LambdaHandlerProcessor .getXrayTraceId ();
146+ Optional < String > xRayTraceId = LambdaHandlerProcessor .getXrayTraceId ();
148147
149- assertThat (xRayTraceId . isPresent ()). isTrue ();
148+ assertThat (xRayTraceId ). isPresent ();
150149 assertThat (traceID .split (";" )[0 ].replace (LambdaConstants .ROOT_EQUALS , "" )).isEqualTo (xRayTraceId .get ());
151150 }
152151
@@ -161,7 +160,7 @@ void getXrayTraceId_notPresent() {
161160
162161 @ Test
163162 void extractContext_fromRequestHandler () {
164- Object [] args = {new Object (), mock ( Context . class ) };
163+ Object [] args = { new Object (), new TestLambdaContext () };
165164 ProceedingJoinPoint pjpMock = mockRequestHandlerPjp (RequestHandler .class , args );
166165
167166 Context context = LambdaHandlerProcessor .extractContext (pjpMock );
@@ -171,7 +170,7 @@ void extractContext_fromRequestHandler() {
171170
172171 @ Test
173172 void extractContext_fromStreamRequestHandler () {
174- Object [] args = {mock ( InputStream . class ), mock ( OutputStream . class ), mock ( Context . class ) };
173+ Object [] args = { new TestInputStream ( ), new TestOutputStream ( ), new TestLambdaContext () };
175174 ProceedingJoinPoint pjpMock = mockRequestHandlerPjp (RequestStreamHandler .class , args );
176175
177176 Context context = LambdaHandlerProcessor .extractContext (pjpMock );
@@ -181,7 +180,7 @@ void extractContext_fromStreamRequestHandler() {
181180
182181 @ Test
183182 void extractContext_notKnownHandler () {
184- Object [] args = {new Object ()};
183+ Object [] args = { new Object () };
185184 ProceedingJoinPoint pjpMock = mockRequestHandlerPjp (Object .class , args );
186185
187186 Context context = LambdaHandlerProcessor .extractContext (pjpMock );
@@ -230,10 +229,8 @@ void serviceName_Undefined() {
230229 assertThat (LambdaHandlerProcessor .serviceName ()).isEqualTo (LambdaConstants .SERVICE_UNDEFINED );
231230 }
232231
233- private ProceedingJoinPoint mockRequestHandlerPjp (Class handlerClass , Object [] handlerArgs ) {
234- when (signature .getDeclaringType ()).thenReturn (handlerClass );
235- when (pjpMock .getArgs ()).thenReturn (handlerArgs );
236- when (pjpMock .getSignature ()).thenReturn (signature );
237- return pjpMock ;
232+ private ProceedingJoinPoint mockRequestHandlerPjp (Class <?> handlerClass , Object [] handlerArgs ) {
233+ TestSignature signature = new TestSignature (handlerClass );
234+ return new TestProceedingJoinPoint (signature , handlerArgs );
238235 }
239236}
0 commit comments