21
21
import java .io .InputStream ;
22
22
import java .lang .reflect .Field ;
23
23
import java .nio .charset .StandardCharsets ;
24
+ import java .util .Collections ;
24
25
import java .util .Map ;
25
26
import java .util .function .Consumer ;
26
27
import java .util .function .Function ;
27
28
import java .util .function .Supplier ;
28
29
29
30
import com .amazonaws .services .lambda .runtime .events .APIGatewayProxyRequestEvent ;
31
+ import com .amazonaws .services .lambda .runtime .events .APIGatewayProxyResponseEvent ;
30
32
import com .amazonaws .services .lambda .runtime .events .APIGatewayV2HTTPEvent ;
33
+ import com .amazonaws .services .lambda .runtime .events .APIGatewayV2HTTPResponse ;
31
34
import com .amazonaws .services .lambda .runtime .events .KinesisEvent ;
32
35
import com .amazonaws .services .lambda .runtime .events .S3Event ;
33
36
import com .amazonaws .services .lambda .runtime .events .SNSEvent ;
46
49
import org .springframework .util .MimeType ;
47
50
48
51
import static org .assertj .core .api .Assertions .assertThat ;
49
- import static org .junit .jupiter .api .Assertions .fail ;
50
52
51
53
/**
52
54
*
@@ -698,7 +700,7 @@ public void testApiGatewayStringEventBody() throws Exception {
698
700
invoker .handleRequest (targetStream , output , null );
699
701
ObjectMapper mapper = new ObjectMapper ();
700
702
Map result = mapper .readValue (output .toByteArray (), Map .class );
701
- assertThat (result .get ("body" )).isEqualTo ("\" HELLO\" " );
703
+ assertThat (result .get ("body" )).isEqualTo ("HELLO" );
702
704
}
703
705
704
706
@ SuppressWarnings ("rawtypes" )
@@ -713,7 +715,7 @@ public void testApiGatewayMapEventBody() throws Exception {
713
715
invoker .handleRequest (targetStream , output , null );
714
716
715
717
Map result = mapper .readValue (output .toByteArray (), Map .class );
716
- assertThat (result .get ("body" )).isEqualTo ("\" JIM LAHEY\" " );
718
+ assertThat (result .get ("body" )).isEqualTo ("JIM LAHEY" );
717
719
}
718
720
719
721
@ SuppressWarnings ("rawtypes" )
@@ -729,7 +731,7 @@ public void testApiGatewayEvent() throws Exception {
729
731
730
732
Map result = mapper .readValue (output .toByteArray (), Map .class );
731
733
System .out .println (result );
732
- assertThat (result .get ("body" )).isEqualTo ("\" hello\" " );
734
+ assertThat (result .get ("body" )).isEqualTo ("hello" );
733
735
}
734
736
735
737
@ SuppressWarnings ("rawtypes" )
@@ -745,7 +747,7 @@ public void testApiGatewayV2Event() throws Exception {
745
747
746
748
Map result = mapper .readValue (output .toByteArray (), Map .class );
747
749
System .out .println (result );
748
- assertThat (result .get ("body" )).isEqualTo ("\" Hello from Lambda\" " );
750
+ assertThat (result .get ("body" )).isEqualTo ("Hello from Lambda" );
749
751
}
750
752
751
753
@ SuppressWarnings ("rawtypes" )
@@ -761,9 +763,63 @@ public void testApiGatewayAsSupplier() throws Exception {
761
763
762
764
Map result = mapper .readValue (output .toByteArray (), Map .class );
763
765
System .out .println (result );
764
- assertThat (result .get ("body" )).isEqualTo ("\" boom\" " );
766
+ assertThat (result .get ("body" )).isEqualTo ("boom" );
765
767
}
766
768
769
+ @ SuppressWarnings ("rawtypes" )
770
+ @ Test
771
+ public void testApiGatewayInAndOut () throws Exception {
772
+ System .setProperty ("MAIN_CLASS" , ApiGatewayConfiguration .class .getName ());
773
+ System .setProperty ("spring.cloud.function.definition" , "inputOutputApiEvent" );
774
+ FunctionInvoker invoker = new FunctionInvoker ();
775
+
776
+ InputStream targetStream = new ByteArrayInputStream (this .apiGatewayEvent .getBytes ());
777
+ ByteArrayOutputStream output = new ByteArrayOutputStream ();
778
+ invoker .handleRequest (targetStream , output , null );
779
+
780
+ Map result = mapper .readValue (output .toByteArray (), Map .class );
781
+ assertThat (result .get ("body" )).isEqualTo ("hello" );
782
+ Map headers = (Map ) result .get ("headers" );
783
+ assertThat (headers .get ("foo" )).isEqualTo ("bar" );
784
+ }
785
+
786
+ @ SuppressWarnings ("rawtypes" )
787
+ @ Test
788
+ public void testApiGatewayInAndOutV2 () throws Exception {
789
+ System .setProperty ("MAIN_CLASS" , ApiGatewayConfiguration .class .getName ());
790
+ System .setProperty ("spring.cloud.function.definition" , "inputOutputApiEventV2" );
791
+ FunctionInvoker invoker = new FunctionInvoker ();
792
+
793
+ InputStream targetStream = new ByteArrayInputStream (this .apiGatewayEvent .getBytes ());
794
+ ByteArrayOutputStream output = new ByteArrayOutputStream ();
795
+ invoker .handleRequest (targetStream , output , null );
796
+
797
+ Map result = mapper .readValue (output .toByteArray (), Map .class );
798
+ assertThat (result .get ("body" )).isEqualTo ("hello" );
799
+ Map headers = (Map ) result .get ("headers" );
800
+ assertThat (headers .get ("foo" )).isEqualTo ("bar" );
801
+ }
802
+
803
+ // @SuppressWarnings("rawtypes")
804
+ // @Test
805
+ // public void testApiGatewayInAndOutWithException() throws Exception {
806
+ // System.setProperty("MAIN_CLASS", ApiGatewayConfiguration.class.getName());
807
+ // System.setProperty("spring.cloud.function.definition", "inputOutputApiEventException");
808
+ // FunctionInvoker invoker = new FunctionInvoker();
809
+ //
810
+ // InputStream targetStream = new ByteArrayInputStream(this.apiGatewayEvent.getBytes());
811
+ // ByteArrayOutputStream output = new ByteArrayOutputStream();
812
+ // invoker.handleRequest(targetStream, output, null);
813
+ //
814
+ // Map result = mapper.readValue(output.toByteArray(), Map.class);
815
+ // assertThat(result.get("body")).isEqualTo("Intentional");
816
+ //
817
+ // Map headers = (Map) result.get("headers");
818
+ // assertThat(headers.get("foo")).isEqualTo("bar");
819
+ // }
820
+
821
+
822
+
767
823
@ SuppressWarnings ("rawtypes" )
768
824
@ Test
769
825
public void testApiGatewayEventAsMessage () throws Exception {
@@ -777,7 +833,7 @@ public void testApiGatewayEventAsMessage() throws Exception {
777
833
778
834
Map result = mapper .readValue (output .toByteArray (), Map .class );
779
835
System .out .println (result );
780
- assertThat (result .get ("body" )).isEqualTo ("\" hello\" " );
836
+ assertThat (result .get ("body" )).isEqualTo ("hello" );
781
837
}
782
838
783
839
@ SuppressWarnings ("rawtypes" )
@@ -793,7 +849,7 @@ public void testApiGatewayEventAsMap() throws Exception {
793
849
794
850
Map result = mapper .readValue (output .toByteArray (), Map .class );
795
851
System .out .println (result );
796
- assertThat (result .get ("body" )).isEqualTo ("\" hello\" " );
852
+ assertThat (result .get ("body" )).isEqualTo ("hello" );
797
853
}
798
854
799
855
@ SuppressWarnings ("rawtypes" )
@@ -818,13 +874,9 @@ public void testWithDefaultRoutingFailure() throws Exception {
818
874
819
875
InputStream targetStream = new ByteArrayInputStream (this .apiGatewayEvent .getBytes ());
820
876
ByteArrayOutputStream output = new ByteArrayOutputStream ();
821
- try {
822
- invoker .handleRequest (targetStream , output , null );
823
- fail ();
824
- }
825
- catch (Exception e ) {
826
- // success, since no definition nor routing instructions are provided
827
- }
877
+ invoker .handleRequest (targetStream , output , null );
878
+ Map result = mapper .readValue (output .toByteArray (), Map .class );
879
+ assertThat (((String ) result .get ("body" ))).startsWith ("Failed to establish route, since neither were provided:" );
828
880
}
829
881
830
882
@ SuppressWarnings ("rawtypes" )
@@ -839,7 +891,7 @@ public void testWithDefaultRouting() throws Exception {
839
891
invoker .handleRequest (targetStream , output , null );
840
892
841
893
Map result = mapper .readValue (output .toByteArray (), Map .class );
842
- assertThat (result .get ("body" )).isEqualTo ("\" olleh\" " );
894
+ assertThat (result .get ("body" )).isEqualTo ("olleh" );
843
895
}
844
896
845
897
@ SuppressWarnings ("rawtypes" )
@@ -855,7 +907,7 @@ public void testWithDefinitionEnvVariable() throws Exception {
855
907
invoker .handleRequest (targetStream , output , null );
856
908
857
909
Map result = mapper .readValue (output .toByteArray (), Map .class );
858
- assertThat (result .get ("body" )).isEqualTo ("\" OLLEH\" " );
910
+ assertThat (result .get ("body" )).isEqualTo ("OLLEH" );
859
911
}
860
912
861
913
@ SuppressWarnings ("unchecked" )
@@ -1086,6 +1138,35 @@ public Function<APIGatewayProxyRequestEvent, String> inputApiEvent() {
1086
1138
};
1087
1139
}
1088
1140
1141
+ @ Bean
1142
+ public Function <APIGatewayProxyRequestEvent , APIGatewayProxyResponseEvent > inputOutputApiEvent () {
1143
+ return v -> {
1144
+ APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent ();
1145
+ response .setBody (v .getBody ());
1146
+ response .setStatusCode (200 );
1147
+ response .setHeaders (Collections .singletonMap ("foo" , "bar" ));
1148
+ return response ;
1149
+ };
1150
+ }
1151
+
1152
+ @ Bean
1153
+ public Function <APIGatewayV2HTTPEvent , APIGatewayV2HTTPResponse > inputOutputApiEventV2 () {
1154
+ return v -> {
1155
+ APIGatewayV2HTTPResponse response = new APIGatewayV2HTTPResponse ();
1156
+ response .setBody (v .getBody ());
1157
+ response .setStatusCode (200 );
1158
+ response .setHeaders (Collections .singletonMap ("foo" , "bar" ));
1159
+ return response ;
1160
+ };
1161
+ }
1162
+
1163
+ @ Bean
1164
+ public Function <APIGatewayV2HTTPEvent , APIGatewayV2HTTPResponse > inputOutputApiEventException () {
1165
+ return v -> {
1166
+ throw new IllegalStateException ("Intentional" );
1167
+ };
1168
+ }
1169
+
1089
1170
@ Bean
1090
1171
public Function <APIGatewayV2HTTPEvent , String > inputApiV2Event () {
1091
1172
return v -> {
0 commit comments