Commit 086bc362 authored by Siiri Tann's avatar Siiri Tann
Browse files

Merge branch 'TEIS-2109' into 'develop'

TEIS-2109: added mockmvc to OfficeApiGatewayTestBase

See merge request teis/office-api-gateway!170
parents 2f61e7c6 71afebda
package ee.sm.ti.teis.officegateway.docgen.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotBlank;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class GenerateDocumentDto {
@NotBlank
private String templateId;
private String fileName;
private String outputFormat;
@NotBlank
private String jsonData;
private String objectType;
}
......@@ -2,13 +2,9 @@ package ee.sm.ti.teis.officegateway.docgen.request;
import ee.sm.ti.teis.AbstractDTO;
import ee.sm.ti.teis.ErrorDTO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import ee.sm.ti.teis.officegateway.docgen.dto.GenerateDocumentDto;
import static ee.sm.ti.teis.officegateway.docgen.request.GenerateDocumentRequest.Parameters;
public class GenerateDocumentRequest extends AbstractDTO<Parameters, ErrorDTO> {
public class GenerateDocumentRequest extends AbstractDTO<GenerateDocumentDto, ErrorDTO> {
public static final String ROUTING_KEY = "api.GenerateDocumentRequest";
......@@ -17,15 +13,4 @@ public class GenerateDocumentRequest extends AbstractDTO<Parameters, ErrorDTO> {
return ROUTING_KEY;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Parameters {
private String templateId;
private String fileName;
private String jsonData;
private String outputFormat;
private String objectType;
}
}
......@@ -2,15 +2,18 @@ package ee.sm.ti.teis.officegateway;
import ee.sm.ti.teis.OfficeApiGateway;
import ee.sm.ti.teis.gatewaycommon.config.JacksonIntegTestConfig;
import ee.sm.ti.teis.gatewaycommon.rabbit.RequestProcessingService;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.util.LinkedMultiValueMap;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Testcontainers;
......@@ -22,7 +25,13 @@ import org.testcontainers.junit.jupiter.Testcontainers;
@ContextConfiguration(initializers = {OfficeApiGatewayTestBase.Initializer.class}, classes = JacksonIntegTestConfig.class)
public abstract class OfficeApiGatewayTestBase {
protected LinkedMultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
private static final GenericContainer<?> rabbitMQContainer;
public static final String MOCK_ROLE = "OFFICIAL_USER";
@MockBean
public RequestProcessingService requestProcessingService;
// to speed up tests, lets use static container, it is used in all test classes that are executed inside one Spring Context
static {
......
package ee.sm.ti.teis.officegateway.classifiers;
import ee.sm.ti.teis.gatewaycommon.error.NoResponseFromRabbitException;
import com.fasterxml.jackson.core.type.TypeReference;
import ee.sm.ti.teis.gatewaycommon.MockMvcUtil;
import ee.sm.ti.teis.officegateway.OfficeApiGatewayTestBase;
import ee.sm.ti.teis.officegateway.classifiers.dto.ClassifierItemLinkOfficeDto;
import ee.sm.ti.teis.officegateway.classifiers.dto.PatchClassifierDefinitionDto;
import ee.sm.ti.teis.officegateway.classifiers.dto.PatchClassifierItemDto;
import ee.sm.ti.teis.officegateway.classifiers.dto.ShiftClassifierDirectionDto;
import ee.sm.ti.teis.servicerequest.RequestMetaDTO;
import ee.sm.ti.teis.officegateway.classifiers.dto.*;
import ee.sm.ti.teis.officegateway.classifiers.request.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.test.context.support.WithMockUser;
import static java.util.Collections.emptyList;
import java.util.List;
import static ee.sm.ti.teis.officegateway.OfficeApiGatewayTestBase.MOCK_ROLE;
import static java.util.UUID.randomUUID;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@WithMockUser(roles = MOCK_ROLE)
public class ClassifiersControllerTest extends OfficeApiGatewayTestBase {
private static final RequestMetaDTO REQUEST_META_DTO = RequestMetaDTO.builder().requestId(randomUUID().toString()).build();
@Autowired
ClassifiersController controller;
MockMvcUtil mockMvcUtil;
@Test
void getClassifierDefinitions_test() {
assertThrows(NoResponseFromRabbitException.class, () ->
controller.getClassifierDefinitions(REQUEST_META_DTO));
void getClassifierDefinitions_success() {
when(requestProcessingService.sendAndReceive(any(ClassifierDefinitionsOfficeRequest.class)))
.thenReturn(List.of(new ClassifierDefinitionDto()));
mockMvcUtil.testGet("/classifier-definitions", null, new TypeReference<List<ClassifierDefinitionDto>>() {
});
verify(requestProcessingService).sendAndReceive(any(ClassifierDefinitionsOfficeRequest.class));
}
@Test
void getClassifierDefinition_test() {
assertThrows(NoResponseFromRabbitException.class, () ->
controller.getClassifierDefinition(null, REQUEST_META_DTO));
void getClassifierDefinition_success() {
when(requestProcessingService.sendAndReceive(any(ClassifierDefinitionOfficeRequest.class)))
.thenReturn(new ClassifierDefinitionDto());
mockMvcUtil.testGet("/classifier-definitions/" + randomUUID(), null, new TypeReference<ClassifierDefinitionDto>() {
});
verify(requestProcessingService).sendAndReceive(any(ClassifierDefinitionOfficeRequest.class));
}
@Test
void createClassifierItem_test() {
assertThrows(NoResponseFromRabbitException.class, () ->
controller.createClassifierItem(null, REQUEST_META_DTO));
void patchClassifierDefinition_success() {
when(requestProcessingService.sendAndReceive(any(PatchClassifierDefinitionOfficeRequest.class)))
.thenReturn(new ClassifierDefinitionDto());
mockMvcUtil.testPatch("/classifier-definitions/" + randomUUID() + "/default-value", new PatchClassifierDefinitionDto(),
new TypeReference<ClassifierDefinitionDto>() {
});
verify(requestProcessingService).sendAndReceive(any(PatchClassifierDefinitionOfficeRequest.class));
}
@Test
void patchClassifierItem_test() {
assertThrows(NoResponseFromRabbitException.class, () ->
controller.patchClassifierItem("id", PatchClassifierItemDto.builder().build(), REQUEST_META_DTO));
void createClassifierItem_success() {
when(requestProcessingService.sendAndReceive(any(CreateClassifierItemRequest.class)))
.thenReturn(new ClassifierItemOfficeDto());
mockMvcUtil.testPost("/classifier-items", new ClassifierItemOfficeDto(), new TypeReference<ClassifierItemOfficeDto>() {
});
verify(requestProcessingService).sendAndReceive(any(CreateClassifierItemRequest.class));
}
@Test
void deleteClassifierItem_test() {
assertThrows(NoResponseFromRabbitException.class, () ->
controller.deleteClassifierItem("id", REQUEST_META_DTO));
void patchClassifierItem_success() {
when(requestProcessingService.sendAndReceive(any(PatchClassifierItemRequest.class)))
.thenReturn(new ClassifierItemOfficeDto());
mockMvcUtil.testPatch("/classifier-items/" + randomUUID() + "/name", new PatchClassifierItemDto(), new TypeReference<ClassifierItemOfficeDto>() {
});
verify(requestProcessingService).sendAndReceive(any(PatchClassifierItemRequest.class));
}
@Test
void deleteClassifierLink_test() {
assertThrows(NoResponseFromRabbitException.class, () ->
controller.deleteClassifierLink(randomUUID(), REQUEST_META_DTO));
void deleteClassifierItem_success() {
mockMvcUtil.testDelete("/classifier-items/" + randomUUID());
verify(requestProcessingService).sendAndReceive(any(DeleteClassifierItemRequest.class));
}
@Test
void postClassifierAttributes_test() {
assertThrows(NoResponseFromRabbitException.class, () ->
controller.postClassifierAttributes(emptyList(), REQUEST_META_DTO));
void deleteClassifierLink_success() {
mockMvcUtil.testDelete("/classifier-items/item-link/" + randomUUID());
verify(requestProcessingService).sendAndReceive(any(DeleteClassifierItemLinkRequest.class));
}
@Test
void shiftClassifier_test() {
assertThrows(NoResponseFromRabbitException.class, () ->
controller.shiftClassifier(randomUUID().toString(), new ShiftClassifierDirectionDto(), REQUEST_META_DTO));
void postClassifierAttributes_success() {
when(requestProcessingService.sendAndReceive(any(CreateClassifierAttributesRequest.class)))
.thenReturn(List.of(new ClassifierAttributeValueDto()));
mockMvcUtil.testPost("/classifier-items/attributes", List.of(new ClassifierAttributeValueDto()), new TypeReference<List<ClassifierAttributeValueDto>>() {
});
verify(requestProcessingService).sendAndReceive(any(CreateClassifierAttributesRequest.class));
}
@Test
void patchClassifierDefinition_test() {
assertThrows(NoResponseFromRabbitException.class, () ->
controller.patchClassifierDefinition(randomUUID().toString(), new PatchClassifierDefinitionDto(), REQUEST_META_DTO));
void postClassifierLink_success() {
when(requestProcessingService.sendAndReceive(any(CreateClassifierItemLinkRequest.class)))
.thenReturn(new ClassifierItemLinkOfficeDto());
mockMvcUtil.testPost("/classifier-items/item-link/", new ClassifierItemLinkOfficeDto(), new TypeReference<ClassifierItemLinkOfficeDto>() {
});
verify(requestProcessingService).sendAndReceive(any(CreateClassifierItemLinkRequest.class));
}
@Test
void postClassifierLink_test() {
assertThrows(NoResponseFromRabbitException.class, () ->
controller.postClassifierLink(new ClassifierItemLinkOfficeDto(), REQUEST_META_DTO));
void shiftClassifier_success() {
when(requestProcessingService.sendAndReceive(any(ShiftClassifierItemRequest.class)))
.thenReturn(List.of(new ClassifierItemOfficeDto()));
mockMvcUtil.testPatch("/classifier-items/test/seq-no", new ShiftClassifierDirectionDto(), new TypeReference<List<ClassifierItemOfficeDto>>() {
});
verify(requestProcessingService).sendAndReceive(any(ShiftClassifierItemRequest.class));
}
}
package ee.sm.ti.teis.officegateway.docgen;
import ee.sm.ti.teis.gatewaycommon.error.NoResponseFromRabbitException;
import ee.sm.ti.teis.officegateway.OfficeApiGatewayTestBase;
import ee.sm.ti.teis.servicerequest.RequestMetaDTO;
import ee.sm.ti.teis.officegateway.docgen.request.UploadDocTemplateRequest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static ee.sm.ti.teis.officegateway.OfficeApiGatewayTestBase.MOCK_ROLE;
import static java.util.UUID.randomUUID;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WithMockUser(roles = MOCK_ROLE)
class DocGenControllerTest extends OfficeApiGatewayTestBase {
@Autowired
DocGenController controller;
MockMvc mockMvc;
@Test
void uploadTemplate_badRequest() {
assertThrows(NoResponseFromRabbitException.class, () ->
controller.uploadTemplate(new MockMultipartFile("test", "test-file".getBytes()), "", new RequestMetaDTO()));
void uploadTemplate_success() throws Exception {
mockMvc.perform(multipart("/templates/upload")
.file(new MockMultipartFile("templateFile", "blah".getBytes()))
.param("templateId", randomUUID().toString()))
.andExpect(status().isOk());
verify(requestProcessingService).sendAndReceive(any(UploadDocTemplateRequest.class));
}
}
package ee.sm.ti.teis.officegateway.docgen;
import com.fasterxml.jackson.core.type.TypeReference;
import ee.sm.ti.teis.commongateway.file.FileMetadataDto;
import ee.sm.ti.teis.gatewaycommon.MockMvcUtil;
import ee.sm.ti.teis.gatewaycommon.error.NoResponseFromRabbitException;
import ee.sm.ti.teis.officegateway.OfficeApiGatewayTestBase;
import ee.sm.ti.teis.officegateway.docgen.dto.GenerateDocumentDto;
import ee.sm.ti.teis.officegateway.docgen.request.GenerateDocumentRequest;
import ee.sm.ti.teis.servicerequest.RequestMetaDTO;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.test.context.support.WithMockUser;
import static ee.sm.ti.teis.officegateway.OfficeApiGatewayTestBase.MOCK_ROLE;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@WithMockUser(roles = MOCK_ROLE)
class DocGenTestControllerTest extends OfficeApiGatewayTestBase {
@Autowired
MockMvcUtil mockMvcUtil;
@Autowired
DocGenTestController controller;
......@@ -20,8 +33,13 @@ class DocGenTestControllerTest extends OfficeApiGatewayTestBase {
}
@Test
void generate_badRequest() {
assertThrows(NoResponseFromRabbitException.class, () ->
controller.generate("", "", "", "", "", new RequestMetaDTO()));
void generate_success() {
when(requestProcessingService.sendAndReceive(any(GenerateDocumentRequest.class)))
.thenReturn(new FileMetadataDto());
mockMvcUtil.testPost("/templates/generate", new GenerateDocumentDto(), new TypeReference<FileMetadataDto>() {
});
verify(requestProcessingService).sendAndReceive(any(GenerateDocumentRequest.class));
}
}
package ee.sm.ti.teis.officegateway.messages.messagedefinition;
import ee.sm.ti.teis.gatewaycommon.error.NoResponseFromRabbitException;
import com.fasterxml.jackson.core.type.TypeReference;
import ee.sm.ti.teis.gatewaycommon.MockMvcUtil;
import ee.sm.ti.teis.officegateway.OfficeApiGatewayTestBase;
import ee.sm.ti.teis.officegateway.messages.messagedefinition.dto.CreateMessageDefinitionDto;
import ee.sm.ti.teis.officegateway.messages.messagedefinition.dto.MessageDefinitionDto;
import ee.sm.ti.teis.officegateway.messages.messagedefinition.dto.UpdateMessageDefinitionDto;
import ee.sm.ti.teis.servicerequest.RequestMetaDTO;
import ee.sm.ti.teis.officegateway.messages.messagedefinition.request.CreateMessageDefinitionRequest;
import ee.sm.ti.teis.officegateway.messages.messagedefinition.request.DeleteMessageDefinitionRequest;
import ee.sm.ti.teis.officegateway.messages.messagedefinition.request.MessageDefinitionsRequest;
import ee.sm.ti.teis.officegateway.messages.messagedefinition.request.UpdateMessageDefinitionRequest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.test.context.support.WithMockUser;
import java.util.UUID;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static ee.sm.ti.teis.officegateway.OfficeApiGatewayTestBase.MOCK_ROLE;
import static java.util.UUID.randomUUID;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@WithMockUser(roles = MOCK_ROLE)
public class MessageDefinitionControllerTest extends OfficeApiGatewayTestBase {
private static final String BASE_URL = "/message-definitions/";
@Autowired
MessageDefinitionController controller;
MockMvcUtil mockMvcUtil;
@Test
void create_badRequest() {
assertThrows(NoResponseFromRabbitException.class, () -> controller.create(new CreateMessageDefinitionDto(), new RequestMetaDTO()));
void create_success() {
when(requestProcessingService.sendAndReceive(any(CreateMessageDefinitionRequest.class)))
.thenReturn(new MessageDefinitionDto());
mockMvcUtil.testPost(BASE_URL, new CreateMessageDefinitionDto(), new TypeReference<MessageDefinitionDto>() {
});
verify(requestProcessingService).sendAndReceive(any(CreateMessageDefinitionRequest.class));
}
@Test
void update_badRequest() {
assertThrows(NoResponseFromRabbitException.class, () -> controller.update(UUID.randomUUID(), new UpdateMessageDefinitionDto(), new RequestMetaDTO()));
void update_success() {
when(requestProcessingService.sendAndReceive(any(UpdateMessageDefinitionRequest.class)))
.thenReturn(new MessageDefinitionDto());
mockMvcUtil.testPut(BASE_URL + randomUUID(), new UpdateMessageDefinitionDto(), new TypeReference<MessageDefinitionDto>() {
});
verify(requestProcessingService).sendAndReceive(any(UpdateMessageDefinitionRequest.class));
}
@Test
void get_badRequest() {
assertThrows(NoResponseFromRabbitException.class, () -> controller.get(new RequestMetaDTO()));
void get_success() {
when(requestProcessingService.sendAndReceive(any(MessageDefinitionsRequest.class)))
.thenReturn(List.of(new MessageDefinitionDto()));
mockMvcUtil.testGet(BASE_URL, null, new TypeReference<List<MessageDefinitionDto>>() {
});
verify(requestProcessingService).sendAndReceive(any(MessageDefinitionsRequest.class));
}
@Test
void delete_badRequest() {
assertThrows(NoResponseFromRabbitException.class, () -> controller.delete(UUID.randomUUID(), new RequestMetaDTO()));
void delete_success() {
mockMvcUtil.testDelete(BASE_URL + "/" + randomUUID());
verify(requestProcessingService).sendAndReceive(any(DeleteMessageDefinitionRequest.class));
}
}
package ee.sm.ti.teis.officegateway.proceedings.proceduralact;
import com.fasterxml.jackson.core.type.TypeReference;
import ee.sm.ti.teis.gatewaycommon.MockMvcUtil;
import ee.sm.ti.teis.gatewaycommon.error.NoResponseFromRabbitException;
import ee.sm.ti.teis.officegateway.OfficeApiGatewayTestBase;
import ee.sm.ti.teis.officegateway.proceedings.proceduralact.controller.ProceduralActController;
import ee.sm.ti.teis.officegateway.proceedings.proceduralact.dto.ProceduralActConductingTimeDto;
import ee.sm.ti.teis.officegateway.proceedings.proceduralact.dto.ProceduralActDto;
import ee.sm.ti.teis.officegateway.proceedings.proceduralact.request.*;
import ee.sm.ti.teis.officegateway.proceedings.supervision.dto.StatusDto;
import ee.sm.ti.teis.servicerequest.RequestMetaDTO;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.test.context.support.WithMockUser;
import java.time.LocalDate;
import java.util.List;
import static ee.sm.ti.teis.officegateway.OfficeApiGatewayTestBase.MOCK_ROLE;
import static java.util.UUID.randomUUID;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@WithMockUser(roles = MOCK_ROLE)
class ProceduralActControllerTest extends OfficeApiGatewayTestBase {
private static final String BASE_URL = "/procedural-acts/";
@Autowired
MockMvcUtil mockMvcUtil;
@Autowired
ProceduralActController controller;
@Test
void getProceduralActs_badRequest() {
assertThrows(NoResponseFromRabbitException.class, () -> controller.getProceduralActs(randomUUID(), new RequestMetaDTO()));
void getProceduralActs_success() {
when(requestProcessingService.sendAndReceive(any(ProceduralActsRequest.class)))
.thenReturn(List.of(new ProceduralActDto()));
parameters.add("proceedingId", randomUUID().toString());
mockMvcUtil.testGet(BASE_URL, parameters, new TypeReference<List<ProceduralActDto>>() {
});
verify(requestProcessingService).sendAndReceive(any(ProceduralActsRequest.class));
}
@Test
void getProceduralAct_badRequest() {
assertThrows(NoResponseFromRabbitException.class, () -> controller.getProceduralAct(randomUUID(), new RequestMetaDTO()));
void getProceduralAct_success() {
when(requestProcessingService.sendAndReceive(any(ProceduralActRequest.class)))
.thenReturn(new ProceduralActDto());
mockMvcUtil.testGet(BASE_URL + randomUUID(), null, new TypeReference<ProceduralActDto>() {
});
verify(requestProcessingService).sendAndReceive(any(ProceduralActRequest.class));
}
@Test
void create_badRequest() {
assertThrows(NoResponseFromRabbitException.class, () -> controller.create(new ProceduralActDto(),
new RequestMetaDTO()));
void create_success() {
when(requestProcessingService.sendAndReceive(any(CreateProceduralActRequest.class)))
.thenReturn(new ProceduralActDto());
mockMvcUtil.testPost(BASE_URL, new ProceduralActDto(), new TypeReference<ProceduralActDto>() {
});
verify(requestProcessingService).sendAndReceive(any(CreateProceduralActRequest.class));
}
@Test
void updateStatus_badRequest() {
assertThrows(NoResponseFromRabbitException.class, () -> controller.updateStatus(randomUUID(),
new StatusDto(), new RequestMetaDTO()));
void update_success() {
when(requestProcessingService.sendAndReceive(any(UpdateProceduralActRequest.class)))
.thenReturn(new ProceduralActDto());
mockMvcUtil.testPut(BASE_URL + randomUUID(), new ProceduralActDto(), new TypeReference<ProceduralActDto>() {
});
verify(requestProcessingService).sendAndReceive(any(UpdateProceduralActRequest.class));
}
@Test
void delete_badRequest() {
assertThrows(NoResponseFromRabbitException.class, () -> controller.delete(randomUUID(), new RequestMetaDTO()));
void updateStatus_success() {
when(requestProcessingService.sendAndReceive(any(UpdateProceduralActStatusRequest.class)))
.thenReturn(new ProceduralActDto());
mockMvcUtil.testPatch(BASE_URL + randomUUID() + "/status", new StatusDto(), new TypeReference<ProceduralActDto>() {
});
verify(requestProcessingService).sendAndReceive(any(UpdateProceduralActStatusRequest.class));
}
@Test
void createProceduralActConductingTime_badRequest() {
assertThrows(NoResponseFromRabbitException.class, () ->
controller.createProceduralActConductingTime(randomUUID