This repository has been archived by the owner on Aug 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
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
Showing
50 changed files
with
1,624 additions
and
609 deletions.
There are no files selected for viewing
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,47 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>adapters</artifactId> | ||
<groupId>uk.gov.justice.services</groupId> | ||
<version>0.1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>adapters-commons</artifactId> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.raml</groupId> | ||
<artifactId>raml-parser</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>uk.gov.justice.services</groupId> | ||
<artifactId>core</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>uk.gov.justice.services</groupId> | ||
<artifactId>adapters-test-utils</artifactId> | ||
<version>${project.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest-library</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
|
||
</project> |
56 changes: 56 additions & 0 deletions
56
.../src/main/java/uk/gov/justice/raml/common/validator/AbstractContentTypeRamlValidator.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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package uk.gov.justice.raml.common.validator; | ||
|
||
import org.raml.model.Action; | ||
import org.raml.model.ActionType; | ||
import org.raml.model.MimeType; | ||
import org.raml.model.Resource; | ||
|
||
import java.util.Collection; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.lang.String.format; | ||
import static java.util.Arrays.stream; | ||
import static org.apache.commons.lang.StringUtils.capitalize; | ||
|
||
public abstract class AbstractContentTypeRamlValidator extends AbstractResourceRamlValidator { | ||
private final Pattern mediaTypePattern; | ||
private final ActionType actionType; | ||
private final String contentTypeDec; | ||
|
||
public AbstractContentTypeRamlValidator(ActionType actionType, String contentTypeDec, String... componentTypes) { | ||
this.actionType = actionType; | ||
this.contentTypeDec = contentTypeDec; | ||
String pipelineSeparatedComponentTypes = stream(componentTypes).collect(Collectors.joining("|")); | ||
mediaTypePattern = Pattern | ||
.compile(format("application/vnd\\.\\S+\\.(%s)\\.\\S+\\+json", pipelineSeparatedComponentTypes)); | ||
} | ||
|
||
@Override | ||
protected void validate(final Resource resource) { | ||
Action postAction = resource.getActions().get(actionType); | ||
if (postAction != null) { | ||
Collection<MimeType> mediaTypes = mediaTypesToValidate(postAction); | ||
checkNonEmpty(mediaTypes); | ||
checkValid(mediaTypes); | ||
} | ||
} | ||
|
||
protected abstract Collection<MimeType> mediaTypesToValidate(Action postAction); | ||
|
||
private void checkValid(final Collection<MimeType> mediaTypes) { | ||
mediaTypes.forEach(mt -> { | ||
Matcher matcher = mediaTypePattern.matcher(mt.getType()); | ||
if (!matcher.matches()) { | ||
throw new RamlValidationException(format("Invalid %s: %s", contentTypeDec, mt.getType())); | ||
} | ||
}); | ||
} | ||
|
||
private void checkNonEmpty(final Collection<MimeType> mediaTypes) { | ||
if (mediaTypes.isEmpty()) { | ||
throw new RamlValidationException(format("%s not set", capitalize(contentTypeDec))); | ||
} | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
...dation/AbstractResourceRamlValidator.java → ...idator/AbstractResourceRamlValidator.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
2 changes: 1 addition & 1 deletion
2
...ms/validation/CompositeRamlValidator.java → ...mon/validator/CompositeRamlValidator.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
2 changes: 1 addition & 1 deletion
2
...idation/ContainsActionsRamlValidator.java → ...lidator/ContainsActionsRamlValidator.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
2 changes: 1 addition & 1 deletion
2
...ation/ContainsResourcesRamlValidator.java → ...dator/ContainsResourcesRamlValidator.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
2 changes: 1 addition & 1 deletion
2
...s/validation/RamlValidationException.java → ...on/validator/RamlValidationException.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
2 changes: 1 addition & 1 deletion
2
...ce/raml/jms/validation/RamlValidator.java → .../raml/common/validator/RamlValidator.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
23 changes: 23 additions & 0 deletions
23
...s/src/main/java/uk/gov/justice/raml/common/validator/RequestContentTypeRamlValidator.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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package uk.gov.justice.raml.common.validator; | ||
|
||
import org.raml.model.Action; | ||
import org.raml.model.ActionType; | ||
import org.raml.model.MimeType; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* Checks if action's mediaType is valid and contains a valid command or event. | ||
*/ | ||
public class RequestContentTypeRamlValidator extends AbstractContentTypeRamlValidator { | ||
|
||
public RequestContentTypeRamlValidator() { | ||
super(ActionType.POST, "request type", "commands", "events"); | ||
} | ||
|
||
@Override | ||
protected Collection<MimeType> mediaTypesToValidate(final Action postAction) { | ||
return postAction.getBody().values(); | ||
} | ||
|
||
} |
23 changes: 10 additions & 13 deletions
23
...alidation/CompositeRamlValidatorTest.java → ...validator/CompositeRamlValidatorTest.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,42 +1,39 @@ | ||
package uk.gov.justice.raml.jms.validation; | ||
package uk.gov.justice.raml.common.validator; | ||
|
||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
import org.raml.model.Raml; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
public class CompositeRamlValidatorTest { | ||
|
||
@Test | ||
public void shouldCallAllElementsOfComposite() throws Exception { | ||
|
||
RamlValidator element1 = mock(RamlValidator.class); | ||
RamlValidator element2 = mock(RamlValidator.class); | ||
RamlValidator element1 = Mockito.mock(RamlValidator.class); | ||
RamlValidator element2 = Mockito.mock(RamlValidator.class); | ||
|
||
RamlValidator validator = new CompositeRamlValidator(element1, element2); | ||
|
||
Raml raml = new Raml(); | ||
validator.validate(raml); | ||
|
||
verify(element1).validate(raml); | ||
verify(element2).validate(raml); | ||
Mockito.verify(element1).validate(raml); | ||
Mockito.verify(element2).validate(raml); | ||
} | ||
|
||
@Test | ||
public void shouldCallAllElementsOfCompositeMultipleTimes() throws Exception { | ||
RamlValidator element1 = mock(RamlValidator.class); | ||
RamlValidator element2 = mock(RamlValidator.class); | ||
RamlValidator element1 = Mockito.mock(RamlValidator.class); | ||
RamlValidator element2 = Mockito.mock(RamlValidator.class); | ||
|
||
RamlValidator validator = new CompositeRamlValidator(element1, element2); | ||
|
||
Raml raml = new Raml(); | ||
validator.validate(raml); | ||
validator.validate(raml); | ||
|
||
verify(element1, times(2)).validate(raml); | ||
verify(element2, times(2)).validate(raml); | ||
Mockito.verify(element1, Mockito.times(2)).validate(raml); | ||
Mockito.verify(element2, Mockito.times(2)).validate(raml); | ||
} | ||
|
||
} |
10 changes: 5 additions & 5 deletions
10
...ion/ContainsActionsRamlValidatorTest.java → ...tor/ContainsActionsRamlValidatorTest.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
8 changes: 4 additions & 4 deletions
8
...n/ContainsResourcesRamlValidatorTest.java → ...r/ContainsResourcesRamlValidatorTest.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
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
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
Oops, something went wrong.