diff --git a/schema-core/src/main/java/org/opengroup/osdu/schema/constants/SchemaConstants.java b/schema-core/src/main/java/org/opengroup/osdu/schema/constants/SchemaConstants.java index eaaac41090fcec0a878fb23ccf863401d35894a3..c4622126a96c76b3f4680627dd4b3ede5237aa91 100644 --- a/schema-core/src/main/java/org/opengroup/osdu/schema/constants/SchemaConstants.java +++ b/schema-core/src/main/java/org/opengroup/osdu/schema/constants/SchemaConstants.java @@ -21,6 +21,7 @@ public class SchemaConstants { public static final String SCHEMA_KIND_DELIMITER = ":"; public static final String SCHEMA_Version_DELIMITER = "."; public static final String SCHEMA_KIND_REGEX="^([^.:\\r\\n\\s]+):([^.:\\r\\n\\s]+):([^.:\\r\\n\\s]+):(([\\d]+)[.]([\\d]+)[.]([\\d]+))$"; + public static final String SCHEMA_EMPTY_REGEX = "^[\\w\\-\\.]+$"; // schema public static final String SCHEMA_KIND = "schema"; diff --git a/schema-core/src/main/java/org/opengroup/osdu/schema/model/SchemaIdentity.java b/schema-core/src/main/java/org/opengroup/osdu/schema/model/SchemaIdentity.java index ada15fa5fe4a84a5c5de2f5b2bc5d5915116561f..5809f68b71d070800a2f729ba174e14dadae7630 100644 --- a/schema-core/src/main/java/org/opengroup/osdu/schema/model/SchemaIdentity.java +++ b/schema-core/src/main/java/org/opengroup/osdu/schema/model/SchemaIdentity.java @@ -1,11 +1,13 @@ package org.opengroup.osdu.schema.model; import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import org.opengroup.osdu.schema.constants.SchemaConstants; @Data @Builder @@ -14,12 +16,15 @@ import lombok.NoArgsConstructor; public class SchemaIdentity { @NotNull(message = "authority must not be null") + @Pattern(regexp = SchemaConstants.SCHEMA_EMPTY_REGEX, message = "authority must not contain whitespaces and special characters except - and .") private String authority; @NotNull(message = "source must not be null") + @Pattern(regexp = SchemaConstants.SCHEMA_EMPTY_REGEX, message = "source must not contain whitespaces and special characters except - and .") private String source; @NotNull(message = "entityType must not be null") + @Pattern(regexp = SchemaConstants.SCHEMA_EMPTY_REGEX, message = "entityType must not contain whitespaces and special characters except - and .") private String entityType; @NotNull(message = "schemaVersionMajor must not be null") diff --git a/schema-core/src/test/java/org/opengroup/osdu/schema/api/SchemaControllerTest.java b/schema-core/src/test/java/org/opengroup/osdu/schema/api/SchemaControllerTest.java index f294e84b1566bcd5db68f43df8385c556fc3af58..8b2eff8a3ad1d1745588e3c209f55571516a3938 100644 --- a/schema-core/src/test/java/org/opengroup/osdu/schema/api/SchemaControllerTest.java +++ b/schema-core/src/test/java/org/opengroup/osdu/schema/api/SchemaControllerTest.java @@ -79,6 +79,62 @@ public class SchemaControllerTest { schemaController.upsertSchema(schemaRequest); } + @Test(expected = BadRequestException.class) + public void testCreateEmptyAuthoritySchema_Failed() throws ApplicationException, BadRequestException { + schemaRequest = getSchemaRequestEmptyAuthority(); + + when(schemaService.createSchema(schemaRequest)).thenThrow(BadRequestException.class); + assertNotNull(schemaController.createSchema(schemaRequest)); + } + + @Test(expected = BadRequestException.class) + public void testCreateEmptyEntitySchema_Failed() throws ApplicationException, BadRequestException { + schemaRequest = getSchemaRequestEmptySource(); + + when(schemaService.createSchema(schemaRequest)).thenThrow(BadRequestException.class); + assertNotNull(schemaController.createSchema(schemaRequest)); + } + + @Test(expected = BadRequestException.class) + public void testCreateEmptySourceSchema_Failed() throws ApplicationException, BadRequestException { + schemaRequest = getSchemaRequestEmptyEntity(); + + when(schemaService.createSchema(schemaRequest)).thenThrow(BadRequestException.class); + assertNotNull(schemaController.createSchema(schemaRequest)); + } + + @Test(expected = BadRequestException.class) + public void testCreateAuthoritySchemaSpecialCharacters_Failed() throws ApplicationException, BadRequestException { + schemaRequest = getSchemaRequestSpecialCharactersAuthority(); + + when(schemaService.createSchema(schemaRequest)).thenThrow(BadRequestException.class); + assertNotNull(schemaController.createSchema(schemaRequest)); + } + + @Test(expected = BadRequestException.class) + public void testCreateSourceSchemaSpecialCharacters_Failed() throws ApplicationException, BadRequestException { + schemaRequest = getSchemaRequestSpecialCharactersSource(); + + when(schemaService.createSchema(schemaRequest)).thenThrow(BadRequestException.class); + assertNotNull(schemaController.createSchema(schemaRequest)); + } + + @Test(expected = BadRequestException.class) + public void testCreateEntitySchemaSpecialCharacters_Failed() throws ApplicationException, BadRequestException { + schemaRequest = getSchemaRequestSpecialCharactersEntity(); + + when(schemaService.createSchema(schemaRequest)).thenThrow(BadRequestException.class); + assertNotNull(schemaController.createSchema(schemaRequest)); + } + + @Test(expected = BadRequestException.class) + public void testCreateAuthorityMultipleSpaces_Failed() throws ApplicationException, BadRequestException { + schemaRequest = getSchemaRequestMultipleSpacesAuthority(); + + when(schemaService.createSchema(schemaRequest)).thenThrow(BadRequestException.class); + assertNotNull(schemaController.createSchema(schemaRequest)); + } + @Test public void testGetSchemaInfoList() throws ApplicationException, NotFoundException, BadRequestException { schemaRequest = getSchemaRequestObject(); @@ -121,4 +177,81 @@ public class SchemaControllerTest { .schemaVersionMajor(1L).schemaVersionMinor(1L).source("wks").build()) .build(); } + + private SchemaRequest getSchemaRequestEmptyAuthority() { + return SchemaRequest.builder().schema(null).schemaInfo(SchemaInfo.builder().createdBy("creator") + .dateCreated(new Date(System.currentTimeMillis())) + .schemaIdentity(SchemaIdentity.builder().authority("").entityType("well").id("..wks.well.1.1") + .schemaVersionMajor(1L).schemaVersionMinor(1L).source("wks").build()) + .scope(SchemaScope.INTERNAL).status(SchemaStatus.DEVELOPMENT) + .supersededBy(SchemaIdentity.builder().authority("").entityType("well").id("..wks.well.1.4") + .schemaVersionMajor(1L).schemaVersionMinor(1L).source("wks").build()) + .build()).build(); + } + + private SchemaRequest getSchemaRequestEmptyEntity() { + return SchemaRequest.builder().schema(null).schemaInfo(SchemaInfo.builder().createdBy("creator") + .dateCreated(new Date(System.currentTimeMillis())) + .schemaIdentity(SchemaIdentity.builder().authority("os").entityType("").id("os..well.1.1") + .schemaVersionMajor(1L).schemaVersionMinor(1L).source("well").build()) + .scope(SchemaScope.INTERNAL).status(SchemaStatus.DEVELOPMENT) + .supersededBy(SchemaIdentity.builder().authority("os").entityType("").id("os..well.1.4") + .schemaVersionMajor(1L).schemaVersionMinor(1L).source("well").build()) + .build()).build(); + } + + private SchemaRequest getSchemaRequestEmptySource() { + return SchemaRequest.builder().schema(null).schemaInfo(SchemaInfo.builder().createdBy("creator") + .dateCreated(new Date(System.currentTimeMillis())) + .schemaIdentity(SchemaIdentity.builder().authority("os").entityType("well").id("os.well.1.1") + .schemaVersionMajor(1L).schemaVersionMinor(1L).source("").build()) + .scope(SchemaScope.INTERNAL).status(SchemaStatus.DEVELOPMENT) + .supersededBy(SchemaIdentity.builder().authority("os").entityType("well").id("os.well.1.4") + .schemaVersionMajor(1L).schemaVersionMinor(1L).source("").build()) + .build()).build(); + } + + private SchemaRequest getSchemaRequestSpecialCharactersAuthority() { + return SchemaRequest.builder().schema(null).schemaInfo(SchemaInfo.builder().createdBy("creator") + .dateCreated(new Date(System.currentTimeMillis())) + .schemaIdentity(SchemaIdentity.builder().authority(" :").entityType("well").id(" :.well.1.1") + .schemaVersionMajor(1L).schemaVersionMinor(1L).source("").build()) + .scope(SchemaScope.INTERNAL).status(SchemaStatus.DEVELOPMENT) + .supersededBy(SchemaIdentity.builder().authority(" :").entityType("well").id(" :.well.1.4") + .schemaVersionMajor(1L).schemaVersionMinor(1L).source("").build()) + .build()).build(); + } + + private SchemaRequest getSchemaRequestSpecialCharactersSource() { + return SchemaRequest.builder().schema(null).schemaInfo(SchemaInfo.builder().createdBy("creator") + .dateCreated(new Date(System.currentTimeMillis())) + .schemaIdentity(SchemaIdentity.builder().authority("os").entityType("well").id("os. :.1.1") + .schemaVersionMajor(1L).schemaVersionMinor(1L).source(" :").build()) + .scope(SchemaScope.INTERNAL).status(SchemaStatus.DEVELOPMENT) + .supersededBy(SchemaIdentity.builder().authority("os").entityType("well").id("os. :.1.4") + .schemaVersionMajor(1L).schemaVersionMinor(1L).source(" :").build()) + .build()).build(); + } + + private SchemaRequest getSchemaRequestSpecialCharactersEntity() { + return SchemaRequest.builder().schema(null).schemaInfo(SchemaInfo.builder().createdBy("creator") + .dateCreated(new Date(System.currentTimeMillis())) + .schemaIdentity(SchemaIdentity.builder().authority("os").entityType(" :").id("os.source. :.1.1") + .schemaVersionMajor(1L).schemaVersionMinor(1L).source("source").build()) + .scope(SchemaScope.INTERNAL).status(SchemaStatus.DEVELOPMENT) + .supersededBy(SchemaIdentity.builder().authority("os").entityType(" :").id("os.source. :.1.4") + .schemaVersionMajor(1L).schemaVersionMinor(1L).source("source").build()) + .build()).build(); + } + + private SchemaRequest getSchemaRequestMultipleSpacesAuthority() { + return SchemaRequest.builder().schema(null).schemaInfo(SchemaInfo.builder().createdBy("creator") + .dateCreated(new Date(System.currentTimeMillis())) + .schemaIdentity(SchemaIdentity.builder().authority(" ").entityType("well").id(" .source.well.1.1") + .schemaVersionMajor(1L).schemaVersionMinor(1L).source("source").build()) + .scope(SchemaScope.INTERNAL).status(SchemaStatus.DEVELOPMENT) + .supersededBy(SchemaIdentity.builder().authority(" ").entityType("well").id(" .source.well.1.4") + .schemaVersionMajor(1L).schemaVersionMinor(1L).source("source").build()) + .build()).build(); + } } \ No newline at end of file diff --git a/testing/schema-test-core/src/test/resources/features/IntegrationTest_SchemaService_POST_SchemaValidations.feature b/testing/schema-test-core/src/test/resources/features/IntegrationTest_SchemaService_POST_SchemaValidations.feature index 1c68fc754b367a4ef47616a68253e4302480f15c..8307a08d0451fb0bc5d8b9fb38c0645b10a23b74 100644 --- a/testing/schema-test-core/src/test/resources/features/IntegrationTest_SchemaService_POST_SchemaValidations.feature +++ b/testing/schema-test-core/src/test/resources/features/IntegrationTest_SchemaService_POST_SchemaValidations.feature @@ -584,3 +584,15 @@ Feature: To verify schema validation functionality of POST schema Service Examples: | RefBaseInputPayload | InputPayloadWithChanges | tenant | ReponseStatusCode | ResponseMessage1 | ReponseStatusCode1 | ResponseMessage | | "/input_payloads/RefBaseSchema_New.json" | "/input_payloads/RefBaseSchema_EntityNameChanged_MinorVersion.json" | "TENANT1" | "400" | "/output_payloads/SchemaPost_PrivateScope_SuccessfulCreation.json" | "201" | "/output_payloads/SchemaPost_MinorBreakingChangeError.json" | + + @SchemaService + Scenario Outline: Verify that Schema Service's POST API responds as bad request for empty value of authority, source and entity + When I hit schema service POST API with and data-partition-id as + Then service should respond back with error and + + Examples: + | InputPayload | ReponseStatusCode | ResponseMessage | tenant | + | "/input_payloads/inputPayloadWithEmptyAuthority.json" | "400" | "/output_payloads/EmptyAuthorityPayload.json" | "TENANT1" | + | "/input_payloads/inputPayloadWithEmptyEntity.json" | "400" | "/output_payloads/EmptyEntityPayload.json" | "TENANT1" | + | "/input_payloads/inputPayloadWithEmptySource.json" | "400" | "/output_payloads/EmptySourcePayload.json" | "TENANT1" | + diff --git a/testing/schema-test-core/src/test/resources/input_payloads/inputPayloadWithEmptyAuthority.json b/testing/schema-test-core/src/test/resources/input_payloads/inputPayloadWithEmptyAuthority.json new file mode 100644 index 0000000000000000000000000000000000000000..26d86843dfcd47b09f6b85d8151ac61ca91a40ae --- /dev/null +++ b/testing/schema-test-core/src/test/resources/input_payloads/inputPayloadWithEmptyAuthority.json @@ -0,0 +1,18 @@ +{ + "schemaInfo": { + "schemaIdentity": { + "authority": "", + "source": "testSource", + "entityType": "testEntity", + "schemaVersionMajor": 1, + "schemaVersionMinor": 1, + "schemaVersionPatch": 0, + "id": ":testSource:testEntity:1.1.0" + }, + "status": "DEVELOPMENT" + }, + "schema": { + "$schema": "http://json-schema.org/draft-07/schema#", + "description": "The entity well." + } +} \ No newline at end of file diff --git a/testing/schema-test-core/src/test/resources/input_payloads/inputPayloadWithEmptyEntity.json b/testing/schema-test-core/src/test/resources/input_payloads/inputPayloadWithEmptyEntity.json new file mode 100644 index 0000000000000000000000000000000000000000..f6818a38f21246b166243fd539c2a39c638f0de8 --- /dev/null +++ b/testing/schema-test-core/src/test/resources/input_payloads/inputPayloadWithEmptyEntity.json @@ -0,0 +1,18 @@ +{ + "schemaInfo": { + "schemaIdentity": { + "authority": "testAuthority2", + "source": "testSource2", + "entityType": "", + "schemaVersionMajor": 1, + "schemaVersionMinor": 1, + "schemaVersionPatch": 0, + "id": "testAuthority2:testSource2::1.1.0" + }, + "status": "DEVELOPMENT" + }, + "schema": { + "$schema": "http://json-schema.org/draft-07/schema#", + "description": "The entity well." + } +} \ No newline at end of file diff --git a/testing/schema-test-core/src/test/resources/input_payloads/inputPayloadWithEmptySource.json b/testing/schema-test-core/src/test/resources/input_payloads/inputPayloadWithEmptySource.json new file mode 100644 index 0000000000000000000000000000000000000000..39c25196e09763dad4fc9e4f1395d120c9df6a8c --- /dev/null +++ b/testing/schema-test-core/src/test/resources/input_payloads/inputPayloadWithEmptySource.json @@ -0,0 +1,18 @@ +{ + "schemaInfo": { + "schemaIdentity": { + "authority": "testAuthority2", + "source": "", + "entityType": "testEntity", + "schemaVersionMajor": 1, + "schemaVersionMinor": 1, + "schemaVersionPatch": 0, + "id": "testAuthority2::testEntity:1.1.0" + }, + "status": "DEVELOPMENT" + }, + "schema": { + "$schema": "http://json-schema.org/draft-07/schema#", + "description": "The entity well." + } +} \ No newline at end of file diff --git a/testing/schema-test-core/src/test/resources/output_payloads/EmptyAuthorityPayload.json b/testing/schema-test-core/src/test/resources/output_payloads/EmptyAuthorityPayload.json new file mode 100644 index 0000000000000000000000000000000000000000..edf582f0a498a3a1850da775a4afa8e6fa973c26 --- /dev/null +++ b/testing/schema-test-core/src/test/resources/output_payloads/EmptyAuthorityPayload.json @@ -0,0 +1,13 @@ +{ + "error": { + "code": 400, + "message": "Validation Error", + "errors": [ + { + "domain": "global", + "reason": "badRequest", + "message": "authority must not contain whitespaces and special characters except - and ." + } + ] + } +} \ No newline at end of file diff --git a/testing/schema-test-core/src/test/resources/output_payloads/EmptyEntityPayload.json b/testing/schema-test-core/src/test/resources/output_payloads/EmptyEntityPayload.json new file mode 100644 index 0000000000000000000000000000000000000000..db799f3c0c923aca592d3aa1625c10903709300a --- /dev/null +++ b/testing/schema-test-core/src/test/resources/output_payloads/EmptyEntityPayload.json @@ -0,0 +1,13 @@ +{ + "error": { + "code": 400, + "message": "Validation Error", + "errors": [ + { + "domain": "global", + "reason": "badRequest", + "message": "entityType must not contain whitespaces and special characters except - and ." + } + ] + } +} \ No newline at end of file diff --git a/testing/schema-test-core/src/test/resources/output_payloads/EmptySourcePayload.json b/testing/schema-test-core/src/test/resources/output_payloads/EmptySourcePayload.json new file mode 100644 index 0000000000000000000000000000000000000000..87f40ade1427e95b068b8e6d78ec11bc017dae8c --- /dev/null +++ b/testing/schema-test-core/src/test/resources/output_payloads/EmptySourcePayload.json @@ -0,0 +1,13 @@ +{ + "error": { + "code": 400, + "message": "Validation Error", + "errors": [ + { + "domain": "global", + "reason": "badRequest", + "message": "source must not contain whitespaces and special characters except - and ." + } + ] + } +} \ No newline at end of file