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

Commit

Permalink
Add latest changes from master and schema catalog support changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mapingo committed Jan 10, 2018
1 parent c9b2c6d commit 87def83
Show file tree
Hide file tree
Showing 20 changed files with 320 additions and 6 deletions.
5 changes: 5 additions & 0 deletions framework-api-all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,10 @@
<artifactId>framework-api-messaging-jms</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>uk.gov.justice.services</groupId>
<artifactId>framework-api-messaging-client</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
5 changes: 5 additions & 0 deletions framework-api-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
<artifactId>commons-testing</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-testlib</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
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 {
}
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 {
}
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);
}
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();
}
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);
}
}
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);
}
}
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);
}
}
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();
}
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);
}
}
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);
}
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);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.gov.justice.services.core.requester;

import uk.gov.justice.services.messaging.Envelope;
import uk.gov.justice.services.messaging.JsonEnvelope;

/**
Expand All @@ -14,7 +15,17 @@ public interface Requester {
* @param envelope envelope containing the request that needs to be sent
* @return an envelope containing the response
*/
JsonEnvelope request(final JsonEnvelope envelope);
JsonEnvelope request(final Envelope<?> envelope);

/**
* Sends a request envelope to the next component.
* The correct requester is injected by the framework.
*
* @param envelope envelope containing the request that needs to be sent
* @param clazz the return Envelope payload type
* @return an envelope containing the response with payload T
*/
<T> Envelope<T> request(final Envelope<?> envelope, Class<T> clazz);

/**
* Sends a request envelope to the next component setting system user id.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.gov.justice.services.core.sender;

import uk.gov.justice.services.messaging.Envelope;
import uk.gov.justice.services.messaging.JsonEnvelope;

/**
Expand All @@ -10,9 +11,9 @@ public interface Sender {
/**
* Sends envelope to the next component. The correct sender is injected by the framework.
*
* @param envelope JsonEnvelope that needs to be sent.
* @param envelope with payload T that needs to be sent.
*/
void send(final JsonEnvelope envelope);
void send(final Envelope<?> envelope);

/**
* Sends envelope to the next component setting system user id.
Expand Down
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();
}
}
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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
@Retention(RUNTIME)
@Target(TYPE)
public @interface Event {

public static final String SYSTEM_EVENTS = "system.events.";

String value();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package uk.gov.justice.services.eventsourcing.source.core;

import uk.gov.justice.services.eventsourcing.source.core.exception.EventStreamException;

import java.util.UUID;

/**
Expand All @@ -10,8 +12,25 @@ public interface EventSource {
/**
* Get a stream of events by stream id.
*
* @param streamId the stream id
* @param streamId - the stream id of the stream to be retrieved
* @return the {@link EventStream}
*/
public EventStream getStreamById(final UUID streamId);
EventStream getStreamById(final UUID streamId);


/**
* Clones the stream into a new stream id.
*
* @param streamId - the stream id of the stream to be cloned
* @return the StreamId of the cloned stream.
* @throws EventStreamException if the cloning of the stream fails
*/
UUID cloneStream(final UUID streamId) throws EventStreamException;

/**
* Clears all of the events from a stream id.
*
* @param streamId - the streamId of the stream to be cleared
*/
void clearStream(final UUID streamId) throws EventStreamException;
}
Loading

0 comments on commit 87def83

Please sign in to comment.