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

Commit

Permalink
Added SPI for InterceptorContextProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Synowiec committed Jan 29, 2018
1 parent 766cf25 commit 38b3525
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to

## [Unreleased]

## [1.2.0] - 2018-01-26

### Added
- InterceptorContextProvider service provider

## 1.1.0 - 2018-01-17

### Changed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
package uk.gov.justice.services.core.interceptor;

import uk.gov.justice.services.core.interceptor.spi.InterceptorContextProvider;
import uk.gov.justice.services.messaging.JsonEnvelope;

import java.util.Optional;

public interface InterceptorContext {

/**
* Create an interceptor context with an input {@link ContextPayload} that wraps the input
* envelope, an output {@link ContextPayload} with no envelope and an injection point.
*
* @param inputEnvelope the input JsonEnvelope
* @return the new InterceptorContext
*/
static InterceptorContext interceptorContextWithInput(final JsonEnvelope inputEnvelope) {
return InterceptorContextProvider.provider().interceptorContextWithInput(inputEnvelope);

}

/**
* Create a copy of this interceptor context that will contain the provided input
* envelope.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package uk.gov.justice.services.core.interceptor.spi;

import uk.gov.justice.services.core.interceptor.InterceptorContext;
import uk.gov.justice.services.messaging.JsonEnvelope;

import java.util.Iterator;
import java.util.ServiceLoader;

public interface InterceptorContextProvider {

static InterceptorContextProvider provider() {
final ServiceLoader<InterceptorContextProvider> loader = ServiceLoader.load(InterceptorContextProvider.class);
final Iterator<InterceptorContextProvider> iterator = loader.iterator();

if (iterator.hasNext()) {
return iterator.next();
} else {
throw new InterceptorContextProviderNotFoundException("No InterceptorContextProvider implementation found");
}
}

public abstract InterceptorContext interceptorContextWithInput(final JsonEnvelope inputEnvelope);


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

public class InterceptorContextProviderNotFoundException extends RuntimeException {

private static final long serialVersionUID = -9004536849208475775L;

public InterceptorContextProviderNotFoundException(final String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package uk.gov.justice.services.core.interceptor.spi;

import uk.gov.justice.services.core.interceptor.InterceptorContext;
import uk.gov.justice.services.messaging.JsonEnvelope;

public class DummyInterceptorContextProvider implements InterceptorContextProvider {

@Override
public InterceptorContext interceptorContextWithInput(final JsonEnvelope inputEnvelope) {
return null;
}

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

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

import org.junit.Test;

public class InterceptorContextProviderNotFoundExceptionTest {

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

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

import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsInstanceOf.instanceOf;

import org.junit.Test;

public class InterceptorContextProviderTest {

@Test
public void shouldProvideInterceptorContextProvider() throws Exception {
final InterceptorContextProvider interceptorContextProvider = InterceptorContextProvider.provider();

assertThat(interceptorContextProvider, notNullValue());
assertThat(interceptorContextProvider, instanceOf(DummyInterceptorContextProvider.class));
}

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

import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uk.gov.justice.services.core.interceptor.spi.DummyInterceptorContextProvider

0 comments on commit 38b3525

Please sign in to comment.