Skip to content
Snippets Groups Projects
Commit 2d0a353e authored by OZGCloud's avatar OZGCloud
Browse files

Merge pull request 'OZG-6720-signatur-bearbeiten' (#54) from...

Merge pull request 'OZG-6720-signatur-bearbeiten' (#54) from OZG-6720-signatur-bearbeiten into master

Reviewed-on: https://git.ozg-sh.de/ozgcloud-app/administration/pulls/54


Reviewed-by: default avatarOZGCloud <ozgcloud@mgm-tp.com>
parents 086fdc37 d2f271f6
Branches
Tags
No related merge requests found
Showing
with 243 additions and 1 deletion
......@@ -21,6 +21,7 @@ import lombok.extern.jackson.Jacksonized;
public class OrganisationsEinheit {
static final String COLLECTION_NAME = "organisationsEinheit";
static final String FIELD_SETTINGS = "settings";
@JsonIgnore
@Id
......
......@@ -3,7 +3,9 @@ package de.ozgcloud.admin.organisationseinheit;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
......@@ -36,4 +38,11 @@ public class OrganisationsEinheitController {
var children = organisationsEinheitService.getChildren(id);
return assembler.toChildrenCollectionModel(id, children);
}
@PatchMapping("/{id}")
public EntityModel<OrganisationsEinheit> editOrganisationsEinheit(@PathVariable String id,
@RequestBody OrganisationsEinheitPatch organisationsEinheitPatch) {
var patched = organisationsEinheitService.editOrganisationsEinheit(id, organisationsEinheitPatch);
return assembler.toModel(patched);
}
}
......@@ -20,6 +20,8 @@ class OrganisationsEinheitModelAssembler
static final String REL_CHILD_ORGANISATIONS_EINHEITEN = "childList";
static final String REL_EDIT = "edit";
@Override
public EntityModel<OrganisationsEinheit> toModel(OrganisationsEinheit organisationsEinheit) {
var halModelBuilder = HalModelBuilder.halModelOf(organisationsEinheit);
......@@ -28,7 +30,8 @@ class OrganisationsEinheitModelAssembler
return halModelBuilder
.<EntityModel<OrganisationsEinheit>>build()
.add(linkTo(methodOn(OrganisationsEinheitController.class).getById(organisationsEinheit.getId())).withSelfRel());
.add(linkTo(methodOn(OrganisationsEinheitController.class).getById(organisationsEinheit.getId())).withSelfRel())
.add(linkTo(methodOn(OrganisationsEinheitController.class).editOrganisationsEinheit(organisationsEinheit.getId(), null)).withRel(REL_EDIT));
}
void embedChildOrganisationsEinheiten(OrganisationsEinheit organisationsEinheit, HalModelBuilder halModelBuilder) {
......
package de.ozgcloud.admin.organisationseinheit;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PUBLIC)
@ToString
@Builder
@Getter
public class OrganisationsEinheitPatch {
private String signatur;
}
......@@ -6,6 +6,7 @@ import java.util.Objects;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.stereotype.Service;
import de.ozgcloud.common.errorhandling.TechnicalException;
import lombok.RequiredArgsConstructor;
@Service
......@@ -40,4 +41,19 @@ class OrganisationsEinheitService {
public List<OrganisationsEinheit> getChildren(String parentId) {
return repository.findChildren(parentId);
}
public OrganisationsEinheit editOrganisationsEinheit(String id, OrganisationsEinheitPatch organisationsEinheitPatch) {
return repository.findById(id)
.map(organisationsEinheit -> patchOrganisationsEinheit(organisationsEinheit, organisationsEinheitPatch))
.map(repository::save)
.orElseThrow(() -> new TechnicalException("Organisationseinheit with id " + id + " not found"));
}
OrganisationsEinheit patchOrganisationsEinheit(OrganisationsEinheit organisationsEinheit, OrganisationsEinheitPatch patch) {
var organisationsEinheitBuilder = organisationsEinheit.toBuilder();
if (Objects.nonNull(patch.getSignatur())) {
organisationsEinheitBuilder.settings(organisationsEinheit.getSettings().toBuilder().signatur(patch.getSignatur()).build());
}
return organisationsEinheitBuilder.build();
}
}
......@@ -10,18 +10,25 @@ import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
class OrganisationsEinheitControllerTest {
private final ObjectMapper objectMapper = new ObjectMapper();
@InjectMocks
private OrganisationsEinheitController controller;
......@@ -176,4 +183,60 @@ class OrganisationsEinheitControllerTest {
return mockMvc.perform(get(OrganisationsEinheitController.PATH + "/" + OrganisationsEinheitTestFactory.ID + "/children"));
}
}
@Nested
class TestEditOrganisationsEinheit {
private final OrganisationsEinheit patchedOrganisationsEinheit = OrganisationsEinheitTestFactory.create();
@Captor
private ArgumentCaptor<OrganisationsEinheitPatch> organisationsEinheitPatchArgumentCaptor;
@BeforeEach
void setUp() {
when(organisationsEinheitService.editOrganisationsEinheit(eq(OrganisationsEinheitTestFactory.ID), any())).thenReturn(
patchedOrganisationsEinheit);
when(assembler.toModel(patchedOrganisationsEinheit)).thenReturn(EntityModel.of(patchedOrganisationsEinheit));
}
@Test
void shouldCallService() {
doRequest();
verify(organisationsEinheitService).editOrganisationsEinheit(eq(OrganisationsEinheitTestFactory.ID),
organisationsEinheitPatchArgumentCaptor.capture());
assertThat(organisationsEinheitPatchArgumentCaptor.getValue())
.usingRecursiveComparison()
.isEqualTo(OrganisationsEinheitPatchTestFactory.create());
}
@SneakyThrows
@Test
void shouldReturnStatusOk() {
doRequest().andExpect(status().isOk());
}
@Test
void shouldCallAssembler() {
doRequest();
verify(assembler).toModel(patchedOrganisationsEinheit);
}
@SneakyThrows
@Test
void shouldReturnModel() {
var model = controller.editOrganisationsEinheit(OrganisationsEinheitTestFactory.ID, OrganisationsEinheitPatchTestFactory.create());
assertThat(model).usingRecursiveComparison().isEqualTo(EntityModel.of(patchedOrganisationsEinheit));
}
@SneakyThrows
private ResultActions doRequest() {
return mockMvc.perform(
patch(OrganisationsEinheitController.PATH + "/" + OrganisationsEinheitTestFactory.ID)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(OrganisationsEinheitPatchTestFactory.create())));
}
}
}
\ No newline at end of file
......@@ -63,6 +63,17 @@ class OrganisationsEinheitModelAssemblerTest {
verify(assembler).embedChildOrganisationsEinheiten(eq(organisationsEinheit), any());
}
@Test
void shouldAddEditLink() {
var model = assembler.toModel(organisationsEinheit);
assertThat(model.getLink(OrganisationsEinheitModelAssembler.REL_EDIT))
.isNotEmpty()
.get()
.extracting(Link::getHref)
.isEqualTo(new UriTemplate(OrganisationsEinheitController.PATH + "/{id}").expand(OrganisationsEinheitTestFactory.ID).toString());
}
}
@Nested
......
package de.ozgcloud.admin.organisationseinheit;
import com.thedeanda.lorem.LoremIpsum;
public class OrganisationsEinheitPatchTestFactory {
public static final String SIGNATUR = LoremIpsum.getInstance().getWords(3);
public static OrganisationsEinheitPatch create() {
return createBuilder().build();
}
public static OrganisationsEinheitPatch.OrganisationsEinheitPatchBuilder createBuilder() {
return OrganisationsEinheitPatch.builder().signatur(SIGNATUR);
}
}
......@@ -15,6 +15,8 @@ import org.mockito.Mock;
import org.mockito.Spy;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import de.ozgcloud.common.errorhandling.TechnicalException;
class OrganisationsEinheitServiceTest {
@Spy
......@@ -158,4 +160,107 @@ class OrganisationsEinheitServiceTest {
assertThat(children).containsExactly(organisationsEinheit);
}
}
@Nested
class TestEditOrganisationsEinheit {
private final OrganisationsEinheit organisationsEinheit = OrganisationsEinheitTestFactory.create();
private final OrganisationsEinheit patchedOrganisationsEinheit = OrganisationsEinheitTestFactory.create();
private final OrganisationsEinheitPatch organisationsEinheitPatch = OrganisationsEinheitPatchTestFactory.create();
@Nested
class OrgnisationsEinheitExists {
@BeforeEach
void setUp() {
when(repository.findById(OrganisationsEinheitTestFactory.ID)).thenReturn(Optional.of(organisationsEinheit));
doReturn(patchedOrganisationsEinheit).when(service).patchOrganisationsEinheit(organisationsEinheit, organisationsEinheitPatch);
doReturn(patchedOrganisationsEinheit).when(repository).save(patchedOrganisationsEinheit);
}
@Test
void shouldLoadOrganisationsEinheit() {
service.editOrganisationsEinheit(OrganisationsEinheitTestFactory.ID, organisationsEinheitPatch);
verify(repository).findById(OrganisationsEinheitTestFactory.ID);
}
@Test
void shouldPatch() {
service.editOrganisationsEinheit(OrganisationsEinheitTestFactory.ID, organisationsEinheitPatch);
verify(service).patchOrganisationsEinheit(organisationsEinheit, organisationsEinheitPatch);
}
@Test
void shouldSavedPatchedOrganisationsEinheit() {
service.editOrganisationsEinheit(OrganisationsEinheitTestFactory.ID, organisationsEinheitPatch);
verify(repository).save(patchedOrganisationsEinheit);
}
@Test
void shouldReturnPatchedOrganisationsEinheit() {
var patched = service.editOrganisationsEinheit(OrganisationsEinheitTestFactory.ID, organisationsEinheitPatch);
assertThat(patched)
.usingRecursiveComparison()
.isEqualTo(patchedOrganisationsEinheit);
}
}
@Nested
class OrganisationsEinheitDoesNotExists {
@BeforeEach
void setUp() {
when(repository.findById(OrganisationsEinheitTestFactory.ID)).thenReturn(Optional.empty());
}
@Test
void shouldThrowTechnicalException() {
assertThatThrownBy(() ->
service.editOrganisationsEinheit(OrganisationsEinheitTestFactory.ID, OrganisationsEinheitPatchTestFactory.create()))
.isInstanceOf(TechnicalException.class)
.hasMessageStartingWith("Organisationseinheit with id " + OrganisationsEinheitTestFactory.ID + " not found");
}
}
}
@Nested
class TestPatchOrganisationsEinheit {
@Test
void shouldPatchSignatur() {
var organisationsEinheit = OrganisationsEinheitTestFactory.create();
var organisationsEinheitPatch = OrganisationsEinheitPatchTestFactory.create();
var patched = service.patchOrganisationsEinheit(organisationsEinheit, organisationsEinheitPatch);
assertThat(patched.getSettings().getSignatur()).isEqualTo(OrganisationsEinheitPatchTestFactory.SIGNATUR);
}
@Test
void shouldNotPatchOtherFields() {
var organisationsEinheit = OrganisationsEinheitTestFactory.create();
var organisationsEinheitPatch = OrganisationsEinheitPatchTestFactory.create();
var patched = service.patchOrganisationsEinheit(organisationsEinheit, organisationsEinheitPatch);
assertThat(patched)
.usingRecursiveComparison()
.ignoringFields(OrganisationsEinheit.FIELD_SETTINGS)
.isEqualTo(organisationsEinheit);
}
@Test
void shouldNotPatchSignaturIfNull() {
var organisationsEinheit = OrganisationsEinheitTestFactory.create();
var organisationsEinheitPatch = OrganisationsEinheitPatchTestFactory.createBuilder().signatur(null).build();
var patched = service.patchOrganisationsEinheit(organisationsEinheit, organisationsEinheitPatch);
assertThat(patched.getSettings().getSignatur()).isEqualTo(OrganisationsEinheitSettingsTestFactory.SIGNATUR);
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment