Skip to content
This repository has been archived by the owner on Aug 13, 2020. It is now read-only.

Commit

Permalink
Merge 1deb059 into fdd2506
Browse files Browse the repository at this point in the history
  • Loading branch information
jaceko committed Apr 7, 2016
2 parents fdd2506 + 1deb059 commit 71961ea
Show file tree
Hide file tree
Showing 50 changed files with 1,624 additions and 609 deletions.
47 changes: 47 additions & 0 deletions adapters/adapters-commons/pom.xml
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>
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)));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package uk.gov.justice.raml.jms.validation;
package uk.gov.justice.raml.common.validator;

import org.raml.model.Raml;
import org.raml.model.Resource;

abstract class AbstractResourceRamlValidator implements RamlValidator {
public abstract class AbstractResourceRamlValidator implements RamlValidator {

@Override
public void validate(final Raml raml) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.justice.raml.jms.validation;
package uk.gov.justice.raml.common.validator;

import org.raml.model.Raml;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.justice.raml.jms.validation;
package uk.gov.justice.raml.common.validator;

import org.raml.model.Resource;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.justice.raml.jms.validation;
package uk.gov.justice.raml.common.validator;

import org.raml.model.Raml;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.justice.raml.jms.validation;
package uk.gov.justice.raml.common.validator;

public class RamlValidationException extends RuntimeException {
private static final long serialVersionUID = 1L;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.justice.raml.jms.validation;
package uk.gov.justice.raml.common.validator;

import org.raml.model.Raml;

Expand Down
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();
}

}
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);
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package uk.gov.justice.raml.jms.validation;

import static uk.gov.justice.services.adapters.test.utils.builder.ActionBuilder.action;
import static uk.gov.justice.services.adapters.test.utils.builder.RamlBuilder.raml;
import static uk.gov.justice.services.adapters.test.utils.builder.ResourceBuilder.resource;
package uk.gov.justice.raml.common.validator;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static uk.gov.justice.services.adapters.test.utils.builder.ActionBuilder.action;
import static uk.gov.justice.services.adapters.test.utils.builder.RamlBuilder.raml;
import static uk.gov.justice.services.adapters.test.utils.builder.ResourceBuilder.resource;

public class ContainsActionsRamlValidatorTest {

private RamlValidator validator = new ContainsActionsRamlValidator();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package uk.gov.justice.raml.jms.validation;

import static uk.gov.justice.services.adapters.test.utils.builder.RamlBuilder.raml;
import static uk.gov.justice.services.adapters.test.utils.builder.ResourceBuilder.resource;
package uk.gov.justice.raml.common.validator;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.raml.model.Raml;

import static uk.gov.justice.services.adapters.test.utils.builder.RamlBuilder.raml;
import static uk.gov.justice.services.adapters.test.utils.builder.ResourceBuilder.resource;

public class ContainsResourcesRamlValidatorTest {

private RamlValidator validator = new ContainsResourcesRamlValidator();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package uk.gov.justice.raml.jms.validation;
package uk.gov.justice.raml.common.validator;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.raml.model.ActionType;

import static org.raml.model.ActionType.GET;
import static org.raml.model.ActionType.HEAD;
Expand All @@ -9,14 +14,9 @@
import static uk.gov.justice.services.adapters.test.utils.builder.RamlBuilder.raml;
import static uk.gov.justice.services.adapters.test.utils.builder.ResourceBuilder.resource;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.raml.model.ActionType;
public class RequestContentTypeRamlValidatorTest {

public class MediaTypeRamlValidatorTest {

private RamlValidator validator = new MediaTypeRamlValidator();
private RamlValidator validator = new RequestContentTypeRamlValidator();

@Test
public void shouldPassIfMediaTypeContainsAValidCommand() throws Exception {
Expand All @@ -28,7 +28,7 @@ public void shouldPassIfMediaTypeContainsAValidCommand() throws Exception {
.build());

}

@Test
public void shouldIgnoreInvalidMediaTypesInNonPOSTActions() throws Exception {

Expand All @@ -40,7 +40,7 @@ public void shouldIgnoreInvalidMediaTypesInNonPOSTActions() throws Exception {
.with(action(HEAD, "application/vnd.structure.dummy.command3+json"))
.with(action(PUT, "application/vnd.structure.dummy.command4+json"))
.with(action(OPTIONS, "application/vnd.structure.dummy.command5+json"))
)
)
.build());

}
Expand All @@ -52,7 +52,7 @@ public void shouldIgnoreInvalidMediaTypesInNonPOSTActions() throws Exception {
public void shouldThrowExceptionIfMediaTypeNotSet() throws Exception {

exception.expect(RamlValidationException.class);
exception.expectMessage("No declared media types");
exception.expectMessage("Request type not set");

validator.validate(raml()
.with(resource()
Expand All @@ -65,7 +65,7 @@ public void shouldThrowExceptionIfMediaTypeNotSet() throws Exception {
public void shouldThrowExceptionIfMediaTypeDoesNotContainAValidCommand() throws Exception {

exception.expect(RamlValidationException.class);
exception.expectMessage("Invalid media type: application/vnd.people.invalid.command1+json");
exception.expectMessage("Invalid request type: application/vnd.people.invalid.command1+json");

validator.validate(
raml()
Expand All @@ -79,7 +79,7 @@ public void shouldThrowExceptionIfMediaTypeDoesNotContainAValidCommand() throws
public void shouldThrowExceptionIfNotvalidMediaType() throws Exception {

exception.expect(RamlValidationException.class);
exception.expectMessage("Invalid media type: nd.people.commands.command1+json");
exception.expectMessage("Invalid request type: nd.people.commands.command1+json");

validator.validate(
raml()
Expand All @@ -93,7 +93,7 @@ public void shouldThrowExceptionIfNotvalidMediaType() throws Exception {
public void shouldThrowExceptionIfNotvalidMediaType2() throws Exception {

exception.expect(RamlValidationException.class);
exception.expectMessage("Invalid media type: nd.people.unknown.command1+nonjson");
exception.expectMessage("Invalid request type: nd.people.unknown.command1+nonjson");

validator.validate(
raml()
Expand All @@ -107,7 +107,7 @@ public void shouldThrowExceptionIfNotvalidMediaType2() throws Exception {
public void shouldThrowExceptionIfMediaTypeDoesNotContainContext() throws Exception {

exception.expect(RamlValidationException.class);
exception.expectMessage("Invalid media type: application/vnd.handlers.command1+json");
exception.expectMessage("Invalid request type: application/vnd.handlers.command1+json");

validator.validate(
raml()
Expand Down
9 changes: 9 additions & 0 deletions adapters/adapters-test-utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
</dependency>
<dependency>
<groupId>uk.gov.justice.maven</groupId>
<artifactId>raml-generator-core</artifactId>
<version>${raml-maven-plugin.version}</version>
</dependency>
<dependency>
<groupId>org.raml</groupId>
<artifactId>raml-parser</artifactId>
Expand All @@ -46,5 +51,9 @@
<groupId>com.jayway.awaitility</groupId>
<artifactId>awaitility</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
Loading

0 comments on commit 71961ea

Please sign in to comment.