Skip to content

Commit

Permalink
VUU-333: Add constraints to name, group, and user in BaseMetadata (fi…
Browse files Browse the repository at this point in the history
…nos#991)

* SLVUU-90: Add not blank constraints to name, group, and user in BaseMetadata

* SLVUU: Revert aggressive auto-formatting
  • Loading branch information
cfisher-scottlogic authored Nov 23, 2023
1 parent 3635d43 commit 3fbc182
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package org.finos.vuu.layoutserver.dto.request;

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

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import lombok.Data;

@Data
public class LayoutRequestDto {

/**
* The definition of the layout as an arbitrary JSON structure, describing all required components
* The definition of the layout as an arbitrary JSON structure, describing all required
* components
*/
@NotNull(message = "Definition must not be null")
private ObjectNode definition;

@NotNull(message = "Metadata must not be null")
@Valid
private MetadataRequestDto metadata;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.finos.vuu.layoutserver.dto.request;

import com.fasterxml.jackson.annotation.JsonUnwrapped;
import javax.validation.Valid;
import lombok.Data;
import org.finos.vuu.layoutserver.model.BaseMetadata;

@Data
public class MetadataRequestDto {

@JsonUnwrapped
@Valid
BaseMetadata baseMetadata;
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package org.finos.vuu.layoutserver.model;

import lombok.Data;

import javax.persistence.Embeddable;
import javax.persistence.Lob;
import javax.validation.constraints.NotBlank;
import lombok.Data;

@Data
@Embeddable
public class BaseMetadata {

@NotBlank(message = "Name is required")
private String name;

@NotBlank(message = "Group is required")
private String group;

@Lob
private String screenshot;

@NotBlank(message = "User is required")
private String user;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import org.finos.vuu.layoutserver.utils.ObjectNodeConverter;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
Expand Down Expand Up @@ -244,6 +247,62 @@ void createLayout_invalidRequestBodyMetadataIsNull_returns400AndDoesNotCreateLay
assertThat(metadataRepository.findAll()).isEmpty();
}

@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {" ", "\t", "\n"})
void createLayout_invalidRequestBodyMetadataNameIsInvalid_returns400AndDoesNotCreateLayout(
String name) throws Exception {
LayoutRequestDto layoutRequest = createValidLayoutRequest();
layoutRequest.getMetadata().getBaseMetadata().setName(name);

mockMvc.perform(post("/layouts")
.content(objectMapper.writeValueAsString(layoutRequest))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.messages", iterableWithSize(1)))
.andExpect(
jsonPath("$.messages", contains("metadata.baseMetadata.name: Name is required")));

assertThat(layoutRepository.findAll()).isEmpty();
assertThat(metadataRepository.findAll()).isEmpty();
}

@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {" ", "\t", "\n"})
void createLayout_invalidRequestBodyMetadataGroupIsInvalid_returns400AndDoesNotCreateLayout(
String group) throws Exception {
LayoutRequestDto layoutRequest = createValidLayoutRequest();
layoutRequest.getMetadata().getBaseMetadata().setGroup(group);

mockMvc.perform(post("/layouts")
.content(objectMapper.writeValueAsString(layoutRequest))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.messages", iterableWithSize(1)))
.andExpect(
jsonPath("$.messages", contains("metadata.baseMetadata.group: Group is required")));

assertThat(layoutRepository.findAll()).isEmpty();
assertThat(metadataRepository.findAll()).isEmpty();
}

@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {" ", "\t", "\n"})
void createLayout_invalidRequestBodyUserIsInvalid_returns400AndDoesNotCreateLayout(String user)
throws Exception {
LayoutRequestDto layoutRequest = createValidLayoutRequest();
layoutRequest.getMetadata().getBaseMetadata().setUser(user);

mockMvc.perform(post("/layouts")
.content(objectMapper.writeValueAsString(layoutRequest))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.messages", iterableWithSize(1)))
.andExpect(
jsonPath("$.messages", contains("metadata.baseMetadata.user: User is required")));
}

@Test
void createLayout_invalidRequestBodyUnexpectedFormat_returns400() throws Exception {
Expand Down Expand Up @@ -366,6 +425,72 @@ void updateLayout_invalidRequestBodyUnexpectedFormat_returns400AndLayoutDoesNotC
layout);
}

@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {" ", "\t", "\n"})
void updateLayout_invalidRequestBodyMetadataNameIsInvalid_returns400AndLayoutDoesNotChange(
String name) throws Exception {
Layout layout = createDefaultLayoutInDatabase();
assertThat(layoutRepository.findById(layout.getId()).orElseThrow()).isEqualTo(layout);

LayoutRequestDto request = createValidLayoutRequest();
request.getMetadata().getBaseMetadata().setName(name);

mockMvc.perform(put("/layouts/{id}", layout.getId())
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.messages", iterableWithSize(1)))
.andExpect(
jsonPath("$.messages", contains("metadata.baseMetadata.name: Name is required")));

assertThat(layoutRepository.findById(layout.getId()).orElseThrow()).isEqualTo(layout);
}

@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {" ", "\t", "\n"})
void updateLayout_invalidRequestBodyMetadataGroupIsInvalid_returns400AndDoesNotUpdateLayout(
String group) throws Exception {
Layout layout = createDefaultLayoutInDatabase();
assertThat(layoutRepository.findById(layout.getId()).orElseThrow()).isEqualTo(layout);

LayoutRequestDto request = createValidLayoutRequest();
request.getMetadata().getBaseMetadata().setGroup(group);

mockMvc.perform(put("/layouts/{id}", layout.getId())
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.messages", iterableWithSize(1)))
.andExpect(
jsonPath("$.messages", contains("metadata.baseMetadata.group: Group is required")));

assertThat(layoutRepository.findById(layout.getId()).orElseThrow()).isEqualTo(layout);
}

@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {" ", "\t", "\n"})
void updateLayout_invalidRequestBodyMetadataUserIsNullOrEmpty_returns400AndDoesNotUpdateLayout(
String user) throws Exception {
Layout layout = createDefaultLayoutInDatabase();
assertThat(layoutRepository.findById(layout.getId()).orElseThrow()).isEqualTo(layout);

LayoutRequestDto request = createValidLayoutRequest();
request.getMetadata().getBaseMetadata().setUser(user);

mockMvc.perform(put("/layouts/{id}", layout.getId())
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.messages", iterableWithSize(1)))
.andExpect(
jsonPath("$.messages", contains("metadata.baseMetadata.user: User is required")));

assertThat(layoutRepository.findById(layout.getId()).orElseThrow()).isEqualTo(layout);
}

@Test
void updateLayout_validIdButLayoutDoesNotExist_returnsNotFound() throws Exception {
UUID layoutID = UUID.randomUUID();
Expand Down

0 comments on commit 3fbc182

Please sign in to comment.