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

Commit

Permalink
Add support for POJO envelopes
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Synowiec committed Mar 22, 2018
1 parent bd5a8b3 commit 1a9e55c
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 136 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package uk.gov.justice.services.core.enveloper;

import uk.gov.justice.services.core.enveloper.spi.EnveloperProvider;
import uk.gov.justice.services.messaging.Envelope;
import uk.gov.justice.services.messaging.JsonEnvelope;
import uk.gov.justice.services.messaging.Metadata;

import java.util.function.Function;

Expand All @@ -24,4 +27,35 @@ public interface Enveloper {
* @return a function that wraps objects into an envelope.
*/
Function<Object, JsonEnvelope> withMetadataFrom(final JsonEnvelope envelope, final String name);

static Function<Object, JsonEnvelope> toEnvelopeWithMetadataFrom(final Envelope<?> envelope) {
return EnveloperProvider.provider().toEnvelopeWithMetadataFrom(envelope);
}

static <T> EnveloperBuilder<T> envelop(final T payload) {
return EnveloperProvider.provider().envelop(payload);
}

/**
* A helper interface to provide an instance of a {@link Envelope} with given {@link Metadata}
*/
interface EnveloperBuilder<T> {

/**
* Helper interface method to set name
*
* @param name the name to be added to the Envelope.
* @return the EnveloperBuilder instance
*/
EnveloperBuilder<T> withName(String name);

/**
* Helper interface method to apply metadata
*
* @param envelope the Envelope with metadata to be applied on new envelope.
* @return the EnveloperBuilder instance
*/
Envelope<T> withMetadataFrom(final Envelope<?> envelope);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package uk.gov.justice.services.core.enveloper.spi;

import uk.gov.justice.services.core.enveloper.Enveloper;
import uk.gov.justice.services.messaging.Envelope;
import uk.gov.justice.services.messaging.JsonEnvelope;
import uk.gov.justice.services.messaging.Metadata;
import uk.gov.justice.services.messaging.MetadataBuilder;

import java.util.Iterator;
import java.util.ServiceLoader;
import java.util.function.Function;

import javax.json.JsonValue;

/**
* Interface for EnveloperProvider implementations to provide methods for constructing
* {@link Envelope} and {@link MetadataBuilder} instances.
*
* Call the static method {@code EnveloperProvider.provider()} to retrieve the
* EnveloperProvider instance from service dependency on the classpath.
*/
public interface EnveloperProvider {

/**
* Loads an implementation of EnveloperProvider using the {@link ServiceLoader} mechanism. An
* instance of the first implementing class from the loader list is returned.
*
* @return an instance of EnveloperProvider
* @throws EnveloperProviderNotFoundException if no implementations of EnveloperProvider
* are found
*/
static EnveloperProvider provider() {
final ServiceLoader<EnveloperProvider> loader = ServiceLoader.load(EnveloperProvider.class);
final Iterator<EnveloperProvider> iterator = loader.iterator();

if (iterator.hasNext()) {
return iterator.next();
}

throw new EnveloperProviderNotFoundException("No EnveloperProvider implementation found");
}

/**
* Provide an instance of a {@link Envelope} with given {@link Metadata} and {@link
* JsonValue}.
*
//* @param metadata the Metadata to be added to the Envelope
* @param payload the JsonValue to be added to the Envelope
* @return the Envelope instance
*/

<T> Enveloper.EnveloperBuilder<T> envelop(final T payload);

Function<Object, JsonEnvelope> toEnvelopeWithMetadataFrom(final Envelope<?> envelope);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package uk.gov.justice.services.core.enveloper.spi;

public class EnveloperProviderNotFoundException extends RuntimeException {

private static final long serialVersionUID = -9007593512368498735L;

public EnveloperProviderNotFoundException(final String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package uk.gov.justice.services.core.mapping;

import static java.lang.String.format;
import static java.util.Optional.of;

public interface NameToMediaTypeConverter {

MediaType convert(final String name);
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import javax.json.JsonValue;

/**
* Abstract class for EnvelopeProvider implementations to provide methods for constructing
* Interface for EnvelopeProvider implementations to provide methods for constructing
* {@link Envelope} and {@link MetadataBuilder} instances.
*
* Call the static method {@code EnvelopeProvider.provider()} to retrieve the
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package uk.gov.justice.services.messaging.spi;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

import org.junit.Test;

public class JsonEnvelopeProviderNotFoundExceptionTest {

@Test
public void shouldCreateExceptionWithMessage() throws Exception {
final JsonEnvelopeProviderNotFoundException exception = new JsonEnvelopeProviderNotFoundException("Test message");
assertThat(exception.getMessage(), is("Test message"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsInstanceOf.instanceOf;

import uk.gov.justice.services.messaging.spi.DummyJsonEnvelopeProvider;
import uk.gov.justice.services.messaging.spi.JsonEnvelopeProvider;

import org.junit.Test;

public class JsonEnvelopeProviderTest {
Expand Down

0 comments on commit 1a9e55c

Please sign in to comment.