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

OZG-6676 OZG-6880 implement model assembler for fachstelle resource

parent cd9735f5
No related branches found
No related tags found
No related merge requests found
......@@ -11,5 +11,7 @@ class Fachstelle {
@JsonIgnore
private String id;
private String name;
private String anschrift;
private String email;
}
......@@ -2,7 +2,9 @@ package de.ozgcloud.alfa.collaboration;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
......@@ -21,4 +23,9 @@ class FachstelleController {
public CollectionModel<EntityModel<Fachstelle>> search(@RequestParam String searchBy) {
return null;
}
@GetMapping("/{organisationsEinheitId}")
public ResponseEntity<EntityModel<Fachstelle>> getById(@PathVariable String id) {
return null;
}
}
package de.ozgcloud.alfa.collaboration;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
import jakarta.annotation.Nonnull;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.server.RepresentationModelAssembler;
import org.springframework.stereotype.Component;
import de.ozgcloud.alfa.common.ModelBuilder;
@Component
class FachstelleModelAssembler
implements RepresentationModelAssembler<Fachstelle, EntityModel<Fachstelle>> {
@Override
public EntityModel<Fachstelle> toModel(@Nonnull Fachstelle entity) {
return ModelBuilder.fromEntity(entity)
.addLink(linkTo(FachstelleController.class).slash(entity.getId()).withSelfRel())
.buildModel();
}
@Override
public CollectionModel<EntityModel<Fachstelle>> toCollectionModel(Iterable<? extends Fachstelle> entities) {
return RepresentationModelAssembler.super.toCollectionModel(entities)
.add(linkTo(FachstelleController.class).withSelfRel());
}
}
package de.ozgcloud.alfa.collaboration;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.List;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.Spy;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.IanaLinkRelations;
import org.springframework.hateoas.Link;
class FachstelleModelAssemblerTest {
@Spy
private FachstelleModelAssembler assembler;
@Nested
class TestToModel {
@Test
void shouldHaveSelfLink() {
var model = assembler.toModel(FachstelleTestFactory.create());
assertHaveSelfLink(model);
}
private void assertHaveSelfLink(EntityModel<Fachstelle> model) {
assertThat(model.getLink(IanaLinkRelations.SELF_VALUE)).isPresent().get().extracting(Link::getHref)
.isEqualTo(FachstelleController.PATH + "/" + FachstelleTestFactory.ID);
}
}
@Nested
class TestToCollectionModel {
private Fachstelle fachstelle = FachstelleTestFactory.create();
@Test
void shouldCallToModel() {
callAssembler();
verify(assembler).toModel(fachstelle);
}
@Test
void shouldHaveSelfLink() {
var model = callAssembler();
assertThat(model.getLink(IanaLinkRelations.SELF_VALUE)).isPresent().get().extracting(Link::getHref)
.isEqualTo(FachstelleController.PATH);
}
@Test
void shouldContainEntityModel() {
var entityModel = EntityModel.of(fachstelle);
doReturn(entityModel).when(assembler).toModel(fachstelle);
var model = callAssembler();
assertThat(model).containsExactly(entityModel);
}
private CollectionModel<EntityModel<Fachstelle>> callAssembler() {
return assembler.toCollectionModel(List.of(fachstelle));
}
}
}
/*
* Copyright (C) 2024 Das Land Schleswig-Holstein vertreten durch den
* Ministerpräsidenten des Landes Schleswig-Holstein
* Staatskanzlei
* Abteilung Digitalisierung und zentrales IT-Management der Landesregierung
*
* Lizenziert unter der EUPL, Version 1.2 oder - sobald
* diese von der Europäischen Kommission genehmigt wurden -
* Folgeversionen der EUPL ("Lizenz");
* Sie dürfen dieses Werk ausschließlich gemäß
* dieser Lizenz nutzen.
* Eine Kopie der Lizenz finden Sie hier:
*
* https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* Sofern nicht durch anwendbare Rechtsvorschriften
* gefordert oder in schriftlicher Form vereinbart, wird
* die unter der Lizenz verbreitete Software "so wie sie
* ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN -
* ausdrücklich oder stillschweigend - verbreitet.
* Die sprachspezifischen Genehmigungen und Beschränkungen
* unter der Lizenz sind dem Lizenztext zu entnehmen.
*/
package de.ozgcloud.alfa.collaboration;
import java.util.UUID;
import com.thedeanda.lorem.LoremIpsum;
;
public class FachstelleTestFactory {
public static final String ID = UUID.randomUUID().toString();
public static final String NAME = LoremIpsum.getInstance().getName();
public static final String ANSCHRIFT = LoremIpsum.getInstance().getCity();
public static final String EMAIL = LoremIpsum.getInstance().getEmail();
public static Fachstelle create() {
return createBuilder().build();
}
public static Fachstelle.FachstelleBuilder createBuilder() {
return Fachstelle.builder()
.id(ID)
.name(NAME)
.anschrift(ANSCHRIFT)
.email(EMAIL);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment