Skip to content
Snippets Groups Projects
Commit 0e3d53cc authored by Krzysztof Witukiewicz's avatar Krzysztof Witukiewicz
Browse files

OZG-7501 OZG-7874 Small refactorings

parent 7dc714eb
Branches
Tags
1 merge request!19Ozg 7501 weiterleitung vorbereiten
Showing
with 80 additions and 95 deletions
......@@ -59,4 +59,8 @@ public class OrganisationsEinheitController {
public CollectionModel<EntityModel<OrganisationsEinheitHeader>> search(@RequestParam String searchBy) {
return headerModelAssembler.toCollectionModel(service.searchOrganisationsEinheiten(searchBy).toList());
}
public OrganisationsEinheit getOrganisationEinheit(String organisationEinheitId) {
return service.getById(organisationEinheitId);
}
}
......@@ -32,12 +32,10 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import de.ozgcloud.alfa.collaboration.OrganisationsEinheit;
import de.ozgcloud.alfa.collaboration.OrganisationsEinheitController;
import de.ozgcloud.alfa.common.command.CommandController;
import de.ozgcloud.alfa.common.command.CommandService;
import de.ozgcloud.alfa.common.command.CreateCommand;
import de.ozgcloud.common.errorhandling.TechnicalException;
import lombok.RequiredArgsConstructor;
@RestController
......@@ -52,25 +50,19 @@ class ForwardByVorgangCommandController {
@PostMapping
public ResponseEntity<Void> createCommand(@PathVariable String vorgangId, @PathVariable long vorgangVersion, @RequestBody CreateCommand command) {
var body = (ForwardVorgangCommandBody) command.getBody();
var completeBody = addOrganisationEinheitData((ForwardVorgangCommandBody) command.getBody());
var enrichedCommand = command.toBuilder()
.vorgangId(vorgangId)
.relationId(vorgangId)
.body(addOrganisationsEinheitData(getOrganisationsEinheitById(body.getOrganisationsEinheitId()), body))
.body(completeBody)
.build();
var created = commandService.createCommand(enrichedCommand, vorgangVersion);
return ResponseEntity.created(linkTo(CommandController.class).slash(created.getId()).toUri()).build();
}
private OrganisationsEinheit getOrganisationsEinheitById(String id) {
var response = organisationsEinheitController.getById(id);
if (!response.getStatusCode().is2xxSuccessful() || response.getBody() == null) {
throw new TechnicalException("Could not get Organisationseinheit by id: " + id);
}
return response.getBody().getContent();
}
private ForwardVorgangCommandBody addOrganisationsEinheitData(OrganisationsEinheit organisationsEinheit, ForwardVorgangCommandBody body) {
return bodyMapper.updateFromOrganisationsEinheit(organisationsEinheit, body);
private ForwardVorgangCommandBody addOrganisationEinheitData(ForwardVorgangCommandBody body) {
var organisationEinheit = organisationsEinheitController.getOrganisationEinheit(body.getOrganisationEinheitId());
return bodyMapper.updateFromOrganisationEinheit(organisationEinheit, body);
}
}
......@@ -41,7 +41,7 @@ import lombok.Setter;
public class ForwardVorgangCommandBody implements CommandBody {
@JsonDeserialize(using = LinkedResourceDeserializer.class)
private String organisationsEinheitId;
private String organisationEinheitId;
private String name;
private String strasse;
......
......@@ -32,7 +32,7 @@ import de.ozgcloud.alfa.collaboration.OrganisationsEinheit;
@Mapper
interface ForwardVorgangCommandBodyMapper {
@Mapping(target = "organisationsEinheitId", ignore = true)
@Mapping(target = "organisationEinheitId", ignore = true)
@Mapping(target = ".", source = "anschrift")
ForwardVorgangCommandBody updateFromOrganisationsEinheit(OrganisationsEinheit organisationsEinheit, @MappingTarget ForwardVorgangCommandBody commandBody);
ForwardVorgangCommandBody updateFromOrganisationEinheit(OrganisationsEinheit organisationsEinheit, @MappingTarget ForwardVorgangCommandBody commandBody);
}
......@@ -23,6 +23,7 @@
*/
package de.ozgcloud.alfa.collaboration;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
......@@ -256,4 +257,24 @@ class OrganisationsEinheitControllerTest {
}
}
@Nested
class TestGetOrganisationEinheit {
@Test
void shouldCallService() {
controller.getOrganisationEinheit(OrganisationsEinheitTestFactory.ID);
verify(service).getById(OrganisationsEinheitTestFactory.ID);
}
@Test
void shouldReturnOrganisationEinheit() {
var organisationEinheitFromService = OrganisationsEinheitTestFactory.create();
when(service.getById(any())).thenReturn(organisationEinheitFromService);
var organisationsEinheit = controller.getOrganisationEinheit(OrganisationsEinheitTestFactory.ID);
assertThat(organisationsEinheit).isSameAs(organisationEinheitFromService);
}
}
}
......@@ -37,10 +37,7 @@ import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.springframework.hateoas.EntityModel;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
......@@ -81,86 +78,57 @@ class ForwardByVorgangCommandControllerTest {
@Nested
class TestCreateCommand {
private final OrganisationsEinheit organisationsEinheit = OrganisationsEinheitTestFactory.create();
@Captor
private ArgumentCaptor<ForwardVorgangCommandBody> bodyCaptor;
@Nested
class OnOrganisationsEinheitAvailable {
private final OrganisationsEinheit organisationsEinheit = OrganisationsEinheitTestFactory.create();
@Captor
private ArgumentCaptor<CreateCommand> commandCaptor;
@BeforeEach
void init() {
when(organisationsEinheitController.getById(any())).thenReturn(
new ResponseEntity<>(EntityModel.of(organisationsEinheit), HttpStatus.OK));
when(commandService.createCommand(any(), anyLong())).thenReturn(CommandTestFactory.create());
when(bodyMapper.updateFromOrganisationsEinheit(any(), any())).thenReturn(ForwardVorgangCommandBodyTestFactory.create());
}
@Test
void shouldCallOrganisationsEinheitController() {
doRequest();
verify(organisationsEinheitController).getById(OrganisationsEinheitTestFactory.ID);
}
@Test
void shouldAddOrganisationsEinheitData() {
doRequest();
verify(controller).createCommand(any(), anyLong(), commandCaptor.capture());
verify(bodyMapper).updateFromOrganisationsEinheit(organisationsEinheit, (ForwardVorgangCommandBody) commandCaptor.getValue().getBody());
}
@Test
void shouldCallCommandService() {
doRequest();
private ArgumentCaptor<CreateCommand> commandCaptor;
@Captor
private ArgumentCaptor<ForwardVorgangCommandBody> commandBodyCaptor;
verify(commandService).createCommand(commandCaptor.capture(), eq(VorgangHeaderTestFactory.VERSION));
assertThat(commandCaptor.getValue()).usingRecursiveComparison().isEqualTo(CreateForwardVorgangCommandTestFactory.create());
}
@BeforeEach
void init() {
when(organisationsEinheitController.getOrganisationEinheit(any())).thenReturn(organisationsEinheit);
when(commandService.createCommand(any(), anyLong())).thenReturn(CommandTestFactory.create());
when(bodyMapper.updateFromOrganisationEinheit(any(), any())).thenReturn(ForwardVorgangCommandBodyTestFactory.create());
}
@SneakyThrows
@Test
void shouldReturnCreated() {
var response = doRequest();
@Test
void shouldCallOrganisationsEinheitController() {
doRequest();
response.andExpect(status().isCreated());
}
verify(organisationsEinheitController).getOrganisationEinheit(OrganisationsEinheitTestFactory.ID);
}
@SneakyThrows
@Test
void shouldReturnLinkToCreatedCommand() {
var response = doRequest();
@Test
void shouldAddOrganisationsEinheitData() {
doRequest();
response.andExpect(header().stringValues("location", "http://localhost" + COMMANDS_PATH + "/" + CommandTestFactory.ID));
}
verify(bodyMapper).updateFromOrganisationEinheit(same(organisationsEinheit), commandBodyCaptor.capture());
assertThat(commandBodyCaptor.getValue()).usingRecursiveComparison()
.isEqualTo(ForwardVorgangCommandBody.builder().organisationEinheitId(OrganisationsEinheitTestFactory.ID).build());
}
@Nested
class OnOrganisationsEinheitMissing {
@SneakyThrows
@Test
void shouldReturnInternalServerErrorIfNotFound() {
when(organisationsEinheitController.getById(any())).thenReturn(new ResponseEntity<>(HttpStatus.NOT_FOUND));
@Test
void shouldCallCommandService() {
doRequest();
var response = doRequest();
verify(commandService).createCommand(commandCaptor.capture(), eq(VorgangHeaderTestFactory.VERSION));
assertThat(commandCaptor.getValue()).usingRecursiveComparison().isEqualTo(CreateForwardVorgangCommandTestFactory.create());
}
response.andExpect(status().isInternalServerError());
}
@SneakyThrows
@Test
void shouldReturnCreated() {
var response = doRequest();
@SneakyThrows
@Test
void shouldReturnInternalServerErrorIfBodyIsEmpty() {
when(organisationsEinheitController.getById(any())).thenReturn(new ResponseEntity<>(null, HttpStatus.OK));
response.andExpect(status().isCreated());
}
var response = doRequest();
@SneakyThrows
@Test
void shouldReturnLinkToCreatedCommand() {
var response = doRequest();
response.andExpect(status().isInternalServerError());
}
response.andExpect(header().stringValues("location", "http://localhost" + COMMANDS_PATH + "/" + CommandTestFactory.ID));
}
@SneakyThrows
......@@ -173,7 +141,7 @@ class ForwardByVorgangCommandControllerTest {
private String createContent() {
return CommandTestFactory.buildCreateCommandWithBodyContent(
CommandOrder.FORWARD_VORGANG,
"{\"organisationsEinheitId\":\"%s\"}".formatted(OrganisationsEinheitTestFactory.ID));
"{\"organisationEinheitId\":\"%s\"}".formatted(OrganisationsEinheitTestFactory.ID));
}
}
}
......@@ -38,26 +38,26 @@ class ForwardVorgangCommandBodyMapperTest {
private final ForwardVorgangCommandBodyMapper mapper = Mappers.getMapper(ForwardVorgangCommandBodyMapper.class);
@Nested
class TestUpdateFromOrganisationsEinheit {
class TestUpdateFromOrganisationEinheit {
private static final String ORGANISATIONS_EINHEIT_ID = UUID.randomUUID().toString();
private final ForwardVorgangCommandBody body = ForwardVorgangCommandBody.builder()
.organisationsEinheitId(ORGANISATIONS_EINHEIT_ID).build();
.organisationEinheitId(ORGANISATIONS_EINHEIT_ID).build();
@Test
void shouldUpdateFromOrganisationsEinheit() {
mapper.updateFromOrganisationsEinheit(OrganisationsEinheitTestFactory.create(), body);
void shouldUpdateFromOrganisationEinheit() {
mapper.updateFromOrganisationEinheit(OrganisationsEinheitTestFactory.create(), body);
assertThat(body).usingRecursiveComparison().ignoringFields("organisationsEinheitId")
assertThat(body).usingRecursiveComparison().ignoringFields("organisationEinheitId")
.isEqualTo(ForwardVorgangCommandBodyTestFactory.create());
}
@Test
void shouldPreserveId() {
mapper.updateFromOrganisationsEinheit(OrganisationsEinheitTestFactory.create(), body);
mapper.updateFromOrganisationEinheit(OrganisationsEinheitTestFactory.create(), body);
assertThat(body.getOrganisationsEinheitId()).isEqualTo(ORGANISATIONS_EINHEIT_ID);
assertThat(body.getOrganisationEinheitId()).isEqualTo(ORGANISATIONS_EINHEIT_ID);
}
}
}
......@@ -35,7 +35,7 @@ class ForwardVorgangCommandBodyTestFactory {
static ForwardVorgangCommandBodyBuilder createBuilder() {
return ForwardVorgangCommandBody.builder()
.organisationsEinheitId(OrganisationsEinheitTestFactory.ID)
.organisationEinheitId(OrganisationsEinheitTestFactory.ID)
.name(OrganisationsEinheitTestFactory.NAME)
.strasse(AnschriftTestFactory.STRASSE)
.ort(AnschriftTestFactory.ORT)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment