package org.onap.dcaegen2.services.sdk.services.external.schema.manager.service;
+import org.onap.dcaegen2.services.sdk.services.external.schema.manager.exception.NoLocalReferenceException;
import org.onap.dcaegen2.services.sdk.services.external.schema.manager.model.SchemaReference;
import java.io.File;
return urlMapper;
}
- SchemaReference mapToLocalSchema(SchemaReference schemaReference) {
+ SchemaReference mapToLocalSchema(SchemaReference schemaReference) throws NoLocalReferenceException {
String publicUrl = schemaReference.getUrl();
String localUrl = urlMapper.mapToLocalUrl(publicUrl);
return createLocalSchemaReference(localUrl, schemaReference.getInternalReference());
* @param event as JsonNode
* @return validation result
* @throws IncorrectInternalFileReferenceException when reference to part of openApi yaml file with schemas is incorrect.
- * @throws NoLocalReferenceException when mapping for public url is not present in schema mapping file.
+ * @throws NoLocalReferenceException when mapping for public url is not present in schema mapping file.
*/
public boolean validate(JsonNode event) throws IncorrectInternalFileReferenceException, NoLocalReferenceException {
boolean validationResult = false;
validationResult = true;
} catch (ValidationException ex) {
logger.error(String.valueOf(ex.results()));
- } catch (IOException ex){
+ } catch (IOException ex) {
logger.error("Schema reference has invalid characters", ex);
}
return validationResult;
return Map.copyOf(mappingsCache);
}
- String mapToLocalUrl(String publicUrl) {
+ String mapToLocalUrl(String publicUrl) throws NoLocalReferenceException {
String externalUrl = mappingsCache.get(publicUrl);
if (externalUrl == null) {
throw new NoLocalReferenceException("Couldn't find mapping for public url. PublicURL: " + publicUrl);
package org.onap.dcaegen2.services.sdk.services.external.schema.manager.service;
import com.fasterxml.jackson.databind.JsonNode;
+import org.onap.dcaegen2.services.sdk.services.external.schema.manager.exception.NoLocalReferenceException;
import org.onap.dcaegen2.services.sdk.services.external.schema.manager.model.SchemaReference;
import org.onap.dcaegen2.services.sdk.services.external.schema.manager.exception.IncorrectInternalFileReferenceException;
import org.openapi4j.core.exception.ResolutionException;
return schemaReferenceMapper;
}
- synchronized SchemaValidator resolveValidator(JsonNode event, String schemaRefPath) throws IOException {
+ synchronized SchemaValidator resolveValidator(JsonNode event, String schemaRefPath) throws IOException, IncorrectInternalFileReferenceException, NoLocalReferenceException {
SchemaReference schemaReference = resolveSchemaReference(event, schemaRefPath);
schemaReference = schemaReferenceMapper.mapToLocalSchema(schemaReference);
SchemaValidator validator = cache.get(schemaReference.getUrl());
return validator != null;
}
- private SchemaValidator createNewValidator(SchemaReference schemaReference) throws IOException {
+ private SchemaValidator createNewValidator(SchemaReference schemaReference) throws IOException, IncorrectInternalFileReferenceException {
logger.info("Creating new stndDefined schema validator");
JsonNode schemaRefNode = SchemaReferenceJsonGenerator.getSchemaReferenceJson(schemaReference);
SchemaValidator schemaValidator = handleValidatorCreation(schemaRefNode);
return schemaValidator;
}
- private SchemaValidator handleValidatorCreation(JsonNode schemaNode) {
+ private SchemaValidator handleValidatorCreation(JsonNode schemaNode) throws IncorrectInternalFileReferenceException{
try {
return new SchemaValidator("StndDefinedSchemaValidator", schemaNode);
} catch (ResolutionException e) {
public static final String TEST_RESOURCES = "src/main/test/resources/";
@Test
- public void shouldReturnEmptyStringWhenFileNotFound() {
+ void shouldReturnEmptyStringWhenFileNotFound() {
//given
String expectedContent = "";
String fileName = "dummyFileName";
}
@Test
- public void shouldReturnFileContentWhenFileExists() {
+ void shouldReturnFileContentWhenFileExists() {
//given
String expectedContent = "{\n" +
" \"someObject\": \"dummyValue\"\n" +
}
@Test
- public void shouldReturnFalseWhenFileDoesNotExist() {
+ void shouldReturnFalseWhenFileDoesNotExist() {
//when
boolean doesFileExists = new FileReader("dummyFileName").doesFileExists();
}
@Test
- public void shouldReturnTrueWhenFileExists() {
+ void shouldReturnTrueWhenFileExists() {
//when
boolean doesFileExists = new FileReader(TEST_RESOURCES + "file_with_one_line.json").doesFileExists();
private ObjectMapper objectMapper = new ObjectMapper();
@Test
- public void shouldReturnJsonFragmentAtValidPath() throws IOException {
+ void shouldReturnJsonFragmentAtValidPath() throws IOException {
//given
JsonNode jsonContent = objectMapper.readTree(VALID_JSON_CONTENT);
JsonNode expectedJsonNode = objectMapper.readTree("true");
}
@Test
- public void shouldThrowErrorWhenPathDoesNotExistInJsonContent() throws IOException {
+ void shouldThrowErrorWhenPathDoesNotExistInJsonContent() throws IOException {
//given
JsonNode jsonContent = objectMapper.readTree(VALID_JSON_CONTENT);
String dummyPath = "dummyPath";
class SchemaReferenceJsonGeneratorTest {
@Test
- public void shouldReturnValidSchemaReferenceWhenUrlIsInValidFormat() throws IOException {
+ void shouldReturnValidSchemaReferenceWhenUrlIsInValidFormat() throws IOException {
//given
String validUrl = "src/main/test/resources/file_with_one_line.json#/elo";
String schemaReferenceContent = "{\"$ref\":\"" + validUrl + "\"}";
}
@Test
- public void shouldThrowErrorWhenUrlIsInInvalidFormat() {
+ void shouldThrowErrorWhenUrlIsInInvalidFormat() {
//given
String invalidFormatUrl = "\"someDummyValue\n\t";
SchemaReferenceResolver schemaReferenceResolver = new SchemaReferenceResolver(invalidFormatUrl);
public static final String SCHEMAS_PATH = "src/main/test/resources";
@Test
- public void shouldReturnProperReferenceWhenSchemaReferenceHasNoHash() {
+ void shouldReturnProperReferenceWhenSchemaReferenceHasNoHash() {
//given
SchemaReferenceMapper schemaReferenceMapper = getSchemaReferenceMapper();
String publicUrlWithoutHash = "http://someExternalUrl/external";
}
@Test
- public void shouldReturnProperReferenceWhenSchemaReferenceContainsHash() {
+ void shouldReturnProperReferenceWhenSchemaReferenceContainsHash() {
//given
SchemaReferenceMapper schemaReferenceMapper = getSchemaReferenceMapper();
String publicUrlWithHash = "http://someExternalUrl/external#someString";
import static org.assertj.core.api.Assertions.assertThat;
-public class StndDefinedValidatorBuilderTest {
+class StndDefinedValidatorBuilderTest {
private static final String TEST_RESOURCES = "src/main/test/resources/externalRepo/";
Map<String, String> mappingsCache = getMappingsCache(validator);
//then
- assertThat(mappingsCache.size()).isEqualTo(6);
+ assertThat(mappingsCache).hasSize(6);
}
@Test
Map<String, String> mappingsCache = getMappingsCache(validator);
//then
- assertThat(mappingsCache.size()).isEqualTo(5);
+ assertThat(mappingsCache).hasSize(5);
}
@Test
Map<String, String> mappingsCache = getMappingsCache(validator);
//then
- assertThat(mappingsCache.size()).isEqualTo(4);
+ assertThat(mappingsCache).hasSize(4);
}
@Test
Map<String, String> mappingsCache = getMappingsCache(validator);
//then
- assertThat(mappingsCache.size()).isEqualTo(3);
+ assertThat(mappingsCache).hasSize(3);
}
@Test
Map<String, String> mappingsCache = getMappingsCache(validator);
//then
- assertThat(mappingsCache.size()).isEqualTo(0);
+ assertThat(mappingsCache.size()).isZero();
}
@Test
Map<String, String> mappingsCache = getMappingsCache(validator);
//then
- assertThat(mappingsCache.size()).isEqualTo(0);
+ assertThat(mappingsCache.size()).isZero();
}
private StndDefinedValidator getValidator(String mappingFilePath) {
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
-public class StndDefinedValidatorTest {
+class StndDefinedValidatorTest {
private static final String TEST_RESOURCES = "src/main/test/resources/";
private static final String MAPPING_FILE_PATH = TEST_RESOURCES + "externalRepo/schema-map.json";
}
@Test
- public void shouldThrowExceptionWhenNoMappingExists() {
+ void shouldThrowExceptionWhenNoMappingExists() {
//given
String notMappedPublicUrl = "http://localhost:8080/notExisting";
}
@Test
- public void shouldThrowExceptionWhenLocalSchemaFileIsEmpty() {
+ void shouldThrowExceptionWhenLocalSchemaFileIsEmpty() {
//given
String publicUrlToEmptyLocal = "http://someExternalUrl/emptySchema";
}
@Test
- public void shouldThrowExceptionWhenFileHasInvalidYamlStructure() {
+ void shouldThrowExceptionWhenFileHasInvalidYamlStructure() {
//given
String publicUrlToInvalidYamlLocal = "http://someExternalUrl/invalidYamlFile";
}
@Test
- public void shouldThrowExceptionWhenLocalFileDoesNotExist() {
+ void shouldThrowExceptionWhenLocalFileDoesNotExist() {
//given
String publicUrlToNotExistingLocalFile = "http://someExternalUrl/missingFile";
}
@Test
- public void shouldReturnLocalUrlWhenFileValidAndFound() {
+ void shouldReturnLocalUrlWhenFileValidAndFound() {
//given
String publicUrl = "http://someExternalUrl/external";
}
@Test
- public void shouldNotThrowExceptionWhenMappingFileDoesNotExist() {
+ void shouldNotThrowExceptionWhenMappingFileDoesNotExist() {
String invalidMappingFilePath = "src/main/test/resources/missing-schema.json";
Assertions.assertDoesNotThrow(() -> new UrlMapperFactory().getUrlMapper(invalidMappingFilePath, SCHEMAS_PATH));