Skip to content
Snippets Groups Projects
Commit 94833e4a authored by Martin Stähr's avatar Martin Stähr
Browse files

feat: adding removeUserFromChat-Endpoint

test: adding unit tests for endpoints
parent 20e8dcff
No related branches found
No related tags found
No related merge requests found
...@@ -22,7 +22,7 @@ public interface ICommunication { ...@@ -22,7 +22,7 @@ public interface ICommunication {
List<ChatMessage> getChatHistory(String roomId); List<ChatMessage> getChatHistory(String roomId);
boolean sendMessageToChat(Message message); boolean sendMessageToChat(Message message, String roomId);
/** /**
* endpoint: https://rocket.chat/docs/developer-guides/rest-api/groups/create/ * endpoint: https://rocket.chat/docs/developer-guides/rest-api/groups/create/
...@@ -44,6 +44,8 @@ public interface ICommunication { ...@@ -44,6 +44,8 @@ public interface ICommunication {
*/ */
boolean addUserToChatRoom(String roomId, User user); boolean addUserToChatRoom(String roomId, User user);
boolean removeUserFromChatRoom(User user, String roomId);
/** /**
* endpoint: https://rocket.chat/docs/developer-guides/rest-api/groups/settopic/ * endpoint: https://rocket.chat/docs/developer-guides/rest-api/groups/settopic/
* *
...@@ -79,6 +81,6 @@ public interface ICommunication { ...@@ -79,6 +81,6 @@ public interface ICommunication {
*/ */
boolean registerUser(User user); boolean registerUser(User user);
String getChatRoomLink(String userToken,String projectToken, String groupToken); String getChatRoomLink(String userToken, String projectToken, String groupToken);
} }
...@@ -37,7 +37,7 @@ public class CommunicationDummyService implements ICommunication { ...@@ -37,7 +37,7 @@ public class CommunicationDummyService implements ICommunication {
} }
@Override @Override
public boolean sendMessageToChat(Message message) { public boolean sendMessageToChat(Message message, String roomId) {
return true; return true;
} }
...@@ -55,6 +55,11 @@ public class CommunicationDummyService implements ICommunication { ...@@ -55,6 +55,11 @@ public class CommunicationDummyService implements ICommunication {
return true; return true;
} }
@Override
public boolean removeUserFromChatRoom(User user, String roomId) {
return true;
}
@Override @Override
public boolean setChatRoomTopic(String roomId, String topic) { public boolean setChatRoomTopic(String roomId, String topic) {
return true; return true;
......
...@@ -62,19 +62,19 @@ public class CommunicationView { ...@@ -62,19 +62,19 @@ public class CommunicationView {
@POST @POST
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Path("/send") @Path("/send/{roomId}")
public Response sendMessage(Message message) { public Response sendMessage(Message message, @PathParam("roomId") String roomId) {
if (isNull(message)) { if (isNull(message)) {
log.trace("sendMessage message object was null"); log.trace("sendMessage message object was null");
return Response.status(Response.Status.BAD_REQUEST).entity("must provide message").build(); return Response.status(Response.Status.BAD_REQUEST).entity("must provide message").build();
} }
boolean wasSend = communicationService.sendMessageToChat(message); boolean wasSend = communicationService.sendMessageToChat(message, roomId);
Response response; Response response;
if (wasSend) { if (wasSend) {
log.error("error while sending message for message: {}", message); log.trace("response for sendMessage: {}", wasSend);
response = Response.ok(wasSend).build(); response = Response.ok(wasSend).build();
} else { } else {
log.trace("response for sendMessage: {}", wasSend); log.error("error while sending message for message: {}", message);
response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("error while sending message").build(); response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("error while sending message").build();
} }
return response; return response;
...@@ -105,7 +105,31 @@ public class CommunicationView { ...@@ -105,7 +105,31 @@ public class CommunicationView {
return response; return response;
} }
// TODO: remove user from chatroom
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/removeUser/{roomId}")
public Response removeUserFromChatRoom(User user, @PathParam("roomId") String roomId) {
if (isNull(user)) {
log.trace("removeUser user object was null");
return Response.status(Response.Status.BAD_REQUEST).entity("must provide user").build();
}
boolean wasRemoved = communicationService.removeUserFromChatRoom(user, roomId);
if (isNull(wasRemoved)) {
log.error("removeUserToChatRoom: chatRoom not found for roomId: {}, user: {}", roomId, user);
return Response.status(Response.Status.NOT_FOUND).build();
}
Response response;
if (wasRemoved) {
log.trace("response for removeUser: {}", wasRemoved);
response = Response.ok(wasRemoved).build();
} else {
log.error("error while adding user to chat room");
response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("error while adding user to chatRoom").build();
}
return response;
}
@POST @POST
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
......
...@@ -6,6 +6,7 @@ import org.junit.Test; ...@@ -6,6 +6,7 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import unipotsdam.gf.config.GFResourceConfig; import unipotsdam.gf.config.GFResourceConfig;
import unipotsdam.gf.core.management.user.User; import unipotsdam.gf.core.management.user.User;
import unipotsdam.gf.modules.communication.model.Message;
import unipotsdam.gf.modules.communication.model.chat.ChatRoom; import unipotsdam.gf.modules.communication.model.chat.ChatRoom;
import javax.ws.rs.client.Entity; import javax.ws.rs.client.Entity;
...@@ -25,6 +26,7 @@ import static org.junit.Assert.assertThat; ...@@ -25,6 +26,7 @@ import static org.junit.Assert.assertThat;
@RunWith(ConcurrentRunner.class) @RunWith(ConcurrentRunner.class)
public class CommunicationViewTest extends JerseyTest { public class CommunicationViewTest extends JerseyTest {
private final static String ENDPOINT = "chat/";
@Override @Override
protected Application configure() { protected Application configure() {
...@@ -36,7 +38,7 @@ public class CommunicationViewTest extends JerseyTest { ...@@ -36,7 +38,7 @@ public class CommunicationViewTest extends JerseyTest {
@Test @Test
public void getChatRoomInformation() { public void getChatRoomInformation() {
String path = "chat/info/"; String path = ENDPOINT + "info/";
Response responseOk = target().path(path + "1").request().get(); Response responseOk = target().path(path + "1").request().get();
assertThat(responseOk.getStatus(), is(OK.getStatusCode())); assertThat(responseOk.getStatus(), is(OK.getStatusCode()));
assertNotNull(responseOk.readEntity(ChatRoom.class)); assertNotNull(responseOk.readEntity(ChatRoom.class));
...@@ -47,7 +49,7 @@ public class CommunicationViewTest extends JerseyTest { ...@@ -47,7 +49,7 @@ public class CommunicationViewTest extends JerseyTest {
@Test @Test
public void getChatHistory() { public void getChatHistory() {
String path = "chat/history/"; String path = ENDPOINT + "history/";
Response responseOk = target().path(path + "1").request().get(); Response responseOk = target().path(path + "1").request().get();
assertThat(responseOk.getStatus(), is(OK.getStatusCode())); assertThat(responseOk.getStatus(), is(OK.getStatusCode()));
assertNotNull(responseOk.readEntity(List.class)); assertNotNull(responseOk.readEntity(List.class));
...@@ -58,19 +60,69 @@ public class CommunicationViewTest extends JerseyTest { ...@@ -58,19 +60,69 @@ public class CommunicationViewTest extends JerseyTest {
@Test @Test
public void createChatRoom() { public void createChatRoom() {
String path = "chat/create"; String path = ENDPOINT + "create";
Response responseOk = target().path(path).queryParam("name", "test").request().post(null); Response responseOk = target().path(path).queryParam("name", "test").request().post(null);
assertThat(responseOk.getStatus(), is(CREATED.getStatusCode())); assertThat(responseOk.getStatus(), is(CREATED.getStatusCode()));
assertNotNull(responseOk.readEntity(String.class)); assertNotNull(responseOk.readEntity(String.class));
ArrayList<User> users = new ArrayList<>(); ArrayList<User> users = new ArrayList<>();
users.add(new User("test", "test", "test", true)); users.add(new User("test", "test", "test", true));
responseOk = target().path(path).queryParam("name", "test").request().post(Entity.json(users)); Response responseOk2 = target().path(path).queryParam("name", "test").request().post(Entity.json(users));
assertThat(responseOk.getStatus(), is(CREATED.getStatusCode())); assertThat(responseOk2.getStatus(), is(CREATED.getStatusCode()));
assertNotNull(responseOk.readEntity(String.class)); assertNotNull(responseOk2.readEntity(String.class));
Response responseBadRequest = target().path(path).request().post(Entity.json(users)); Response responseBadRequest = target().path(path).request().post(Entity.json(users));
assertThat(responseBadRequest.getStatus(), is(BAD_REQUEST.getStatusCode())); assertThat(responseBadRequest.getStatus(), is(BAD_REQUEST.getStatusCode()));
} }
@Test
public void sendMessage() {
String path = ENDPOINT + "send";
Message message = new Message();
message.setMessage("test");
message.setRoomIdOrChannel("1");
Response responseOk = target().path(path + "/1").request().post(Entity.json(message));
assertThat(responseOk.getStatus(), is(OK.getStatusCode()));
assertNotNull(responseOk.readEntity(String.class));
Response responseBadRequest = target().path(path + "/1").request().post(Entity.json(null));
assertThat(responseBadRequest.getStatus(), is(BAD_REQUEST.getStatusCode()));
}
@Test
public void addUserToChatRoom() {
String fullPath = ENDPOINT + "addUser" + "/1";
User user = new User("test", "test", "test", true);
Response responseOk = target().path(fullPath).request().post(Entity.json(user));
assertThat(responseOk.getStatus(), is(OK.getStatusCode()));
assertNotNull(responseOk.readEntity(String.class));
Response responseBadRequest = target().path(fullPath).request().post(Entity.json(null));
assertThat(responseBadRequest.getStatus(), is(BAD_REQUEST.getStatusCode()));
}
@Test
public void removeUserFromChatRoom() {
String fullPath = ENDPOINT + "removeUser" + "/1";
User user = new User("test2", "test2", "test", true);
Response responseOk = target().path(fullPath).request().post(Entity.json(user));
assertThat(responseOk.getStatus(), is(OK.getStatusCode()));
assertNotNull(responseOk.readEntity(String.class));
Response responseBadRequest = target().path(fullPath).request().post(Entity.json(null));
assertThat(responseBadRequest.getStatus(), is(BAD_REQUEST.getStatusCode()));
}
@Test
public void setChatRoomTopic() {
String fullPath = ENDPOINT + "setTopic" + "/1";
Response responseOk = target().path(fullPath).queryParam("topic", "test").request().post(null);
assertThat(responseOk.getStatus(), is(OK.getStatusCode()));
assertNotNull(responseOk.readEntity(String.class));
Response responseBadRequest = target().path(fullPath).request().post(Entity.json(null));
assertThat(responseBadRequest.getStatus(), is(BAD_REQUEST.getStatusCode()));
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment