Skip to content

Commit

Permalink
VUU86: Add tests for layout server (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
cfisher-scottlogic authored Nov 3, 2023
1 parent 7965bb5 commit 5c86765
Show file tree
Hide file tree
Showing 16 changed files with 841 additions and 31 deletions.
10 changes: 10 additions & 0 deletions layout-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down Expand Up @@ -54,6 +58,12 @@
<artifactId>modelmapper</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>13.0</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import lombok.RequiredArgsConstructor;
import org.finos.vuu.layoutserver.dto.request.LayoutRequestDTO;
import org.finos.vuu.layoutserver.dto.request.MetadataRequestDTO;
import org.finos.vuu.layoutserver.dto.response.MetadataResponseDTO;
import org.finos.vuu.layoutserver.model.Layout;
import org.finos.vuu.layoutserver.model.Metadata;
Expand All @@ -29,16 +28,6 @@ public ModelMapper modelMapper() {
metadata -> layoutService.getLayoutByMetadataId(metadata.getId()),
MetadataResponseDTO::setLayoutId));

mapper.typeMap(MetadataRequestDTO.class, Metadata.class)
.addMappings(m -> m.map(
MetadataRequestDTO::getBaseMetadata,
Metadata::setBaseMetadata));

mapper.typeMap(Metadata.class, MetadataResponseDTO.class)
.addMappings(m -> m.map(
Metadata::getBaseMetadata,
MetadataResponseDTO::setBaseMetadata));

return mapper;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import java.util.UUID;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.finos.vuu.layoutserver.dto.request.LayoutRequestDTO;
import org.finos.vuu.layoutserver.dto.response.LayoutResponseDTO;
Expand All @@ -11,6 +12,7 @@
import org.finos.vuu.layoutserver.service.MetadataService;
import org.modelmapper.ModelMapper;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -24,6 +26,7 @@
@RequiredArgsConstructor
@RestController
@RequestMapping("/layouts")
@Validated
public class LayoutController {

private final LayoutService layoutService;
Expand Down Expand Up @@ -65,7 +68,7 @@ public List<MetadataResponseDTO> getMetadata() {
*/
@ResponseStatus(HttpStatus.CREATED)
@PostMapping
public LayoutResponseDTO createLayout(@RequestBody LayoutRequestDTO layoutToCreate) {
public LayoutResponseDTO createLayout(@Valid @RequestBody LayoutRequestDTO layoutToCreate) {
Layout layout = mapper.map(layoutToCreate, Layout.class);

Layout createdLayout = layoutService.getLayout(layoutService.createLayout(layout));
Expand All @@ -81,7 +84,7 @@ public LayoutResponseDTO createLayout(@RequestBody LayoutRequestDTO layoutToCrea
*/
@ResponseStatus(HttpStatus.NO_CONTENT)
@PutMapping("/{id}")
public void updateLayout(@PathVariable UUID id, @RequestBody LayoutRequestDTO layout) {
public void updateLayout(@PathVariable UUID id, @Valid @RequestBody LayoutRequestDTO layout) {
Layout newLayout = mapper.map(layout, Layout.class);

layoutService.updateLayout(id, newLayout);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package org.finos.vuu.layoutserver.dto.request;

import com.fasterxml.jackson.annotation.JsonProperty;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import lombok.Data;

@Data
public class LayoutRequestDTO {

/**
* The definition of the layout as a string (i.e. stringified JSON structure containing components)
* The definition of the layout as a string (e.g. stringified JSON structure containing
* components)
*/
@JsonProperty(value = "definition", required = true)
@NotBlank(message = "Definition must not be blank")
private String definition;

@JsonProperty(value = "metadata", required = true)
@NotNull(message = "Metadata must not be null")
private MetadataRequestDTO metadata;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.finos.vuu.layoutserver.dto.response;

import com.fasterxml.jackson.annotation.JsonUnwrapped;
import java.util.Date;
import java.time.LocalDate;
import java.util.UUID;
import lombok.Data;
import org.finos.vuu.layoutserver.model.BaseMetadata;
Expand All @@ -14,6 +14,6 @@ public class MetadataResponseDTO {
@JsonUnwrapped
BaseMetadata baseMetadata;

private Date created;
private Date updated;
private LocalDate created;
private LocalDate updated;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.finos.vuu.layoutserver.exceptions;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(NoSuchElementException.class)
public ResponseEntity<String> handleNotFound(NoSuchElementException ex) {
return new ResponseEntity<>(ex.getMessage(),
org.springframework.http.HttpStatus.NOT_FOUND);
}

@ExceptionHandler({
HttpMessageNotReadableException.class,
MethodArgumentTypeMismatchException.class})
public ResponseEntity<String> handleBadRequest(Exception ex) {
return new ResponseEntity<>(ex.getMessage(),
org.springframework.http.HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<String> handleMethodArgumentNotValid(MethodArgumentNotValidException ex) {
List<String> errors = ex.getFieldErrors()
.stream()
.map(fieldError -> fieldError.getField() + ": " + fieldError.getDefaultMessage())
.collect(Collectors.toList());

return new ResponseEntity<>(errors.toString(),
org.springframework.http.HttpStatus.BAD_REQUEST);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.finos.vuu.layoutserver.model;

import java.util.UUID;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
Expand All @@ -9,7 +10,6 @@
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import lombok.Data;
import java.util.UUID;

@Data
@Entity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.finos.vuu.layoutserver.model;

import java.util.Date;
import java.time.LocalDate;
import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.Embedded;
Expand Down Expand Up @@ -28,8 +28,7 @@ public class Metadata {
@Embedded
private BaseMetadata baseMetadata;

private Date created = new Date();

private Date updated;
private final LocalDate created = LocalDate.now();

private LocalDate updated;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.finos.vuu.layoutserver.repository;

import java.util.UUID;
import org.finos.vuu.layoutserver.model.Metadata;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.UUID;

@Repository
public interface MetadataRepository extends CrudRepository<Metadata, UUID> {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.finos.vuu.layoutserver.service;

import java.util.Date;
import java.time.LocalDate;
import java.util.NoSuchElementException;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.finos.vuu.layoutserver.model.Layout;
Expand All @@ -15,7 +16,8 @@ public class LayoutService {
private final LayoutRepository layoutRepository;

public Layout getLayout(UUID id) {
return layoutRepository.findById(id).orElseThrow();
return layoutRepository.findById(id)
.orElseThrow(() -> new NoSuchElementException("Layout with ID '" + id + "' not found"));
}

public Layout getLayoutByMetadataId(UUID id) {
Expand All @@ -32,7 +34,7 @@ public void updateLayout(UUID layoutId, Layout newLayout) {

Metadata updatedMetadata = Metadata.builder()
.baseMetadata(newMetadata.getBaseMetadata())
.updated(new Date())
.updated(LocalDate.now())
.build();

layoutToUpdate.setDefinition(newLayout.getDefinition());
Expand All @@ -42,6 +44,10 @@ public void updateLayout(UUID layoutId, Layout newLayout) {
}

public void deleteLayout(UUID id) {
layoutRepository.deleteById(id);
try {
layoutRepository.deleteById(id);
} catch (Exception e) {
throw new NoSuchElementException("Layout with ID '" + id + "' not found");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package org.finos.vuu.layoutserver.service;

import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.finos.vuu.layoutserver.model.Metadata;
import org.finos.vuu.layoutserver.repository.MetadataRepository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@Service
public class MetadataService {
Expand Down
Loading

0 comments on commit 5c86765

Please sign in to comment.