From ec4a04cb1349fb0a5b3351425ad423d0b8f0637a Mon Sep 17 00:00:00 2001 From: Christian Balster Date: Sun, 4 Jun 2023 22:52:50 +0200 Subject: [PATCH] Fix broken tests --- .../criticalmaps/model/ChatModel.java | 9 +-- .../handler/ServerResponseProcessorTest.java | 20 ++--- .../criticalmaps/model/ChatModelTest.java | 76 ++++++------------- .../server_response_chatmessages.json | 14 ++++ .../resources/server_response_locations.json | 32 ++++++++ .../resources/simple_server_response.json | 1 - 6 files changed, 78 insertions(+), 74 deletions(-) create mode 100644 app/src/test/resources/server_response_chatmessages.json create mode 100644 app/src/test/resources/server_response_locations.json delete mode 100644 app/src/test/resources/simple_server_response.json diff --git a/app/src/main/java/de/stephanlindauer/criticalmaps/model/ChatModel.java b/app/src/main/java/de/stephanlindauer/criticalmaps/model/ChatModel.java index 783e9b61..252954aa 100644 --- a/app/src/main/java/de/stephanlindauer/criticalmaps/model/ChatModel.java +++ b/app/src/main/java/de/stephanlindauer/criticalmaps/model/ChatModel.java @@ -18,7 +18,6 @@ import javax.inject.Inject; import javax.inject.Singleton; -import de.stephanlindauer.criticalmaps.handler.PostChatmessagesHandler; import de.stephanlindauer.criticalmaps.model.chat.ReceivedChatMessage; import de.stephanlindauer.criticalmaps.utils.AeSimpleSHA1; import okhttp3.internal.Util; @@ -58,13 +57,7 @@ public void setFromJson(JSONArray jsonArray) throws JSONException, receivedChatMessages.add(new ReceivedChatMessage(message, timestamp)); } - Collections.sort(receivedChatMessages, new Comparator() { - @Override - public int compare(ReceivedChatMessage oneChatMessages, - ReceivedChatMessage otherChatMessage) { - return oneChatMessages.getTimestamp().compareTo(otherChatMessage.getTimestamp()); - } - }); + Collections.sort(receivedChatMessages, Comparator.comparing(ReceivedChatMessage::getTimestamp)); } public JSONObject createNewOutgoingMessage(String message) { diff --git a/app/src/test/java/de/stephanlindauer/criticalmaps/handler/ServerResponseProcessorTest.java b/app/src/test/java/de/stephanlindauer/criticalmaps/handler/ServerResponseProcessorTest.java index 244de88f..faf10cb4 100644 --- a/app/src/test/java/de/stephanlindauer/criticalmaps/handler/ServerResponseProcessorTest.java +++ b/app/src/test/java/de/stephanlindauer/criticalmaps/handler/ServerResponseProcessorTest.java @@ -1,7 +1,7 @@ package de.stephanlindauer.criticalmaps.handler; +import org.json.JSONArray; import org.json.JSONException; -import org.json.JSONObject; import org.junit.Test; import java.io.File; @@ -28,20 +28,20 @@ public class ServerResponseProcessorTest { public void process_chatmessagesAreSetOnModel() throws IOException, URISyntaxException, JSONException { final String json = readToString(new File(getClass().getClassLoader() - .getResource("simple_server_response.json").toURI())); + .getResource("server_response_locations.json").toURI())); final ChatModel chatModel = mock(ChatModel.class); final ServerResponseProcessor tested = new ServerResponseProcessor( mock(OtherUsersLocationModel.class), mock(EventBus.class), chatModel); - tested.processLocations(json); + tested.processChatmessages(json); - verify(chatModel).setFromJson(any(JSONObject.class)); + verify(chatModel).setFromJson(any(JSONArray.class)); } @Test public void process_eventIsFiredForValidJSON() throws IOException, URISyntaxException { final String json = readToString(new File(getClass().getClassLoader() - .getResource("simple_server_response.json").toURI())); + .getResource("server_response_locations.json").toURI())); final EventBus eventMock = mock(EventBus.class); final ServerResponseProcessor tested = new ServerResponseProcessor( mock(OtherUsersLocationModel.class), eventMock, mock(ChatModel.class)); @@ -67,21 +67,15 @@ public String readToString(File file) throws IOException { } public String readToString(Charset charset, File file) throws IOException { - final FileInputStream stream = new FileInputStream(file); - try { + try (FileInputStream stream = new FileInputStream(file)) { return readToStringFromFileInputStream(charset, stream); - } finally { - stream.close(); } } private String readToStringFromFileInputStream(Charset charset, FileInputStream stream) throws IOException { - final FileChannel fc = stream.getChannel(); - try { + try (FileChannel fc = stream.getChannel()) { final MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); return charset.decode(bb).toString(); - } finally { - fc.close(); } } } diff --git a/app/src/test/java/de/stephanlindauer/criticalmaps/model/ChatModelTest.java b/app/src/test/java/de/stephanlindauer/criticalmaps/model/ChatModelTest.java index 0dbf4b38..07d63343 100644 --- a/app/src/test/java/de/stephanlindauer/criticalmaps/model/ChatModelTest.java +++ b/app/src/test/java/de/stephanlindauer/criticalmaps/model/ChatModelTest.java @@ -1,13 +1,12 @@ package de.stephanlindauer.criticalmaps.model; +import org.json.JSONArray; import org.json.JSONException; -import org.json.JSONObject; import org.junit.Test; import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.net.URISyntaxException; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; @@ -16,69 +15,48 @@ import de.stephanlindauer.criticalmaps.model.chat.ReceivedChatMessage; import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class ChatModelTest { - @Test - public void hasOutgoingChatmessages_isTrueIfOutgoingMessagesExist() { - final ChatModel tested = new ChatModel(); - tested.setNewOutgoingMessage(new OutgoingChatMessage("test")); - - assertThat(tested.hasOutgoingMessages()).isTrue(); - } - - @Test - public void hasOutgoingChatmessages_isFalseIfNoOutgoingMessagesExist() { - final ChatModel tested = new ChatModel(); - - assertThat(tested.hasOutgoingMessages()).isFalse(); - } @Test public void setFromJson_testThatChatmessagesAreSorted() throws IOException, URISyntaxException, JSONException { final String json = readToString(new File(getClass().getClassLoader() - .getResource("simple_server_response.json").toURI())); - final JSONObject response = new JSONObject(json); - final ChatModel tested = new ChatModel(); + .getResource("server_response_chatmessages.json").toURI())); + final JSONArray response = new JSONArray(json); - tested.setFromJson(response.getJSONObject("chatMessages")); - final ReceivedChatMessage message0 = - (ReceivedChatMessage) tested.getSavedAndOutgoingMessages().get(0); - final ReceivedChatMessage message1 = - (ReceivedChatMessage) tested.getSavedAndOutgoingMessages().get(1); + UserModel userModel = mock(UserModel.class); + when(userModel.getChangingDeviceToken()).thenReturn("t0k3n"); - assertThat(message0.getTimestamp()).isLessThan(message1.getTimestamp()); - } + final ChatModel tested = new ChatModel(userModel); - @Test - public void setFromJson_outgoingMessagesAreRemovedAfterSend() throws JSONException, - UnsupportedEncodingException { - final OutgoingChatMessage outgoingChatMessage = new OutgoingChatMessage("test"); - final JSONObject testResponse = new JSONObject("{\"" + outgoingChatMessage.getIdentifier() - + "\":{\"message\":\"" + outgoingChatMessage.getMessage() - + "\",\"timestamp\":1446113099}}"); - final ChatModel tested = new ChatModel(); - - tested.setFromJson(testResponse); - tested.setNewOutgoingMessage(outgoingChatMessage); - tested.setFromJson(testResponse); + tested.setFromJson(response); + final ReceivedChatMessage message0 = tested.getReceivedChatMessages().get(0); + final ReceivedChatMessage message1 = tested.getReceivedChatMessages().get(1); - assertThat(tested.hasOutgoingMessages()).isFalse(); + assertThat(message0.getTimestamp()).isLessThan(message1.getTimestamp()); } + @Test public void setFromJson_existingMessagesAreReplaced() throws URISyntaxException, IOException, JSONException { final String json = readToString(new File(getClass().getClassLoader() - .getResource("simple_server_response.json").toURI())); - final JSONObject testResponse = new JSONObject(json).getJSONObject("chatMessages"); - final ChatModel tested = new ChatModel(); + .getResource("server_response_chatmessages.json").toURI())); + final JSONArray testResponse = new JSONArray(json); + + UserModel userModel = mock(UserModel.class); + when(userModel.getChangingDeviceToken()).thenReturn("t0k3n"); + + final ChatModel tested = new ChatModel(userModel); tested.setFromJson(testResponse); - final int sizeBefore = tested.getSavedAndOutgoingMessages().size(); + final int sizeBefore = tested.getReceivedChatMessages().size(); tested.setFromJson(testResponse); - assertThat(tested.getSavedAndOutgoingMessages()).hasSize(sizeBefore); + assertThat(tested.getReceivedChatMessages()).hasSize(sizeBefore); } public String readToString(File file) throws IOException { @@ -86,21 +64,15 @@ public String readToString(File file) throws IOException { } public String readToString(Charset charset, File file) throws IOException { - final FileInputStream stream = new FileInputStream(file); - try { + try (FileInputStream stream = new FileInputStream(file)) { return readToStringFromFileInputStream(charset, stream); - } finally { - stream.close(); } } private String readToStringFromFileInputStream(Charset charset, FileInputStream stream) throws IOException { - final FileChannel fc = stream.getChannel(); - try { + try (FileChannel fc = stream.getChannel()) { final MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); return charset.decode(bb).toString(); - } finally { - fc.close(); } } } diff --git a/app/src/test/resources/server_response_chatmessages.json b/app/src/test/resources/server_response_chatmessages.json new file mode 100644 index 00000000..75221af8 --- /dev/null +++ b/app/src/test/resources/server_response_chatmessages.json @@ -0,0 +1,14 @@ +[ + { + "identifier": "738e6f90c27936f64bd9bd9ea98ad9d3707ffb8f", + "device": "b181ebe22dbfa7ec07b793c97b4cd4e7e0558f9f", + "message": "foo", + "timestamp": 1446113099 + }, + { + "identifier": "51288884ed6f33745089d7385c0d9d2b89d71309", + "device": "22dc97b4cd4bf81ebe793e7e055a7ec0f9f7b1b8", + "message": "bar", + "timestamp": 1446113267 + } +] \ No newline at end of file diff --git a/app/src/test/resources/server_response_locations.json b/app/src/test/resources/server_response_locations.json new file mode 100644 index 00000000..3ec15279 --- /dev/null +++ b/app/src/test/resources/server_response_locations.json @@ -0,0 +1,32 @@ +[ + { + "device": "ec0381f4842815a213771ba5155f09b1d6fb97b5", + "latitude": 52322786, + "longitude": 13061227, + "timestamp": 1685910782 + }, + { + "device": "5551614c74e377b9745145aabdbec9aa0f3b2bc1", + "latitude": 52509919, + "longitude": 13258792, + "timestamp": 1685910783 + }, + { + "device": "d5bcc1556ff3a60e436e5b503c7efbe4e249e36d", + "latitude": 52550509, + "longitude": 13406121, + "timestamp": 1685910795 + }, + { + "device": "7a55cfa0f84154f4c669e2e63c9e9e438ae8180a", + "latitude": 52510930, + "longitude": 13439071, + "timestamp": 1685910784 + }, + { + "device": "eaedd319cfe37edab0916bf3acb2bc445c430603", + "latitude": 52521592, + "longitude": 13356797, + "timestamp": 1685910735 + } +] \ No newline at end of file diff --git a/app/src/test/resources/simple_server_response.json b/app/src/test/resources/simple_server_response.json deleted file mode 100644 index 93979263..00000000 --- a/app/src/test/resources/simple_server_response.json +++ /dev/null @@ -1 +0,0 @@ -{"locations" : {"13918e88a36cb8cf0325820dbf47e0403f8a9640":{"longitude":"-56253312","latitude":"-34111877","timestamp":1446113116}}, "chatMessages" : {"738e6f90c27936f64bd9bd9ea98ad9d3707ffb8f":{"message":"foo","timestamp":1446113099},"51288884ed6f33745089d7385c0d9d2b89d71309":{"message":"bar","timestamp":1446113267}}} \ No newline at end of file