Skip to content

Commit

Permalink
Merge pull request #331 from cbalster/fix_tests
Browse files Browse the repository at this point in the history
Fix broken tests
  • Loading branch information
cbalster committed Jun 4, 2023
2 parents daf4730 + ec4a04c commit 3f65155
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,13 +57,7 @@ public void setFromJson(JSONArray jsonArray) throws JSONException,
receivedChatMessages.add(new ReceivedChatMessage(message, timestamp));
}

Collections.sort(receivedChatMessages, new Comparator<ReceivedChatMessage>() {
@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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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));
Expand All @@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,91 +15,64 @@
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 {
return readToString(Charset.defaultCharset(), file);
}

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();
}
}
}
14 changes: 14 additions & 0 deletions app/src/test/resources/server_response_chatmessages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"identifier": "738e6f90c27936f64bd9bd9ea98ad9d3707ffb8f",
"device": "b181ebe22dbfa7ec07b793c97b4cd4e7e0558f9f",
"message": "foo",
"timestamp": 1446113099
},
{
"identifier": "51288884ed6f33745089d7385c0d9d2b89d71309",
"device": "22dc97b4cd4bf81ebe793e7e055a7ec0f9f7b1b8",
"message": "bar",
"timestamp": 1446113267
}
]
32 changes: 32 additions & 0 deletions app/src/test/resources/server_response_locations.json
Original file line number Diff line number Diff line change
@@ -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
}
]
1 change: 0 additions & 1 deletion app/src/test/resources/simple_server_response.json

This file was deleted.

0 comments on commit 3f65155

Please sign in to comment.