This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add latest changes from master and schema catalog support changes
- Loading branch information
Showing
20 changed files
with
320 additions
and
6 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
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
12 changes: 12 additions & 0 deletions
12
...work-api-core/src/main/java/uk/gov/justice/services/core/annotation/MediaTypesMapper.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,12 @@ | ||
package uk.gov.justice.services.core.annotation; | ||
|
||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RUNTIME) | ||
@Target(TYPE) | ||
public @interface MediaTypesMapper { | ||
} |
12 changes: 12 additions & 0 deletions
12
framework-api-core/src/main/java/uk/gov/justice/services/core/annotation/SchemaIdMapper.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,12 @@ | ||
package uk.gov.justice.services.core.annotation; | ||
|
||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RUNTIME) | ||
@Target(TYPE) | ||
public @interface SchemaIdMapper { | ||
} |
7 changes: 6 additions & 1 deletion
7
framework-api-core/src/main/java/uk/gov/justice/services/core/json/JsonSchemaValidator.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,5 +1,10 @@ | ||
package uk.gov.justice.services.core.json; | ||
|
||
import uk.gov.justice.services.core.mapping.MediaType; | ||
|
||
import java.util.Optional; | ||
|
||
public interface JsonSchemaValidator { | ||
void validate(final String payload, final String name); | ||
|
||
void validate(final String payload, final String actionName, final Optional<MediaType> mediaType); | ||
} |
8 changes: 8 additions & 0 deletions
8
...core/src/main/java/uk/gov/justice/services/core/mapping/ActionNameToMediaTypesMapper.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,8 @@ | ||
package uk.gov.justice.services.core.mapping; | ||
|
||
import java.util.Map; | ||
|
||
public interface ActionNameToMediaTypesMapper { | ||
|
||
Map<String, MediaTypes> getActionNameToMediaTypesMap(); | ||
} |
10 changes: 10 additions & 0 deletions
10
...-core/src/main/java/uk/gov/justice/services/core/mapping/MalformedMediaTypeException.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,10 @@ | ||
package uk.gov.justice.services.core.mapping; | ||
|
||
public class MalformedMediaTypeException extends RuntimeException { | ||
|
||
private static final long serialVersionUID = -7396471821477969237L; | ||
|
||
public MalformedMediaTypeException(final String message) { | ||
super(message); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...e/src/main/java/uk/gov/justice/services/core/mapping/MalformedMediaTypeNameException.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,10 @@ | ||
package uk.gov.justice.services.core.mapping; | ||
|
||
public class MalformedMediaTypeNameException extends RuntimeException { | ||
|
||
private static final long serialVersionUID = 5426383027375213199L; | ||
|
||
public MalformedMediaTypeNameException(final String message) { | ||
super(message); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
framework-api-core/src/main/java/uk/gov/justice/services/core/mapping/MediaType.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,58 @@ | ||
package uk.gov.justice.services.core.mapping; | ||
|
||
import static java.lang.String.format; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Representation of a media type for usage when identifying schema ids in generated media type to | ||
* schema id mapping classes and requesting a schema for validating a json payload. | ||
*/ | ||
public class MediaType { | ||
|
||
private final String type; | ||
private final String subtype; | ||
|
||
public MediaType(final String type, final String subtype) { | ||
this.type = type; | ||
this.subtype = subtype; | ||
} | ||
|
||
public MediaType(final String mediaType) { | ||
final int index = mediaType.indexOf('/'); | ||
|
||
if (index < 0) { | ||
throw new MalformedMediaTypeException(format("Cannot parse media type '%s' into type and subtype. Missing slash character", mediaType)); | ||
} | ||
|
||
type = mediaType.substring(0, index); | ||
subtype = mediaType.substring(index + 1); | ||
} | ||
|
||
public String getType() { | ||
return type; | ||
} | ||
|
||
public String getSubtype() { | ||
return subtype; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return type + "/" + subtype; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
final MediaType mediaType = (MediaType) o; | ||
return Objects.equals(type, mediaType.type) && | ||
Objects.equals(subtype, mediaType.subtype); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(type, subtype); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...pi-core/src/main/java/uk/gov/justice/services/core/mapping/MediaTypeToSchemaIdMapper.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,8 @@ | ||
package uk.gov.justice.services.core.mapping; | ||
|
||
import java.util.Map; | ||
|
||
public interface MediaTypeToSchemaIdMapper { | ||
|
||
Map<MediaType, String> getMediaTypeToSchemaIdMap(); | ||
} |
24 changes: 24 additions & 0 deletions
24
framework-api-core/src/main/java/uk/gov/justice/services/core/mapping/MediaTypes.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,24 @@ | ||
package uk.gov.justice.services.core.mapping; | ||
|
||
import static java.util.Optional.ofNullable; | ||
|
||
import java.util.Optional; | ||
|
||
public class MediaTypes { | ||
|
||
private final MediaType requestMediaType; | ||
private final MediaType responseMediaType; | ||
|
||
public MediaTypes(final MediaType requestMediaType, final MediaType responseMediaType) { | ||
this.requestMediaType = requestMediaType; | ||
this.responseMediaType = responseMediaType; | ||
} | ||
|
||
public Optional<MediaType> getRequestMediaType() { | ||
return ofNullable(requestMediaType); | ||
} | ||
|
||
public Optional<MediaType> getResponseMediaType() { | ||
return ofNullable(responseMediaType); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...k-api-core/src/main/java/uk/gov/justice/services/core/mapping/MediaTypesMappingCache.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,8 @@ | ||
package uk.gov.justice.services.core.mapping; | ||
|
||
import java.util.Optional; | ||
|
||
public interface MediaTypesMappingCache { | ||
|
||
Optional<MediaTypes> mediaTypesFor(final String actionName); | ||
} |
14 changes: 14 additions & 0 deletions
14
...ork-api-core/src/main/java/uk/gov/justice/services/core/mapping/SchemaIdMappingCache.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,14 @@ | ||
package uk.gov.justice.services.core.mapping; | ||
|
||
import java.util.Optional; | ||
|
||
public interface SchemaIdMappingCache { | ||
|
||
/** | ||
* Get the schema id for a given media type from the cache of {@link MediaType} to schema id. | ||
* | ||
* @param mediaType the {@link MediaType} to look up | ||
* @return Optional of the schema id or Optional.empty() if not found | ||
*/ | ||
Optional<String> schemaIdFor(final MediaType mediaType); | ||
} |
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
52 changes: 52 additions & 0 deletions
52
framework-api-core/src/test/java/uk/gov/justice/services/core/mapping/MediaTypeTest.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,52 @@ | ||
package uk.gov.justice.services.core.mapping; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.fail; | ||
|
||
import com.google.common.testing.EqualsTester; | ||
import org.junit.Test; | ||
|
||
public class MediaTypeTest { | ||
|
||
@Test | ||
public void shouldCreateAMediaTypeFromTypeAndSubType() throws Exception { | ||
|
||
final MediaType mediaType = new MediaType("application", "vnd.starship.command.commence-formation-attack+json"); | ||
|
||
assertThat(mediaType.toString(), is("application/vnd.starship.command.commence-formation-attack+json")); | ||
} | ||
|
||
@Test | ||
public void shouldCreateAMediaTypeFromAMediaTypeString() throws Exception { | ||
|
||
final MediaType mediaType = new MediaType("application/vnd.starship.command.commence-formation-attack+json"); | ||
|
||
assertThat(mediaType.getType(), is("application")); | ||
assertThat(mediaType.getSubtype(), is("vnd.starship.command.commence-formation-attack+json")); | ||
} | ||
|
||
@Test | ||
public void shouldFailIfTheMediaTypeStringIsMalformed() throws Exception { | ||
try { | ||
new MediaType("vnd.some-malformed+json"); | ||
fail(); | ||
} catch (final MalformedMediaTypeException expected) { | ||
assertThat(expected.getMessage(), is("Cannot parse media type 'vnd.some-malformed+json' into type and subtype. Missing slash character")); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldHaveCorrectEqualsAndHashcode() { | ||
final MediaType mediaType_1 = new MediaType("application", "vnd.starship.command.commence-formation-attack+json"); | ||
final MediaType mediaType_2 = new MediaType("application", "vnd.starship.command.commence-formation-attack+json"); | ||
final MediaType mediaType_3 = new MediaType("other", "vnd.starship.command.commence-formation-attack+json"); | ||
final MediaType mediaType_4 = new MediaType("application", "vnd.other.command.commence-other+json"); | ||
|
||
new EqualsTester() | ||
.addEqualityGroup(mediaType_1, mediaType_2) | ||
.addEqualityGroup(mediaType_3) | ||
.addEqualityGroup(mediaType_4) | ||
.testEquals(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
framework-api-core/src/test/java/uk/gov/justice/services/core/mapping/MediaTypesTest.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,40 @@ | ||
package uk.gov.justice.services.core.mapping; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import java.util.Optional; | ||
|
||
import org.junit.Test; | ||
|
||
public class MediaTypesTest { | ||
|
||
@Test | ||
public void shouldCreateMediaTypesWithRequestAndResponse() { | ||
final MediaType request = mock(MediaType.class); | ||
final MediaType response = mock(MediaType.class); | ||
final MediaTypes mediaTypes = new MediaTypes(request, response); | ||
|
||
assertThat(mediaTypes.getRequestMediaType(), is(Optional.of(request))); | ||
assertThat(mediaTypes.getResponseMediaType(), is(Optional.of(response))); | ||
} | ||
|
||
@Test | ||
public void shouldCreateMediaTypesWithRequest() { | ||
final MediaType request = mock(MediaType.class); | ||
final MediaTypes mediaTypes = new MediaTypes(request, null); | ||
|
||
assertThat(mediaTypes.getRequestMediaType(), is(Optional.of(request))); | ||
assertThat(mediaTypes.getResponseMediaType(), is(Optional.empty())); | ||
} | ||
|
||
@Test | ||
public void shouldCreateMediaTypesWithResponse() { | ||
final MediaType response = mock(MediaType.class); | ||
final MediaTypes mediaTypes = new MediaTypes(null, response); | ||
|
||
assertThat(mediaTypes.getRequestMediaType(), is(Optional.empty())); | ||
assertThat(mediaTypes.getResponseMediaType(), is(Optional.of(response))); | ||
} | ||
} |
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.