1

Merge pull request #55 from hhu-propra2/serialization-tests

added deserialization tests
This commit is contained in:
Talha Caliskan
2020-03-10 15:21:25 +01:00
committed by GitHub
3 changed files with 65 additions and 33 deletions

View File

@ -11,4 +11,6 @@ public class DeleteUserEvent extends Event {
public DeleteUserEvent(Long event_id, Long group_id, String user_id) { public DeleteUserEvent(Long event_id, Long group_id, String user_id) {
super(event_id, group_id, user_id); super(event_id, group_id, user_id);
} }
public DeleteUserEvent() {}
} }

View File

@ -8,8 +8,8 @@ import lombok.NoArgsConstructor;
@Getter @Getter
@NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor
@JsonTypeInfo( @JsonTypeInfo(
include = JsonTypeInfo.As.PROPERTY, include = JsonTypeInfo.As.PROPERTY,
use = JsonTypeInfo.Id.NAME, use = JsonTypeInfo.Id.NAME,

View File

@ -1,8 +1,8 @@
package mops.gruppen2.service; package mops.gruppen2.service;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import mops.gruppen2.domain.event.AddUserEvent; import mops.gruppen2.domain.Role;
import mops.gruppen2.domain.event.Event; import mops.gruppen2.domain.event.*;
import mops.gruppen2.repository.EventRepository; import mops.gruppen2.repository.EventRepository;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -12,44 +12,74 @@ import static org.mockito.Mockito.mock;
class SerializationServiceTest { class SerializationServiceTest {
SerializationService serializationService;
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
} serializationService = new SerializationService(mock(EventRepository.class));
}
@Test @Test
void serializeEventTest() { void serializeEventTest() throws JsonProcessingException {
Event event = new Event(1L,1L,"1"); Event event = new Event(1L,1L,"1");
SerializationService serializationService = new SerializationService(mock(EventRepository.class)); assertThat(serializationService.serializeEvent(event)).isEqualTo("{\"type\":\"Event\",\"event_id\":1,\"group_id\":1,\"user_id\":\"1\"}");
}
try { @Test
assertThat(serializationService.serializeEvent(event)).isEqualTo("{\"type\":\"Event\",\"event_id\":1,\"group_id\":1,\"user_id\":\"1\"}"); void deserializeAddUserEventToRightClass() throws JsonProcessingException {
} catch (JsonProcessingException e) { String json = "{\"type\":\"AddUserEvent\",\"event_id\":1,\"group_id\":1,\"user_id\":\"1\"}";
e.printStackTrace();
}
}
@Test Event event = serializationService.deserializeEvent(json);
void deserializeAddUserEvent() throws JsonProcessingException {
SerializationService serializationService = new SerializationService(mock(EventRepository.class));
String json = "{\"type\":\"Event\",\"event_id\":1,\"group_id\":1,\"user_id\":\"1\"}"; assertThat(event).isInstanceOf(AddUserEvent.class);
}
Event event = serializationService.deserializeEvent(json); @Test
void deserializeDeleteUserEventToRightClass() throws JsonProcessingException {
String json = "{\"type\":\"DeleteUserEvent\",\"event_id\":1,\"group_id\":1,\"user_id\":\"1\"}";
assertThat(event).isInstanceOf(Event.class); Event event = serializationService.deserializeEvent(json);
}
@Test assertThat(event).isInstanceOf(DeleteUserEvent.class);
void serializeEventTestAddUserEvent(){ }
AddUserEvent event = new AddUserEvent(1L,1L,"user_id","peter","mueller","a@a");
SerializationService serializationService = new SerializationService(mock(EventRepository.class)); @Test
try { void deserializeUpdateGroupDescriptionEventToRightClass() throws JsonProcessingException {
assertThat(serializationService.serializeEvent(event)).isEqualTo("{\"type\":\"AddUserEvent\",\"event_id\":1,\"group_id\":1,\"user_id\":\"user_id\",\"givenname\":\"peter\",\"familyname\":\"mueller\",\"email\":\"a@a\"}"); String json = "{\"type\":\"UpdateGroupDescriptionEvent\",\"event_id\":1,\"group_id\":1,\"user_id\":\"1\",\"newGroupDescription\":\"test\"}";
} catch (JsonProcessingException e) {
e.printStackTrace(); Event event = serializationService.deserializeEvent(json);
}
} assertThat(event).isInstanceOf(UpdateGroupDescriptionEvent.class);
}
@Test
void deserializeUpdateGroupTitleEventToRightClass() throws JsonProcessingException {
String json = "{\"type\":\"UpdateGroupTitleEvent\",\"event_id\":1,\"group_id\":1,\"user_id\":\"1\",\"newGroupTitle\":\"test\"}";
Event event = serializationService.deserializeEvent(json);
assertThat(event).isInstanceOf(UpdateGroupTitleEvent.class);
}
@Test
void deserializeUpdateRoleEventToRightClass() throws JsonProcessingException {
System.out.println(serializationService.serializeEvent(new UpdateRoleEvent(1L, 1L, "1", Role.ADMIN)));
String json = "{\"type\":\"UpdateRoleEvent\",\"event_id\":1,\"group_id\":1,\"user_id\":1,\"newRole\":\"ADMIN\"}";
Event event = serializationService.deserializeEvent(json);
assertThat(event).isInstanceOf(UpdateRoleEvent.class);
}
@Test
void deserializeCreateGroupEventToRightClass() throws JsonProcessingException {
String json = "{\"type\":\"CreateGroupEvent\",\"event_id\":1,\"group_id\":1,\"user_id\":\"1\",\"groupTitle\":\"test\",\"groupDescription\":\"test\"}";
Event event = serializationService.deserializeEvent(json);
assertThat(event).isInstanceOf(CreateGroupEvent.class);
}
} }