Skip to content

Commit

Permalink
Merge pull request #104 from ScottLogic/VUU-81-definition-as-JsonNode
Browse files Browse the repository at this point in the history
Vuu 81 definition as json node
  • Loading branch information
vferraro-scottlogic authored Nov 17, 2023
2 parents 77e2472 + 1ad1522 commit 7fdeaa7
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package org.finos.vuu.layoutserver.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.RequiredArgsConstructor;
import org.finos.vuu.layoutserver.dto.response.ApplicationLayoutDto;
import org.finos.vuu.layoutserver.service.ApplicationLayoutService;
import org.modelmapper.ModelMapper;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RestController
Expand Down Expand Up @@ -37,7 +44,7 @@ public ApplicationLayoutDto getApplicationLayout(@RequestHeader("username") Stri
*/
@ResponseStatus(HttpStatus.CREATED)
@PutMapping
public void persistApplicationLayout(@RequestHeader("username") String username, @RequestBody JsonNode layoutDefinition) {
public void persistApplicationLayout(@RequestHeader("username") String username, @RequestBody ObjectNode layoutDefinition) {
service.persistApplicationLayout(username, layoutDefinition);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package org.finos.vuu.layoutserver.dto.request;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Data
public class LayoutRequestDto {

/**
* The definition of the layout as a string (e.g. stringified JSON structure containing
* components)
* The definition of the layout as an arbitrary JSON structure, describing all required components
*/
@JsonProperty(value = "definition", required = true)
@NotBlank(message = "Definition must not be blank")
private String definition;
@NotNull(message = "Definition must not be null")
private ObjectNode 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,10 +1,10 @@
package org.finos.vuu.layoutserver.dto.response;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.Data;

@Data
public class ApplicationLayoutDto {
private String username;
private JsonNode definition;
private ObjectNode definition;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.finos.vuu.layoutserver.dto.response;

import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.Data;

import java.util.UUID;
Expand All @@ -8,12 +9,11 @@
public class LayoutResponseDto {

private UUID id;

/**
* The definition of the layout as a string (e.g. stringified JSON structure containing
* components)
* The definition of the layout as an arbitrary JSON structure, describing all required components
*/
private String definition;
private ObjectNode definition;

private MetadataResponseDto metadata;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.finos.vuu.layoutserver.model;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.finos.vuu.layoutserver.utils.JsonNodeConverter;
import org.finos.vuu.layoutserver.utils.ObjectNodeConverter;

import javax.persistence.Column;
import javax.persistence.Convert;
Expand All @@ -19,7 +19,7 @@ public class ApplicationLayout {
@Id
private String username;

@Convert(converter = JsonNodeConverter.class)
@Convert(converter = ObjectNodeConverter.class)
@Column(columnDefinition = "JSON")
private JsonNode definition;
private ObjectNode definition;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.finos.vuu.layoutserver.model;

import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.Data;
import org.finos.vuu.layoutserver.utils.ObjectNodeConverter;

import javax.persistence.*;
import java.util.UUID;
Expand All @@ -13,14 +15,16 @@ public class Layout {
@Column(columnDefinition = "BINARY(16)")
private UUID id;

private String definition;
@Convert(converter = ObjectNodeConverter.class)
@Column(columnDefinition = "JSON")
private ObjectNode definition;

@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "metadata_id", referencedColumnName = "id")
private Metadata metadata;

public void setId(UUID id) {
this.id=id;
this.id = id;
this.metadata.setId(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.finos.vuu.layoutserver.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.RequiredArgsConstructor;
import org.finos.vuu.layoutserver.model.ApplicationLayout;
import org.finos.vuu.layoutserver.repository.ApplicationLayoutRepository;
Expand All @@ -20,7 +20,7 @@ public class ApplicationLayoutService {
private final ApplicationLayoutRepository repository;
private final DefaultApplicationLayoutLoader defaultLoader;

public void persistApplicationLayout(String username, JsonNode layoutDefinition) {
public void persistApplicationLayout(String username, ObjectNode layoutDefinition) {
repository.save(new ApplicationLayout(username, layoutDefinition));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.finos.vuu.layoutserver.utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.finos.vuu.layoutserver.exceptions.InternalServerErrorException;
import org.finos.vuu.layoutserver.model.ApplicationLayout;
import org.springframework.context.annotation.Bean;
Expand All @@ -24,15 +24,15 @@ public ApplicationLayout getDefaultLayout() {
}

private void loadDefaultLayout() {
JsonNode definition = loadDefaultLayoutJsonFile();
ObjectNode definition = loadDefaultLayoutJsonFile();
defaultLayout = new ApplicationLayout(null, definition);
}

private JsonNode loadDefaultLayoutJsonFile() {
private ObjectNode loadDefaultLayoutJsonFile() {
ObjectMapper objectMapper = new ObjectMapper();
ClassPathResource resource = new ClassPathResource(DEFAULT_LAYOUT_FILE);
try {
return objectMapper.readTree(resource.getInputStream());
return objectMapper.readValue(resource.getInputStream(), ObjectNode.class);
} catch (IOException e) {
throw new InternalServerErrorException("Failed to read default application layout");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package org.finos.vuu.layoutserver.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.AttributeConverter;
import java.io.IOException;

public class JsonNodeConverter implements AttributeConverter<JsonNode, String> {
private static final Logger logger = LoggerFactory.getLogger(JsonNodeConverter.class);
public class ObjectNodeConverter implements AttributeConverter<ObjectNode, String> {
private static final Logger logger = LoggerFactory.getLogger(ObjectNodeConverter.class);
private static final ObjectMapper objectMapper = new ObjectMapper();

@Override
public String convertToDatabaseColumn(JsonNode definition) {
public String convertToDatabaseColumn(ObjectNode definition) {
try {
return objectMapper.writeValueAsString(definition);
} catch (final JsonProcessingException e) {
Expand All @@ -25,9 +24,9 @@ public String convertToDatabaseColumn(JsonNode definition) {
}

@Override
public JsonNode convertToEntityAttribute(String definition) {
public ObjectNode convertToEntityAttribute(String definition) {
try {
return objectMapper.readValue(extractDefinition(definition), new TypeReference<>() {});
return objectMapper.readValue(extractDefinition(definition), ObjectNode.class);
} catch (final IOException e) {
logger.error("JSON reading error", e);
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.finos.vuu.layoutserver.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.finos.vuu.layoutserver.dto.response.ApplicationLayoutDto;
import org.finos.vuu.layoutserver.model.ApplicationLayout;
import org.finos.vuu.layoutserver.service.ApplicationLayoutService;
import org.finos.vuu.layoutserver.utils.ObjectNodeConverter;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
Expand All @@ -18,7 +18,7 @@ class ApplicationLayoutControllerTest {
private static ApplicationLayoutService mockService;
private static ApplicationLayoutController controller;
private static final ModelMapper modelMapper = new ModelMapper();
private static final ObjectMapper objectMapper = new ObjectMapper();
private static final ObjectNodeConverter objectNodeConverter = new ObjectNodeConverter();

@BeforeEach
public void setup() {
Expand All @@ -29,7 +29,7 @@ public void setup() {
@Test
public void getApplicationLayout_anyUsername_returnsLayoutFromService() throws JsonProcessingException {
String user = "user";
JsonNode definition = objectMapper.readTree("{\"id\":\"main-tabs\"}");
ObjectNode definition = objectNodeConverter.convertToEntityAttribute("{\"id\":\"main-tabs\"}");

when(mockService.getApplicationLayout(user))
.thenReturn(new ApplicationLayout(user, definition));
Expand All @@ -45,7 +45,7 @@ public void getApplicationLayout_anyUsername_returnsLayoutFromService() throws J
@Test
public void persistApplicationLayout_anyInput_callsService() throws JsonProcessingException {
String user = "user";
JsonNode definition = objectMapper.readTree("{\"id\":\"main-tabs\"}");
ObjectNode definition = objectNodeConverter.convertToEntityAttribute("{\"id\":\"main-tabs\"}");

controller.persistApplicationLayout(user, definition);

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

import com.fasterxml.jackson.core.JsonProcessingException;
import org.finos.vuu.layoutserver.dto.request.LayoutRequestDto;
import org.finos.vuu.layoutserver.dto.request.MetadataRequestDto;
import org.finos.vuu.layoutserver.dto.response.LayoutResponseDto;
Expand All @@ -9,6 +10,7 @@
import org.finos.vuu.layoutserver.model.Metadata;
import org.finos.vuu.layoutserver.service.LayoutService;
import org.finos.vuu.layoutserver.service.MetadataService;
import org.finos.vuu.layoutserver.utils.ObjectNodeConverter;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -29,13 +31,14 @@
@ExtendWith(MockitoExtension.class)
class LayoutControllerTest {

private static final String LAYOUT_DEFINITION = "Test Definition";
private static final String LAYOUT_DEFINITION_STRING = "{\"id\":\"main-tabs\"}";
private static final String LAYOUT_GROUP = "Test Group";
private static final String LAYOUT_NAME = "Test Layout";
private static final String LAYOUT_SCREENSHOT = "Test Screenshot";
private static final String LAYOUT_USER = "Test User";
private static final UUID VALID_ID = UUID.randomUUID();
private static final UUID DOES_NOT_EXIST_ID = UUID.randomUUID();
private static final ObjectNodeConverter objectNodeConverter = new ObjectNodeConverter();

@Mock
private LayoutService layoutService;
Expand All @@ -57,7 +60,7 @@ class LayoutControllerTest {
private MetadataResponseDto metadataResponse;

@BeforeEach
public void setup() {
public void setup() throws JsonProcessingException {
baseMetadata = new BaseMetadata();
baseMetadata.setName(LAYOUT_NAME);
baseMetadata.setUser(LAYOUT_USER);
Expand All @@ -69,7 +72,7 @@ public void setup() {
layout = new Layout();
layout.setMetadata(metadata);
layout.setId(VALID_ID);
layout.setDefinition(LAYOUT_DEFINITION);
layout.setDefinition(objectNodeConverter.convertToEntityAttribute(LAYOUT_DEFINITION_STRING));

layoutRequest = new LayoutRequestDto();
MetadataRequestDto metadataRequestDto = new MetadataRequestDto();
Expand Down Expand Up @@ -135,7 +138,6 @@ void createLayout_validLayout_returnsCreatedLayout() {
@Test
void updateLayout_validLayout_callsLayoutService() {
layout.setId(null);
// layout.getMetadata().setId(null);

when(modelMapper.map(layoutRequest, Layout.class)).thenReturn(layout);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.finos.vuu.layoutserver.integration;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.finos.vuu.layoutserver.exceptions.InternalServerErrorException;
import org.finos.vuu.layoutserver.model.ApplicationLayout;
import org.finos.vuu.layoutserver.repository.ApplicationLayoutRepository;
Expand Down Expand Up @@ -176,6 +176,6 @@ public void deleteApplicationLayout_noUserInHeader_returns400() throws Exception
}

private void persistApplicationLayout(String user, Map<String, String> definition) {
repository.save(new ApplicationLayout(user, objectMapper.convertValue(definition, JsonNode.class)));
repository.save(new ApplicationLayout(user, objectMapper.convertValue(definition, ObjectNode.class)));
}
}
Loading

0 comments on commit 7fdeaa7

Please sign in to comment.