From 9100b5bda037fcd1b051b98585744077132320bc Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 22 Aug 2021 00:13:02 +0300 Subject: Type-safe ActivityStreams deserialization --- .../java/com/juick/server/tests/ServerTests.java | 32 +++++++++++----------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'src/test/java') diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index e25f0b0d..c11dfb46 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -40,6 +40,7 @@ import com.juick.www.api.activity.Profile; import com.juick.www.api.activity.model.Context; import com.juick.www.api.activity.model.activities.*; import com.juick.www.api.activity.model.objects.Application; +import com.juick.www.api.activity.model.objects.Image; import com.juick.www.api.activity.model.objects.Note; import com.juick.www.api.activity.model.objects.Person; import com.juick.www.api.webfinger.model.Account; @@ -1778,12 +1779,12 @@ public class ServerTests { Person person = (Person) jsonMapper.readValue(personJsonStr, Context.class); String undoJsonStr = IOUtils.toString(new ClassPathResource("undo.json").getURI(), StandardCharsets.UTF_8); Undo undo = jsonMapper.readValue(undoJsonStr, Undo.class); - String undoFollower = (String) ((Map) undo.getObject()).get("object"); + String undoFollower = undo.getObject().getId(); String createJsonStr = IOUtils.toString(new ClassPathResource("create.json").getURI(), StandardCharsets.UTF_8); Create create = jsonMapper.readValue(createJsonStr, Create.class); - Map note = (Map) create.getObject(); - Map attachmentObj = (Map) ((List) note.get("attachment")).get(0); - String attachment = attachmentObj != null ? (String) attachmentObj.get("url") : StringUtils.EMPTY; + Note note = (Note) create.getObject(); + Context attachmentObj = note.getAttachment().get(0); + String attachment = attachmentObj != null ? (String) attachmentObj.getUrl() : StringUtils.EMPTY; String deleteJsonStr = IOUtils.toString(new ClassPathResource("delete.json").getURI(), StandardCharsets.UTF_8); Delete delete = jsonMapper.readValue(deleteJsonStr, Delete.class); int mid = messagesService.createMessage(ugnich.getUid(), "YO", "", null); @@ -1811,7 +1812,7 @@ public class ServerTests { String undoPleromaStr = IOUtils.toString(new ClassPathResource("undo_pleroma.json").getURI(), StandardCharsets.UTF_8); Undo undoPleroma = jsonMapper.readValue(undoPleromaStr, Undo.class); - String undoPleromaFollower = (String) ((Map) undoPleroma.getObject()).get("object"); + String undoPleromaFollower = undoPleroma.getObject().getId(); String deletev3JsonStr = IOUtils.toString(new ClassPathResource("delete_v3.json").getURI(), StandardCharsets.UTF_8); Delete deleteObject = jsonMapper.readValue(deletev3JsonStr, Delete.class); @@ -1850,7 +1851,7 @@ public class ServerTests { Person to = (Person) signatureManager.getContext(URI.create("http://localhost:8080/u/ugnich")).get(); Follow follow = new Follow(); follow.setActor("http://localhost:8080/u/freefd"); - follow.setObject("http://localhost:8080/u/ugnich"); + follow.setObject(new Context("http://localhost:8080/u/ugnich")); signatureManager.post(from, to, follow); } @@ -2073,9 +2074,9 @@ public class ServerTests { Delete delete = jsonMapper.readValue(deleteJsonStr, Delete.class); ClientHttpRequestFactory originalRequestFactory = apClient.getRequestFactory(); MockRestServiceServer restServiceServer = MockRestServiceServer.createServer(apClient); - restServiceServer.expect(times(2), requestTo((String) delete.getObject())) + restServiceServer.expect(times(2), requestTo(delete.getObject().getId())) .andRespond(withStatus(HttpStatus.GONE)); - restServiceServer.expect(requestTo((String) delete.getObject())).andRespond(response -> { + restServiceServer.expect(requestTo(delete.getObject().getId())).andRespond(response -> { throw new ResourceAccessException("Connection reset"); }); mockMvc.perform(post("/api/inbox").contentType(ACTIVITY_MEDIA_TYPE).content(deleteJsonStr)) @@ -2094,7 +2095,7 @@ public class ServerTests { Delete delete = jsonMapper.readValue(deleteJsonStr, Delete.class); ClientHttpRequestFactory originalRequestFactory = apClient.getRequestFactory(); MockRestServiceServer restServiceServer = MockRestServiceServer.createServer(apClient); - restServiceServer.expect(requestTo((String) delete.getObject())).andRespond(response -> { + restServiceServer.expect(requestTo(delete.getObject().getId())).andRespond(response -> { throw new ResourceAccessException("Connection reset"); }); mockMvc.perform(post("/api/inbox").contentType(ACTIVITY_MEDIA_TYPE).content(deleteJsonStr).header("Signature", @@ -2117,13 +2118,12 @@ public class ServerTests { Announce announce = jsonMapper.readValue(activity, Announce.class); String noteString = IOUtils.toString(noteWithAttachment.getInputStream(), StandardCharsets.UTF_8); Create create = jsonMapper.readValue(noteString, Create.class); - Map note = (Map) create.getObject(); - String markdown = remarkConverter.convertFragment((String) note.get("content")); - String commandBody = note.get("attachment") == null ? markdown - : ((List) note.get("attachment")).stream().map(attachmentObj -> { - Map attachment = (Map) attachmentObj; - String attachmentUrl = attachment.get("url"); - String attachmentName = attachment.get("name"); + Note note = (Note) create.getObject(); + String markdown = remarkConverter.convertFragment((String) note.getContent()); + String commandBody = note.getContent() == null ? markdown + : note.getAttachment().stream().map(attachment -> { + String attachmentUrl = attachment.getUrl(); + String attachmentName = attachment.getName(); return PlainTextFormatter.markdownUrl(attachmentUrl, attachmentName); }).reduce(markdown, (current, next) -> String.format("%s\n%s", current, next)); } -- cgit v1.2.3