Skip to content

Commit

Permalink
feat(specification-storage): implement endpoints to manage field defi…
Browse files Browse the repository at this point in the history
…nitions

Closes: MRSPECS-6
  • Loading branch information
psmagin committed Jun 4, 2024
1 parent b5fa610 commit 968941f
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 65 deletions.
1 change: 1 addition & 0 deletions .spectral.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ rules:
info-license: off
license-url: off
contact-properties: off
oas3-valid-media-example: off
docs-descriptions:
given:
- "#DescribableObjects"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.folio.rspec.domain.dto.SpecificationFieldDtoCollection;
import org.folio.rspec.domain.entity.Specification;
import org.folio.rspec.domain.repository.FieldRepository;
import org.folio.rspec.exception.IllegalSpecificationChangeException;
import org.folio.rspec.exception.ResourceNotFoundException;
import org.folio.rspec.service.mapper.SpecificationFieldMapper;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -40,9 +39,6 @@ public SpecificationFieldDto createLocalField(Specification specification, Speci
@Transactional
public void deleteField(UUID id) {
var fieldEntity = fieldRepository.findById(id).orElseThrow(() -> ResourceNotFoundException.forField(id));
if (fieldEntity.getScope() != Scope.CUSTOM) {
throw IllegalSpecificationChangeException.forDelete(fieldEntity.getScope());
}
fieldRepository.delete(fieldEntity);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package org.folio.api;

import static org.folio.support.ApiEndpoints.fieldPath;
import static org.folio.support.ApiEndpoints.specificationFieldsPath;
import static org.folio.support.TestConstants.BIBLIOGRAPHIC_SPECIFICATION_ID;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.jayway.jsonpath.JsonPath;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.UUID;
import org.folio.rspec.domain.dto.SpecificationFieldChangeDto;
import org.folio.rspec.exception.ResourceNotFoundException;
import org.folio.spring.testing.type.IntegrationTest;
import org.folio.support.IntegrationTestBase;
import org.folio.support.TestConstants;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

@IntegrationTest
class SpecificationStorageFieldsApiIT extends IntegrationTestBase {

@BeforeAll
static void beforeAll() {
setUpTenant();
}

@Test
void deleteField_shouldReturn204AndDeleteLocalField() throws Exception {
var createdFieldId = createLocalField(getLocalFieldDto());

doDelete(fieldPath(createdFieldId));

doGet(specificationFieldsPath(BIBLIOGRAPHIC_SPECIFICATION_ID))
.andExpect(jsonPath("$.fields.[*].id", not(hasItem(createdFieldId))));
}

@Test
void deleteField_shouldReturn404WhenFieldNotExist() throws Exception {
var notExistId = UUID.randomUUID();
tryDelete(fieldPath(notExistId))
.andExpect(status().isNotFound())
.andExpect(exceptionMatch(ResourceNotFoundException.class))
.andExpect(errorMessageMatch(is("field definition with ID [%s] was not found".formatted(notExistId))));
}

@Test
void updateField_shouldReturn201AndUpdateLocalField() throws Exception {
var localTestField = getLocalFieldDto();
var createdFieldId = createLocalField(localTestField);

localTestField.setDeprecated(true);
localTestField.setUrl("http://www.viverra.com");

doPut(fieldPath(createdFieldId), localTestField);

doGet(specificationFieldsPath(BIBLIOGRAPHIC_SPECIFICATION_ID))
.andExpect(jsonPath("$.fields.[*]", hasItem(Matchers.<Map<String, Object>>allOf(
hasEntry("id", createdFieldId),
hasEntry("deprecated", true),
hasEntry("url", "http://www.viverra.com")
))));
}

@Test
void updateField_shouldReturn404WhenFieldNotExist() throws Exception {
var notExistId = UUID.randomUUID();
tryPut(fieldPath(notExistId), getLocalFieldDto())
.andExpect(status().isNotFound())
.andExpect(exceptionMatch(ResourceNotFoundException.class))
.andExpect(errorMessageMatch(is("field definition with ID [%s] was not found".formatted(notExistId))));
}

private SpecificationFieldChangeDto getLocalFieldDto() {
return new SpecificationFieldChangeDto()
.tag("998")
.required(true)
.deprecated(false)
.required(false)
.label("Local Test Field");
}

private String createLocalField(SpecificationFieldChangeDto localTestField) throws UnsupportedEncodingException {
return JsonPath.read(
doPost(specificationFieldsPath(TestConstants.BIBLIOGRAPHIC_SPECIFICATION_ID), localTestField)
.andReturn()
.getResponse().getContentAsString(),
"$.id").toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
@UtilityClass
public class ApiEndpoints {

public static final String SPECIFICATIONS_PATH = "/specification-storage/specifications";
public static final String SPECIFICATION_STORAGE_PATH = "/specification-storage";
public static final String SPECIFICATIONS_PATH = SPECIFICATION_STORAGE_PATH + "/specifications";
public static final String SPECIFICATION_RULES_PATH = SPECIFICATIONS_PATH + "/%s/rules";
public static final String SPECIFICATION_RULE_PATH = SPECIFICATION_RULES_PATH + "/%s";
public static final String SPECIFICATION_FIELDS_PATH = SPECIFICATIONS_PATH + "/%s/fields";
public static final String FIELD_PATH = SPECIFICATION_STORAGE_PATH + "/fields/%s";

public static String specificationsPath() {
return SPECIFICATIONS_PATH;
Expand Down Expand Up @@ -44,6 +46,14 @@ public static String specificationFieldsPath(UUID specId) {
return specificationFieldsPath(specId.toString());
}

public static String fieldPath(String fieldId) {
return FIELD_PATH.formatted(fieldId);
}

public static String fieldPath(UUID fieldId) {
return fieldPath(fieldId.toString());
}

private static String addQueryParams(String path, QueryParams queryParams) {
if (queryParams.isEmpty()) {
return path;
Expand Down

0 comments on commit 968941f

Please sign in to comment.