-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d882f9
commit 67ed1d7
Showing
4 changed files
with
166 additions
and
4 deletions.
There are no files selected for viewing
127 changes: 123 additions & 4 deletions
127
src/test/java/com/golshadi/majid/inputvalidator/InputValidatorApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,132 @@ | ||
package com.golshadi.majid.inputvalidator; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.github.fge.jackson.JsonLoader; | ||
import java.io.IOException; | ||
import java.time.Instant; | ||
import lombok.Data; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; | ||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||
import org.springframework.boot.web.server.LocalServerPort; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
|
||
@SpringBootTest | ||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) | ||
class InputValidatorApplicationTests { | ||
|
||
@Test | ||
void contextLoads() { | ||
} | ||
private final static String RULE_KEY_1 = "v1/test/key"; | ||
private final static String RULE_KEY_2 = "v2/test/key"; | ||
private final static String VALID_JSON_DATA_PATH = "/valid.json"; | ||
private final static String VALID_JSON_DATA_PATH_2 = "/valid-2.json"; | ||
private final static String SINGLE_INVALID_FIELD_JSON_DATA_PATH = "/single-invalid-field.json"; | ||
private final static String EXTRA_FIELD_JSON_DATA_PATH = "/extra-field-invalid.json"; | ||
private final static String NOT_EXISTS_REQUIRED_FIELD_JSON_DATA_PATH = "/not-exists-required-field.json"; | ||
private final static String COMBINATION_OF_ISSUES_JSON_DATA_PATH = "/combination-of-issues.json"; | ||
|
||
@LocalServerPort | ||
private int port; | ||
|
||
@Autowired | ||
private TestRestTemplate restTemplate; | ||
|
||
private String validationUrl; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
validationUrl = String.format("http://localhost:%d/validate", port); | ||
} | ||
|
||
@Test | ||
void badRequestRequiredFieldErrorTest() throws IOException { | ||
var jsonNode = JsonLoader.fromResource(NOT_EXISTS_REQUIRED_FIELD_JSON_DATA_PATH); | ||
var request = makeRequestEntity(RULE_KEY_1, jsonNode.toString()); | ||
var response = restTemplate.exchange(validationUrl, HttpMethod.POST, request, ErrorResponse.class); | ||
|
||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); | ||
assertThat(response.getBody().getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value()); | ||
assertThat(response.getBody().getError()).isEqualTo("Bad Request"); | ||
assertThat(response.getBody().getMessage()).isEqualTo("required fields latitude does not exists"); | ||
} | ||
|
||
@Test | ||
void badRequestInvalidFieldErrorTest() throws IOException { | ||
var jsonNode = JsonLoader.fromResource(SINGLE_INVALID_FIELD_JSON_DATA_PATH); | ||
var request = makeRequestEntity(RULE_KEY_1, jsonNode.toString()); | ||
var response = restTemplate.exchange(validationUrl, HttpMethod.POST, request, ErrorResponse.class); | ||
|
||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); | ||
assertThat(response.getBody().getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value()); | ||
assertThat(response.getBody().getError()).isEqualTo("Bad Request"); | ||
assertThat(response.getBody().getMessage()).isEqualTo("invalid fields: .latitude"); | ||
} | ||
|
||
@Test | ||
void badRequestExtraFieldErrorTest() throws IOException { | ||
var jsonNode = JsonLoader.fromResource(EXTRA_FIELD_JSON_DATA_PATH); | ||
var request = makeRequestEntity("v1/test/key", jsonNode.toString()); | ||
var response = restTemplate.exchange(validationUrl, HttpMethod.POST, request, ErrorResponse.class); | ||
|
||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); | ||
assertThat(response.getBody().getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value()); | ||
assertThat(response.getBody().getError()).isEqualTo("Bad Request"); | ||
assertThat(response.getBody().getMessage()).isEqualTo("extra fields exist: extra_field"); | ||
} | ||
|
||
@Test | ||
void badRequestCombinationIssueErrorTest() throws IOException { | ||
var jsonNode = JsonLoader.fromResource(COMBINATION_OF_ISSUES_JSON_DATA_PATH); | ||
var request = makeRequestEntity(RULE_KEY_1, jsonNode.toString()); | ||
var response = restTemplate.exchange(validationUrl, HttpMethod.POST, request, ErrorResponse.class); | ||
|
||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); | ||
assertThat(response.getBody().getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value()); | ||
assertThat(response.getBody().getError()).isEqualTo("Bad Request"); | ||
assertThat(response.getBody().getMessage()).isEqualTo("extra fields exist: extra"); | ||
} | ||
|
||
@Test | ||
void validDataTest() throws IOException { | ||
var jsonNode = JsonLoader.fromResource(VALID_JSON_DATA_PATH); | ||
var request = makeRequestEntity(RULE_KEY_1, jsonNode.toString()); | ||
var response = restTemplate.exchange(validationUrl, HttpMethod.POST, request, String.class); | ||
|
||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); | ||
} | ||
|
||
@Test | ||
void supportMultipleSchemaJsonConfigTest() throws IOException { | ||
var jsonNode = JsonLoader.fromResource(VALID_JSON_DATA_PATH_2); | ||
var request = makeRequestEntity(RULE_KEY_2, jsonNode.toString()); | ||
var response = restTemplate.exchange(validationUrl, HttpMethod.POST, request, String.class); | ||
|
||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); | ||
} | ||
|
||
private HttpEntity<String> makeRequestEntity(String header, String body) { | ||
var headers = new HttpHeaders(); | ||
|
||
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); | ||
headers.add("X-B3-URL", header); | ||
|
||
return new HttpEntity<>(body, headers); | ||
} | ||
|
||
} | ||
|
||
@Data | ||
class ErrorResponse { | ||
|
||
private final String timestamp; | ||
private final int status; | ||
private final String error; | ||
private final String message; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
header-keys: | ||
rule-key: "X-B3-URL" | ||
|
||
request-validation: | ||
rules: | ||
- description: "description 1" | ||
key: v1/test/key | ||
json-schema: /json-schema.json | ||
- description: "description 1" | ||
key: v2/test/key | ||
json-schema: /json-schema-2.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"title": "product", | ||
"required": [ "productName", "price", "tags"], | ||
"additionalProperties": false, | ||
"type": "object", | ||
"properties": { | ||
"productName": { | ||
"description": "Name of the product", | ||
"type": "string" | ||
}, | ||
"price": { | ||
"description": "The price of the product", | ||
"type": "number", | ||
"minimum": 10 | ||
}, | ||
"tags": { | ||
"description": "Tags for the product", | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
}, | ||
"minItems": 1, | ||
"uniqueItems": true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"productName": "pr1", | ||
"price": 100, | ||
"tags": ["t1", "t2"] | ||
} |