@@ -115,6 +115,18 @@ private static CloudEventImpl<Map<String, Object>> sampleCloudEvent(File snoopFi
115115 .build ();
116116 }
117117
118+ private static JsonObject expectedCloudEventAttributes () {
119+ JsonObject attributes = new JsonObject ();
120+ attributes .addProperty ("datacontenttype" , "application/json" );
121+ attributes .addProperty ("specversion" , "1.0" );
122+ attributes .addProperty ("id" , "B234-1234-1234" );
123+ attributes .addProperty ("source" , "/source" );
124+ attributes .addProperty ("time" , "2018-04-05T17:31:00Z" );
125+ attributes .addProperty ("type" , "com.example.someevent.new" );
126+ attributes .addProperty ("dataschema" , "/schema" );
127+ return attributes ;
128+ }
129+
118130 private static int serverPort ;
119131
120132 /**
@@ -144,7 +156,7 @@ abstract static class TestCase {
144156
145157 abstract Optional <String > expectedResponseText ();
146158
147- abstract Optional <String > expectedJsonString ();
159+ abstract Optional <JsonObject > expectedJson ();
148160
149161 abstract Optional <String > expectedContentType ();
150162
@@ -183,7 +195,7 @@ abstract static class Builder {
183195
184196 abstract Builder setExpectedOutput (String x );
185197
186- abstract Builder setExpectedJsonString ( String x );
198+ abstract Builder setExpectedJson ( JsonObject x );
187199
188200 abstract Builder setHttpContentType (String x );
189201
@@ -319,7 +331,7 @@ public void background() throws Exception {
319331 TestCase testCase = TestCase .builder ()
320332 .setRequestText (requestText )
321333 .setSnoopFile (snoopFile )
322- .setExpectedJsonString ( requestText )
334+ .setExpectedJson ( new Gson (). fromJson ( requestText , JsonObject . class ) )
323335 .build ();
324336 backgroundTest (fullTarget ("BackgroundSnoop.snoop" ), ImmutableList .of (testCase ));
325337 }
@@ -337,24 +349,28 @@ public void newTypedBackground() throws Exception {
337349 private void newBackgroundTest (String target ) throws Exception {
338350 File snoopFile = snoopFile ();
339351 String gcfRequestText = sampleLegacyEvent (snoopFile );
352+ JsonObject expectedJson = new Gson ().fromJson (gcfRequestText , JsonObject .class );
353+ // We don't currently put anything in the attributes() map for legacy events.
354+ expectedJson .add ("attributes" , new JsonObject ());
340355 TestCase gcfTestCase = TestCase .builder ()
341356 .setRequestText (gcfRequestText )
342357 .setSnoopFile (snoopFile )
343- .setExpectedJsonString ( gcfRequestText )
358+ .setExpectedJson ( expectedJson )
344359 .build ();
345360
346361 // A CloudEvent using the "structured content mode", where both the metadata and the payload
347362 // are in the body of the HTTP request.
348363 String cloudEventRequestText = Json .encode (sampleCloudEvent (snoopFile ));
349364 // For CloudEvents, we don't currently populate Context#getResource with anything interesting,
350365 // so we excise that from the expected text we would have with legacy events.
351- String cloudEventExpectedJsonString =
352- gcfRequestText .replaceAll ("\" resource\" :\\ s*\\ {[^}]*\\ }" , "\" resource\" :{}" );
366+ JsonObject cloudEventExpectedJson = new Gson ().fromJson (gcfRequestText , JsonObject .class );
367+ cloudEventExpectedJson .getAsJsonObject ("context" ).add ("resource" , new JsonObject ());
368+ cloudEventExpectedJson .add ("attributes" , expectedCloudEventAttributes ());
353369 TestCase cloudEventsStructuredTestCase = TestCase .builder ()
354370 .setSnoopFile (snoopFile )
355371 .setRequestText (cloudEventRequestText )
356372 .setHttpContentType ("application/cloudevents+json; charset=utf-8" )
357- .setExpectedJsonString ( cloudEventExpectedJsonString )
373+ .setExpectedJson ( cloudEventExpectedJson )
358374 .build ();
359375
360376 // A CloudEvent using the "binary content mode", where the metadata is in HTTP headers and the
@@ -367,7 +383,7 @@ private void newBackgroundTest(String target) throws Exception {
367383 .setRequestText (wire .getPayload ().get ())
368384 .setHttpContentType ("application/json" )
369385 .setHttpHeaders (ImmutableMap .copyOf (wire .getHeaders ()))
370- .setExpectedJsonString ( cloudEventExpectedJsonString )
386+ .setExpectedJson ( cloudEventExpectedJson )
371387 .build ();
372388 // TODO(emcmanus): Update the Content-Type to "application/json; charset=utf-8" when
373389 // https://github.com/cloudevents/sdk-java/issues/89 has been fixed.
@@ -458,19 +474,19 @@ private void backgroundTest(String functionTarget, List<TestCase> testCases) thr
458474 String snooped = new String (Files .readAllBytes (snoopFile .toPath ()), StandardCharsets .UTF_8 );
459475 Gson gson = new Gson ();
460476 JsonObject snoopedJson = gson .fromJson (snooped , JsonObject .class );
461- String expectedJsonString = testCase .expectedJsonString ().get ();
462- JsonObject expectedJson = gson .fromJson (expectedJsonString , JsonObject .class );
477+ JsonObject expectedJson = testCase .expectedJson ().get ();
463478 expect .withMessage ("Testing %s with %s" , functionTarget , testCase )
464479 .that (snoopedJson ).isEqualTo (expectedJson );
465480 }
466481 }
467482
468- private void checkSnoopFile (File snoopFile , String expectedJsonString ) throws IOException {
483+ private void checkSnoopFile (TestCase testCase ) throws IOException {
484+ File snoopFile = testCase .snoopFile ().get ();
485+ JsonObject expectedJson = testCase .expectedJson ().get ();
469486 String snooped = new String (Files .readAllBytes (snoopFile .toPath ()), StandardCharsets .UTF_8 );
470487 Gson gson = new Gson ();
471488 JsonObject snoopedJson = gson .fromJson (snooped , JsonObject .class );
472- JsonObject expectedJson = gson .fromJson (expectedJsonString , JsonObject .class );
473- expect .that (snoopedJson ).isEqualTo (expectedJson );
489+ expect .withMessage ("Testing with %s" , testCase ).that (snoopedJson ).isEqualTo (expectedJson );
474490 }
475491
476492 private void testHttpFunction (String target , List <TestCase > testCases ) throws Exception {
@@ -518,7 +534,7 @@ private void testFunction(
518534 testCase .expectedContentType ()
519535 .ifPresent (type -> expect .that (response .getMediaType ()).isEqualTo (type ));
520536 if (testCase .snoopFile ().isPresent ()) {
521- checkSnoopFile (testCase . snoopFile (). get (), testCase . expectedJsonString (). get () );
537+ checkSnoopFile (testCase );
522538 }
523539 testCase .expectedOutput ()
524540 .ifPresent (output -> expect .that (serverProcess .output ().toString ()).contains (output ));
0 commit comments