From d9b037f6f0bbfec8ef1b5b3c55a13f8bf04cc47d Mon Sep 17 00:00:00 2001 From: Martin Spasov Date: Mon, 29 Apr 2019 13:07:41 +0100 Subject: [PATCH] Add unifiedsearch indexer --- CHANGELOG.md | 1 + .../cakeshop/persistence/IndexRepository.java | 12 ++ .../cakeshop/persistence/entity/Index.java | 55 +++++++ .../persistence/CakeOrderRepositoryIT.java | 1 - .../persistence/IndexRepositoryIT.java | 42 ++++++ .../persistence/entity/IndexTest.java | 31 ++++ .../example-event-indexer/pom.xml | 134 ++++++++++++++++++ .../dummy/DummyUnifiedSearchIndexer.java | 29 ++++ .../DummyUnifiedSearchIndexerProducer.java | 22 +++ .../src/main/resources/META-INF/beans.xml | 6 + .../src/transformer/cake-ordered-spec.json | 12 ++ .../src/yaml/event-sources.yaml | 6 + .../src/yaml/subscriptions-descriptor.yaml | 12 ++ .../src/yaml/unified-search-descriptor.yaml | 10 ++ .../example-service/example-event/pom.xml | 73 ++++++++++ .../example-service/example-it/pom.xml | 13 ++ .../event/LinkedEventRepositoryTruncator.java | 0 .../cakeshop/it/CakeShopTimeStampIT.java | 3 + .../cakeshop/it/UnifiedSearchIndexerIT.java | 72 ++++++++++ .../example/cakeshop/it/helpers/Querier.java | 6 + .../it/params/CakeShopMediaTypes.java | 1 + .../cakeshop/it/params/CakeShopUris.java | 2 + .../wildfly-config/standalone-single.xml | 2 +- .../007-index-table.changelog.xml | 21 +++ .../cakeshop/query/api/IndexQueryApi.java | 24 ++++ .../query/api/request/SearchIndex.java | 24 ++++ .../query/api/response/IndexView.java | 23 +++ .../src/raml/example-query-api.raml | 18 ++- .../src/raml/json/index.json | 4 + .../json/schema/example.search-index.json | 17 +++ .../cakeshop/query/api/IndexQueryApiTest.java | 21 +++ .../cakeshop/query/view/IndexQueryView.java | 29 ++++ .../query/view/request/SearchIndex.java | 24 ++++ .../query/view/response/IndexView.java | 24 ++++ .../query/view/service/IndexService.java | 21 +++ .../src/raml/example-query-view.raml | 18 ++- .../src/raml/json/index.json | 4 + .../json/schema/example.search-index.json | 17 +++ .../query/view/IndexQueryViewTest.java | 61 ++++++++ .../query/view/service/IndexServiceTest.java | 57 ++++++++ .../example-service/example-single/pom.xml | 7 + pom.xml | 1 + 42 files changed, 956 insertions(+), 4 deletions(-) create mode 100644 example-context/example-persistence/src/main/java/uk/gov/justice/services/example/cakeshop/persistence/IndexRepository.java create mode 100644 example-context/example-persistence/src/main/java/uk/gov/justice/services/example/cakeshop/persistence/entity/Index.java create mode 100644 example-context/example-persistence/src/test/java/uk/gov/justice/services/example/cakeshop/persistence/IndexRepositoryIT.java create mode 100644 example-context/example-persistence/src/test/java/uk/gov/justice/services/example/cakeshop/persistence/entity/IndexTest.java create mode 100644 example-context/example-service/example-event/example-event-indexer/pom.xml create mode 100644 example-context/example-service/example-event/example-event-indexer/src/main/java/uk/gov/justice/services/example/cakeshop/dummy/DummyUnifiedSearchIndexer.java create mode 100644 example-context/example-service/example-event/example-event-indexer/src/main/java/uk/gov/justice/services/example/cakeshop/dummy/DummyUnifiedSearchIndexerProducer.java create mode 100644 example-context/example-service/example-event/example-event-indexer/src/main/resources/META-INF/beans.xml create mode 100644 example-context/example-service/example-event/example-event-indexer/src/transformer/cake-ordered-spec.json create mode 100644 example-context/example-service/example-event/example-event-indexer/src/yaml/event-sources.yaml create mode 100644 example-context/example-service/example-event/example-event-indexer/src/yaml/subscriptions-descriptor.yaml create mode 100644 example-context/example-service/example-event/example-event-indexer/src/yaml/unified-search-descriptor.yaml create mode 100644 example-context/example-service/example-it/src/test/java/uk/gov/justice/services/eventsourcing/repository/jdbc/event/LinkedEventRepositoryTruncator.java create mode 100644 example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/UnifiedSearchIndexerIT.java create mode 100644 example-context/example-service/example-liquibase/src/main/resources/liquibase/view-store-db-changesets/007-index-table.changelog.xml create mode 100644 example-context/example-service/example-query/example-query-api/src/main/java/uk/gov/justice/services/example/cakeshop/query/api/IndexQueryApi.java create mode 100644 example-context/example-service/example-query/example-query-api/src/main/java/uk/gov/justice/services/example/cakeshop/query/api/request/SearchIndex.java create mode 100644 example-context/example-service/example-query/example-query-api/src/main/java/uk/gov/justice/services/example/cakeshop/query/api/response/IndexView.java create mode 100644 example-context/example-service/example-query/example-query-api/src/raml/json/index.json create mode 100644 example-context/example-service/example-query/example-query-api/src/raml/json/schema/example.search-index.json create mode 100644 example-context/example-service/example-query/example-query-api/src/test/java/uk/gov/justice/services/example/cakeshop/query/api/IndexQueryApiTest.java create mode 100644 example-context/example-service/example-query/example-query-view/src/main/java/uk/gov/justice/services/example/cakeshop/query/view/IndexQueryView.java create mode 100644 example-context/example-service/example-query/example-query-view/src/main/java/uk/gov/justice/services/example/cakeshop/query/view/request/SearchIndex.java create mode 100644 example-context/example-service/example-query/example-query-view/src/main/java/uk/gov/justice/services/example/cakeshop/query/view/response/IndexView.java create mode 100644 example-context/example-service/example-query/example-query-view/src/main/java/uk/gov/justice/services/example/cakeshop/query/view/service/IndexService.java create mode 100644 example-context/example-service/example-query/example-query-view/src/raml/json/index.json create mode 100644 example-context/example-service/example-query/example-query-view/src/raml/json/schema/example.search-index.json create mode 100644 example-context/example-service/example-query/example-query-view/src/test/java/uk/gov/justice/services/example/cakeshop/query/view/IndexQueryViewTest.java create mode 100644 example-context/example-service/example-query/example-query-view/src/test/java/uk/gov/justice/services/example/cakeshop/query/view/service/IndexServiceTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index c63cff8a..b4ab4ad5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to ## [Unreleased] ### Added +- Unifiedsearch indexer module - Integration test for event catchup - Integration test for PublishedEvent rebuild. diff --git a/example-context/example-persistence/src/main/java/uk/gov/justice/services/example/cakeshop/persistence/IndexRepository.java b/example-context/example-persistence/src/main/java/uk/gov/justice/services/example/cakeshop/persistence/IndexRepository.java new file mode 100644 index 00000000..12d31be8 --- /dev/null +++ b/example-context/example-persistence/src/main/java/uk/gov/justice/services/example/cakeshop/persistence/IndexRepository.java @@ -0,0 +1,12 @@ +package uk.gov.justice.services.example.cakeshop.persistence; + +import uk.gov.justice.services.example.cakeshop.persistence.entity.Index; + +import java.util.UUID; + +import org.apache.deltaspike.data.api.EntityRepository; +import org.apache.deltaspike.data.api.Repository; + +@Repository +public interface IndexRepository extends EntityRepository { +} diff --git a/example-context/example-persistence/src/main/java/uk/gov/justice/services/example/cakeshop/persistence/entity/Index.java b/example-context/example-persistence/src/main/java/uk/gov/justice/services/example/cakeshop/persistence/entity/Index.java new file mode 100644 index 00000000..7657edac --- /dev/null +++ b/example-context/example-persistence/src/main/java/uk/gov/justice/services/example/cakeshop/persistence/entity/Index.java @@ -0,0 +1,55 @@ +package uk.gov.justice.services.example.cakeshop.persistence.entity; + +import java.io.Serializable; +import java.time.ZonedDateTime; +import java.util.Objects; +import java.util.UUID; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "index") +public class Index implements Serializable { + + @Id + @Column(name = "index_id") + private UUID indexId; + + @Column(name = "delivery_date", nullable = false, insertable = true, updatable = true) + private ZonedDateTime deliveryDate; + + public Index(final UUID indexId, final ZonedDateTime deliveryDate) { + this.indexId = indexId; + this.deliveryDate = deliveryDate; + } + + public Index() { + + } + + public UUID getIndexId() { + return indexId; + } + + public ZonedDateTime getDeliveryDate() { + return deliveryDate; + } + + + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + final Index index = (Index) o; + return indexId.equals(index.indexId) && + deliveryDate.equals(index.deliveryDate); + } + + @Override + public int hashCode() { + return Objects.hash(indexId, deliveryDate); + } +} diff --git a/example-context/example-persistence/src/test/java/uk/gov/justice/services/example/cakeshop/persistence/CakeOrderRepositoryIT.java b/example-context/example-persistence/src/test/java/uk/gov/justice/services/example/cakeshop/persistence/CakeOrderRepositoryIT.java index 5f224d45..b3949ac2 100644 --- a/example-context/example-persistence/src/test/java/uk/gov/justice/services/example/cakeshop/persistence/CakeOrderRepositoryIT.java +++ b/example-context/example-persistence/src/test/java/uk/gov/justice/services/example/cakeshop/persistence/CakeOrderRepositoryIT.java @@ -24,7 +24,6 @@ public class CakeOrderRepositoryIT extends BaseTransactionalTest { @Inject private CakeOrderRepository cakeOrderRepository; - private CakeOrder cakeOrderA; @Test public void shouldStoreOrder() { diff --git a/example-context/example-persistence/src/test/java/uk/gov/justice/services/example/cakeshop/persistence/IndexRepositoryIT.java b/example-context/example-persistence/src/test/java/uk/gov/justice/services/example/cakeshop/persistence/IndexRepositoryIT.java new file mode 100644 index 00000000..da5c1610 --- /dev/null +++ b/example-context/example-persistence/src/test/java/uk/gov/justice/services/example/cakeshop/persistence/IndexRepositoryIT.java @@ -0,0 +1,42 @@ +package uk.gov.justice.services.example.cakeshop.persistence; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.MatcherAssert.assertThat; + +import uk.gov.justice.services.example.cakeshop.persistence.entity.Index; +import uk.gov.justice.services.test.utils.persistence.BaseTransactionalTest; + +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.util.UUID; + +import javax.inject.Inject; + +import org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(CdiTestRunner.class) +public class IndexRepositoryIT extends BaseTransactionalTest { + + @Inject + private IndexRepository indexRepository; + + + @Test + public void shouldStoreIndex() { + final UUID indexId = UUID.randomUUID(); + final ZonedDateTime deliveryDate = ZonedDateTime.of(2014, 5, 13, 4, 12, 12, 0, ZoneId.of("UTC")); + + final Index index = new Index(indexId, deliveryDate); + indexRepository.save(index); + + final Index indexResult = indexRepository.findBy(indexId); + + assertThat(indexResult, is(notNullValue())); + assertThat(indexResult.getIndexId(), equalTo(indexId)); + assertThat(indexResult.getDeliveryDate(), equalTo(deliveryDate)); + } +} diff --git a/example-context/example-persistence/src/test/java/uk/gov/justice/services/example/cakeshop/persistence/entity/IndexTest.java b/example-context/example-persistence/src/test/java/uk/gov/justice/services/example/cakeshop/persistence/entity/IndexTest.java new file mode 100644 index 00000000..ebce3406 --- /dev/null +++ b/example-context/example-persistence/src/test/java/uk/gov/justice/services/example/cakeshop/persistence/entity/IndexTest.java @@ -0,0 +1,31 @@ +package uk.gov.justice.services.example.cakeshop.persistence.entity; + +import static java.time.ZonedDateTime.now; +import static java.util.UUID.*; + +import java.time.ZonedDateTime; +import java.util.UUID; + +import com.google.common.testing.EqualsTester; +import org.junit.Test; + +public class IndexTest { + + private final static UUID ID = randomUUID(); + private final static ZonedDateTime TIME = now(); + + @SuppressWarnings({"squid:MethodCyclomaticComplexity", "squid:S1067", "squid:S00122"}) + @Test + public void equalsAndHashCode() { + final Index item1 = new Index(ID, TIME); + final Index item2 = new Index(ID, TIME); + final Index item3 = new Index(randomUUID(), TIME); + + new EqualsTester() + .addEqualityGroup(item1, item2) + .addEqualityGroup(item3) + .testEquals(); + } + + +} \ No newline at end of file diff --git a/example-context/example-service/example-event/example-event-indexer/pom.xml b/example-context/example-service/example-event/example-event-indexer/pom.xml new file mode 100644 index 00000000..b0088528 --- /dev/null +++ b/example-context/example-service/example-event/example-event-indexer/pom.xml @@ -0,0 +1,134 @@ + + + + example-event + uk.gov.justice.services.example + 2.0.0-SNAPSHOT + + 4.0.0 + + example-event-indexer + war + + + EVENT_INDEXER + + + + + javax + javaee-api + provided + + + uk.gov.justice.framework-api + framework-api-core + + + + uk.gov.justice.framework-api + framework-api-unifiedsearch + + + + uk.gov.justice.services + common + ${framework.version} + + + uk.gov.justice.services + core + ${framework.version} + + + uk.gov.justice.framework-generators + messaging-adapter-core + ${framework-generators.version} + + + uk.gov.justice.framework-generators + unifiedsearch-client-generator + ${framework-generators.version} + + + joda-time + joda-time + + + + + uk.gov.justice.event-store + subscription-manager + ${event-store.version} + + + uk.gov.justice.event-store + event-subscription-registry + ${event-store.version} + + + uk.gov.justice.event-store + subscription-event-interceptors + ${event-store.version} + + + uk.gov.justice.framework-api + framework-api-event-listener-interceptors + ${framework-api.version} + + + org.jboss.ejb3 + jboss-ejb3-ext-api + provided + + + uk.gov.justice.services.example + example-persistence + ${project.version} + + + uk.gov.justice.services.example + example-domain + ${project.version} + + + uk.gov.justice.event-store + event-buffer-core + ${event-store.version} + + + + uk.gov.justice.json + json-transformer-jolt + ${json-transformer.version} + + + + + junit + junit + test + + + org.mockito + mockito-core + test + + + uk.gov.justice.services + test-utils-core + ${framework.version} + test + + + uk.gov.justice.utils + test-utils-logging-simple + pom + test + + + + + \ No newline at end of file diff --git a/example-context/example-service/example-event/example-event-indexer/src/main/java/uk/gov/justice/services/example/cakeshop/dummy/DummyUnifiedSearchIndexer.java b/example-context/example-service/example-event/example-event-indexer/src/main/java/uk/gov/justice/services/example/cakeshop/dummy/DummyUnifiedSearchIndexer.java new file mode 100644 index 00000000..8e2a3fcc --- /dev/null +++ b/example-context/example-service/example-event/example-event-indexer/src/main/java/uk/gov/justice/services/example/cakeshop/dummy/DummyUnifiedSearchIndexer.java @@ -0,0 +1,29 @@ +package uk.gov.justice.services.example.cakeshop.dummy; + +import static java.util.UUID.fromString; + +import uk.gov.justice.services.example.cakeshop.persistence.IndexRepository; +import uk.gov.justice.services.example.cakeshop.persistence.entity.Index; +import uk.gov.justice.services.unifiedsearch.UnifiedSearchIndexer; + +import java.time.ZonedDateTime; +import java.util.UUID; + +import javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; +import javax.json.JsonObject; + +@ApplicationScoped +public class DummyUnifiedSearchIndexer implements UnifiedSearchIndexer { + + @Inject + private IndexRepository indexRepository; + + @Override + public void indexData(final JsonObject jsonObject) { + final UUID recipeId = fromString(jsonObject.getString("recipeId")); + final ZonedDateTime deliveryDate = ZonedDateTime.parse(jsonObject.getString("deliveryDate")); + final Index index = new Index(recipeId, deliveryDate); + indexRepository.save(index); + } +} diff --git a/example-context/example-service/example-event/example-event-indexer/src/main/java/uk/gov/justice/services/example/cakeshop/dummy/DummyUnifiedSearchIndexerProducer.java b/example-context/example-service/example-event/example-event-indexer/src/main/java/uk/gov/justice/services/example/cakeshop/dummy/DummyUnifiedSearchIndexerProducer.java new file mode 100644 index 00000000..e83b326c --- /dev/null +++ b/example-context/example-service/example-event/example-event-indexer/src/main/java/uk/gov/justice/services/example/cakeshop/dummy/DummyUnifiedSearchIndexerProducer.java @@ -0,0 +1,22 @@ +package uk.gov.justice.services.example.cakeshop.dummy; + +import uk.gov.justice.services.unifiedsearch.UnifiedSearchIndexer; +import uk.gov.justice.services.unifiedsearch.UnifiedSearchName; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Produces; +import javax.enterprise.inject.spi.InjectionPoint; +import javax.inject.Inject; + +@ApplicationScoped +public class DummyUnifiedSearchIndexerProducer { + + @Inject + private DummyUnifiedSearchIndexer dummyUnifiedSearchIndexer; + + @Produces + @UnifiedSearchName + public UnifiedSearchIndexer unifiedSearchClient(final InjectionPoint injectionPoint) { + return dummyUnifiedSearchIndexer; + } +} diff --git a/example-context/example-service/example-event/example-event-indexer/src/main/resources/META-INF/beans.xml b/example-context/example-service/example-event/example-event-indexer/src/main/resources/META-INF/beans.xml new file mode 100644 index 00000000..ea4c973a --- /dev/null +++ b/example-context/example-service/example-event/example-event-indexer/src/main/resources/META-INF/beans.xml @@ -0,0 +1,6 @@ + + diff --git a/example-context/example-service/example-event/example-event-indexer/src/transformer/cake-ordered-spec.json b/example-context/example-service/example-event/example-event-indexer/src/transformer/cake-ordered-spec.json new file mode 100644 index 00000000..c8479941 --- /dev/null +++ b/example-context/example-service/example-event/example-event-indexer/src/transformer/cake-ordered-spec.json @@ -0,0 +1,12 @@ +{ + "operations": [ + { + "operation": "shift", + "spec": { + "cakeId": "cakeId", + "recipeId": "recipeId", + "deliveryDate": "deliveryDate" + } + } + ] +} diff --git a/example-context/example-service/example-event/example-event-indexer/src/yaml/event-sources.yaml b/example-context/example-service/example-event/example-event-indexer/src/yaml/event-sources.yaml new file mode 100644 index 00000000..17036182 --- /dev/null +++ b/example-context/example-service/example-event/example-event-indexer/src/yaml/event-sources.yaml @@ -0,0 +1,6 @@ +event_sources: +- name: indexer.event.source + location: + jms_uri: jms:topic:example.event + rest_uri: http://localhost:8080/example/event-source-api/rest + data_source: java:/app/example-single/DS.eventstore diff --git a/example-context/example-service/example-event/example-event-indexer/src/yaml/subscriptions-descriptor.yaml b/example-context/example-service/example-event/example-event-indexer/src/yaml/subscriptions-descriptor.yaml new file mode 100644 index 00000000..7714ebb6 --- /dev/null +++ b/example-context/example-service/example-event/example-event-indexer/src/yaml/subscriptions-descriptor.yaml @@ -0,0 +1,12 @@ +subscriptions_descriptor: + spec_version: 1.0.0 + service: example + service_component: EVENT_INDEXER + subscriptions: + - name: event indexer subscription + events: + - name: example.events.cake-ordered + schema_uri: http://justice.gov.uk/example/event/listener/example.events.cake-ordered.json + event_source_name: indexer.event.source + + diff --git a/example-context/example-service/example-event/example-event-indexer/src/yaml/unified-search-descriptor.yaml b/example-context/example-service/example-event/example-event-indexer/src/yaml/unified-search-descriptor.yaml new file mode 100644 index 00000000..9d7139ad --- /dev/null +++ b/example-context/example-service/example-event/example-event-indexer/src/yaml/unified-search-descriptor.yaml @@ -0,0 +1,10 @@ +unified_search_descriptor: + name: example + spec_version: 1.0.0 + service: examplecontext + service_component: EVENT_INDEXER + events: + - name: example.events.cake-ordered + transformer_config: cake-ordered-spec.json + index_name: crime_case_index +... \ No newline at end of file diff --git a/example-context/example-service/example-event/pom.xml b/example-context/example-service/example-event/pom.xml index 3bb867d6..4d0244f3 100644 --- a/example-context/example-service/example-event/pom.xml +++ b/example-context/example-service/example-event/pom.xml @@ -16,6 +16,7 @@ example-event-listener other-event-listener example-event-processor + example-event-indexer @@ -68,9 +69,81 @@ + + copy-transformer-resources + validate + + copy-resources + + + + ${basedir}/target/generated-resources/transformer + + + + src/transformer + true + + + + + + uk.gov.justice.framework-generators + unifiedsearch-client-generator-plugin + ${framework-generators.version} + + + uk.gov.justice.services.clients.unifiedsearch.generator.UnifiedSearchClientGenerator + + + uk.gov.justice.services.clients.unifiedsearch.generator.parser.UnifiedSearchDescriptorFileParserFactory + + ${basedir}/src/yaml + ${project.build.directory}/generated-sources + + uk.gov.justice.api + + + + generate-for-indexer + + generate-unifiedsearch-client + + generate-sources + + + **/unified-search-descriptor.yaml + + + EVENT_INDEXER + + + + + + + unifiedsearch-client-generator + uk.gov.justice.framework-generators + ${framework-generators.version} + + + + uk.gov.justice.framework-generators + generators-commons + ${framework-generators.version} + + + javax + javaee-api + ${javaee-api.version} + runtime + + + + generator-plugin uk.gov.justice.maven.generator diff --git a/example-context/example-service/example-it/pom.xml b/example-context/example-service/example-it/pom.xml index 58a8a0ab..b51b6104 100644 --- a/example-context/example-service/example-it/pom.xml +++ b/example-context/example-service/example-it/pom.xml @@ -556,6 +556,12 @@ ${project.version} war + + uk.gov.justice.services.example + example-event-indexer + ${project.version} + war + uk.gov.justice.services.example other-event-listener @@ -606,6 +612,13 @@ war example-event-listener.war + + uk.gov.justice.services.example + example-event-indexer + ${project.version} + war + example-event-indexer.war + uk.gov.justice.services.example example-event-listenerb diff --git a/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/eventsourcing/repository/jdbc/event/LinkedEventRepositoryTruncator.java b/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/eventsourcing/repository/jdbc/event/LinkedEventRepositoryTruncator.java new file mode 100644 index 00000000..e69de29b diff --git a/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/CakeShopTimeStampIT.java b/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/CakeShopTimeStampIT.java index a6a1f614..c5850e6a 100644 --- a/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/CakeShopTimeStampIT.java +++ b/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/CakeShopTimeStampIT.java @@ -1,6 +1,7 @@ package uk.gov.justice.services.example.cakeshop.it; import static com.jayway.awaitility.Awaitility.await; +import static com.jayway.awaitility.Duration.*; import static com.jayway.jsonassert.JsonAssert.with; import static java.time.temporal.ChronoUnit.SECONDS; import static java.util.UUID.randomUUID; @@ -33,10 +34,12 @@ import javax.ws.rs.client.Client; import javax.ws.rs.core.Response; +import com.jayway.awaitility.Duration; import org.junit.After; import org.junit.Before; import org.junit.Test; + public class CakeShopTimeStampIT { private final DataSource eventStoreDataSource = new DatabaseManager().initEventStoreDb(); diff --git a/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/UnifiedSearchIndexerIT.java b/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/UnifiedSearchIndexerIT.java new file mode 100644 index 00000000..26503626 --- /dev/null +++ b/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/UnifiedSearchIndexerIT.java @@ -0,0 +1,72 @@ +package uk.gov.justice.services.example.cakeshop.it; + +import static com.jayway.awaitility.Awaitility.await; +import static com.jayway.jsonassert.JsonAssert.with; +import static java.util.UUID.randomUUID; +import static javax.json.Json.createObjectBuilder; +import static javax.ws.rs.client.Entity.entity; +import static javax.ws.rs.core.Response.Status.ACCEPTED; +import static javax.ws.rs.core.Response.Status.OK; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; +import static uk.gov.justice.services.example.cakeshop.it.params.CakeShopMediaTypes.ORDER_CAKE_MEDIA_TYPE; +import static uk.gov.justice.services.example.cakeshop.it.params.CakeShopUris.ORDERS_RESOURCE_URI; +import static uk.gov.justice.services.test.utils.core.matchers.HttpStatusCodeMatcher.isStatus; + +import uk.gov.justice.services.example.cakeshop.it.helpers.ApiResponse; +import uk.gov.justice.services.example.cakeshop.it.helpers.Querier; +import uk.gov.justice.services.example.cakeshop.it.helpers.RestEasyClientFactory; + +import java.util.UUID; + +import javax.ws.rs.client.Client; +import javax.ws.rs.core.Response; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +public class UnifiedSearchIndexerIT { + + private Client client; + private Querier querier; + + @Before + public void before() throws Exception { + client = new RestEasyClientFactory().createResteasyClient(); + querier = new Querier(client); + } + + @After + public void cleanup() throws Exception { + client.close(); + } + + @Test + public void shouldIndexData() { + + final UUID recipeId = randomUUID(); + + final Response commandResponse = + client.target(ORDERS_RESOURCE_URI + randomUUID().toString()).request() + .post(entity( + createObjectBuilder() + .add("recipeId", recipeId.toString()) + .add("deliveryDate", "2016-01-21T23:42:03.522+07:00") + .build().toString(), + ORDER_CAKE_MEDIA_TYPE)); + + assertThat(commandResponse.getStatus(), isStatus(ACCEPTED)); + + await().until(() -> querier.queryForIndex(recipeId.toString()).httpCode() == OK.getStatusCode()); + + final ApiResponse queryResponse = querier.queryForIndex(recipeId.toString()); + + System.out.println(queryResponse.body()); + + with(queryResponse.body()) + .assertThat("$.indexId", equalTo(recipeId.toString())) + .assertThat("$.deliveryDate", equalTo("2016-01-21T16:42:03.522Z")); + } +} diff --git a/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/helpers/Querier.java b/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/helpers/Querier.java index c14c52ed..9150e94c 100644 --- a/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/helpers/Querier.java +++ b/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/helpers/Querier.java @@ -6,10 +6,12 @@ import static org.hamcrest.core.Is.is; import static org.junit.Assert.fail; import static uk.gov.justice.services.example.cakeshop.it.params.CakeShopMediaTypes.QUERY_CAKES_MEDIA_TYPE; +import static uk.gov.justice.services.example.cakeshop.it.params.CakeShopMediaTypes.QUERY_INDEX_MEDIA_TYPE; import static uk.gov.justice.services.example.cakeshop.it.params.CakeShopMediaTypes.QUERY_ORDER_MEDIA_TYPE; import static uk.gov.justice.services.example.cakeshop.it.params.CakeShopMediaTypes.QUERY_RECIPES_MEDIA_TYPE; import static uk.gov.justice.services.example.cakeshop.it.params.CakeShopMediaTypes.QUERY_RECIPE_MEDIA_TYPE; import static uk.gov.justice.services.example.cakeshop.it.params.CakeShopUris.CAKES_RESOURCE_QUERY_URI; +import static uk.gov.justice.services.example.cakeshop.it.params.CakeShopUris.INDEXES_RESOURCE_QUERY_URI; import static uk.gov.justice.services.example.cakeshop.it.params.CakeShopUris.ORDERS_RESOURCE_QUERY_URI; import static uk.gov.justice.services.example.cakeshop.it.params.CakeShopUris.RECIPES_RESOURCE_QUERY_URI; @@ -41,6 +43,10 @@ public ApiResponse queryForOrder(final String orderId) { return ApiResponse.from(jaxrsResponse); } + public ApiResponse queryForIndex(final String recipeId) { + final Response jaxrsResponse = client.target(INDEXES_RESOURCE_QUERY_URI + recipeId).request().accept(QUERY_INDEX_MEDIA_TYPE).get(); + return ApiResponse.from(jaxrsResponse); + } public ApiResponse recipesQueryResult() { return recipesQueryResult(singletonList(new BasicNameValuePair("pagesize", "50"))); } diff --git a/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/params/CakeShopMediaTypes.java b/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/params/CakeShopMediaTypes.java index f7ec092e..5df1c727 100644 --- a/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/params/CakeShopMediaTypes.java +++ b/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/params/CakeShopMediaTypes.java @@ -20,4 +20,5 @@ public class CakeShopMediaTypes { public static final String QUERY_CAKES_MEDIA_TYPE = PREFIX + "cakes+json"; public static final String QUERY_ORDER_MEDIA_TYPE = PREFIX + "order+json"; public static final String POST_RECIPES_QUERY_MEDIA_TYPE = PREFIX + "query-recipes+json"; + public static final String QUERY_INDEX_MEDIA_TYPE = PREFIX + "index+json"; } diff --git a/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/params/CakeShopUris.java b/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/params/CakeShopUris.java index 8a626f5d..2cc1d7d9 100644 --- a/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/params/CakeShopUris.java +++ b/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/params/CakeShopUris.java @@ -12,6 +12,8 @@ public class CakeShopUris { public static final String ORDERS_RESOURCE_QUERY_URI = HOST + "/example-query-api/query/api/rest/cakeshop/orders/"; public static final String CAKES_RESOURCE_QUERY_URI = HOST + "/example-query-api/query/api/rest/cakeshop/cakes/"; public static final String OVEN_RESOURCE_CUSTOM_URI = HOST + "/example-custom-api/custom/api/rest/cakeshop/ovens/"; + public static final String INDEXES_RESOURCE_QUERY_URI = HOST + "/example-query-api/query/api/rest/cakeshop/index/"; + public static final String CAKES_RESOURCE_URI_FORMAT = RECIPES_RESOURCE_URI + "%s/cakes/%s"; } diff --git a/example-context/example-service/example-it/src/test/resources/wildfly-config/standalone-single.xml b/example-context/example-service/example-it/src/test/resources/wildfly-config/standalone-single.xml index 81d1735e..bd78b5ab 100644 --- a/example-context/example-service/example-it/src/test/resources/wildfly-config/standalone-single.xml +++ b/example-context/example-service/example-it/src/test/resources/wildfly-config/standalone-single.xml @@ -510,7 +510,7 @@ - + diff --git a/example-context/example-service/example-liquibase/src/main/resources/liquibase/view-store-db-changesets/007-index-table.changelog.xml b/example-context/example-service/example-liquibase/src/main/resources/liquibase/view-store-db-changesets/007-index-table.changelog.xml new file mode 100644 index 00000000..25558409 --- /dev/null +++ b/example-context/example-service/example-liquibase/src/main/resources/liquibase/view-store-db-changesets/007-index-table.changelog.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/example-context/example-service/example-query/example-query-api/src/main/java/uk/gov/justice/services/example/cakeshop/query/api/IndexQueryApi.java b/example-context/example-service/example-query/example-query-api/src/main/java/uk/gov/justice/services/example/cakeshop/query/api/IndexQueryApi.java new file mode 100644 index 00000000..9bc22239 --- /dev/null +++ b/example-context/example-service/example-query/example-query-api/src/main/java/uk/gov/justice/services/example/cakeshop/query/api/IndexQueryApi.java @@ -0,0 +1,24 @@ +package uk.gov.justice.services.example.cakeshop.query.api; + +import static uk.gov.justice.services.core.annotation.Component.QUERY_API; + +import uk.gov.justice.services.core.annotation.Handles; +import uk.gov.justice.services.core.annotation.ServiceComponent; +import uk.gov.justice.services.core.requester.Requester; +import uk.gov.justice.services.example.cakeshop.query.api.request.SearchIndex; +import uk.gov.justice.services.example.cakeshop.query.api.response.IndexView; +import uk.gov.justice.services.messaging.Envelope; + +import javax.inject.Inject; + +@ServiceComponent(QUERY_API) +public class IndexQueryApi { + + @Inject + private Requester requester; + + @Handles("example.get-index") + public Envelope getIndex(final Envelope query) { + return requester.request(query, IndexView.class); + } +} diff --git a/example-context/example-service/example-query/example-query-api/src/main/java/uk/gov/justice/services/example/cakeshop/query/api/request/SearchIndex.java b/example-context/example-service/example-query/example-query-api/src/main/java/uk/gov/justice/services/example/cakeshop/query/api/request/SearchIndex.java new file mode 100644 index 00000000..9a01a505 --- /dev/null +++ b/example-context/example-service/example-query/example-query-api/src/main/java/uk/gov/justice/services/example/cakeshop/query/api/request/SearchIndex.java @@ -0,0 +1,24 @@ +package uk.gov.justice.services.example.cakeshop.query.api.request; + +import java.time.ZonedDateTime; +import java.util.UUID; + +public class SearchIndex { + + private UUID indexId; + private ZonedDateTime deliveryDate; + + public SearchIndex(final UUID indexId, final ZonedDateTime deliveryDate) { + this.indexId = indexId; + this.deliveryDate = deliveryDate; + } + + + public UUID getIndexId() { + return indexId; + } + + public ZonedDateTime getDeliveryDate() { + return deliveryDate; + } +} diff --git a/example-context/example-service/example-query/example-query-api/src/main/java/uk/gov/justice/services/example/cakeshop/query/api/response/IndexView.java b/example-context/example-service/example-query/example-query-api/src/main/java/uk/gov/justice/services/example/cakeshop/query/api/response/IndexView.java new file mode 100644 index 00000000..5a0c5a7b --- /dev/null +++ b/example-context/example-service/example-query/example-query-api/src/main/java/uk/gov/justice/services/example/cakeshop/query/api/response/IndexView.java @@ -0,0 +1,23 @@ +package uk.gov.justice.services.example.cakeshop.query.api.response; + +import java.time.ZonedDateTime; +import java.util.UUID; + +public class IndexView { + + private UUID indexId; + private ZonedDateTime deliveryDate; + + public IndexView(final UUID indexId, final ZonedDateTime deliveryDate) { + this.indexId = indexId; + this.deliveryDate = deliveryDate; + } + + public UUID getIndexId() { + return indexId; + } + + public ZonedDateTime getDeliveryDate() { + return deliveryDate; + } +} diff --git a/example-context/example-service/example-query/example-query-api/src/raml/example-query-api.raml b/example-context/example-service/example-query/example-query-api/src/raml/example-query-api.raml index bb03cc37..91de8cbf 100644 --- a/example-context/example-service/example-query/example-query-api/src/raml/example-query-api.raml +++ b/example-context/example-service/example-query/example-query-api/src/raml/example-query-api.raml @@ -110,4 +110,20 @@ protocols: [ HTTP, HTTPS ] responses: 200: body: - application/octet-stream: \ No newline at end of file + application/octet-stream: + +/index/{indexId}: + description: return a index + get: + description: | + ... + (mapping): + responseType: application/vnd.example.index+json + name: example.get-index + ... + responses: + 200: + body: + application/vnd.example.index+json: + schema: !include json/schema/example.search-index.json + example: !include json/index.json \ No newline at end of file diff --git a/example-context/example-service/example-query/example-query-api/src/raml/json/index.json b/example-context/example-service/example-query/example-query-api/src/raml/json/index.json new file mode 100644 index 00000000..784a4188 --- /dev/null +++ b/example-context/example-service/example-query/example-query-api/src/raml/json/index.json @@ -0,0 +1,4 @@ +{ + "indexId": "79d0c503-052f-4105-9b05-b49d9c4cf6a2", + "deliveryDate":"2016-07-25T23:09:01.795+07:00" +} \ No newline at end of file diff --git a/example-context/example-service/example-query/example-query-api/src/raml/json/schema/example.search-index.json b/example-context/example-service/example-query/example-query-api/src/raml/json/schema/example.search-index.json new file mode 100644 index 00000000..6cfd1814 --- /dev/null +++ b/example-context/example-service/example-query/example-query-api/src/raml/json/schema/example.search-index.json @@ -0,0 +1,17 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "http://justice.gov.uk/example/query/example.search-index.json", + "type": "object", + "properties": { + "indexId": { + "type": "string" + }, + "deliveryDate": { + "type": "string" + } + }, + "required": [ + "indexId", + "deliveryDate" + ] +} diff --git a/example-context/example-service/example-query/example-query-api/src/test/java/uk/gov/justice/services/example/cakeshop/query/api/IndexQueryApiTest.java b/example-context/example-service/example-query/example-query-api/src/test/java/uk/gov/justice/services/example/cakeshop/query/api/IndexQueryApiTest.java new file mode 100644 index 00000000..22ee0249 --- /dev/null +++ b/example-context/example-service/example-query/example-query-api/src/test/java/uk/gov/justice/services/example/cakeshop/query/api/IndexQueryApiTest.java @@ -0,0 +1,21 @@ +package uk.gov.justice.services.example.cakeshop.query.api; + +import static org.junit.Assert.*; +import static uk.gov.justice.services.core.annotation.Component.QUERY_API; +import static uk.gov.justice.services.test.utils.core.matchers.HandlerClassMatcher.isHandlerClass; +import static uk.gov.justice.services.test.utils.core.matchers.HandlerMethodMatcher.method; + +import org.junit.Test; + +public class IndexQueryApiTest { + + @Test + public void shouldHandleIndexQuery() throws Exception { + assertThat(IndexQueryApi.class, isHandlerClass(QUERY_API) + .with(method("getIndex") + .thatHandles("example.get-index") + .withRequesterPassThrough())); + } + + +} \ No newline at end of file diff --git a/example-context/example-service/example-query/example-query-view/src/main/java/uk/gov/justice/services/example/cakeshop/query/view/IndexQueryView.java b/example-context/example-service/example-query/example-query-view/src/main/java/uk/gov/justice/services/example/cakeshop/query/view/IndexQueryView.java new file mode 100644 index 00000000..d85a8225 --- /dev/null +++ b/example-context/example-service/example-query/example-query-view/src/main/java/uk/gov/justice/services/example/cakeshop/query/view/IndexQueryView.java @@ -0,0 +1,29 @@ +package uk.gov.justice.services.example.cakeshop.query.view; + +import static uk.gov.justice.services.core.enveloper.Enveloper.envelop; + +import uk.gov.justice.services.core.annotation.Component; +import uk.gov.justice.services.core.annotation.Handles; +import uk.gov.justice.services.core.annotation.ServiceComponent; +import uk.gov.justice.services.example.cakeshop.query.view.request.SearchIndex; +import uk.gov.justice.services.example.cakeshop.query.view.response.IndexView; +import uk.gov.justice.services.example.cakeshop.query.view.service.IndexService; +import uk.gov.justice.services.messaging.Envelope; + +import javax.inject.Inject; + +@ServiceComponent(Component.QUERY_VIEW) +public class IndexQueryView { + + @Inject + private IndexService indexService; + + @Handles("example.get-index") + public Envelope findIndex(final Envelope query) { + final String indexId = query.payload().getIndexId().toString(); + + return envelop(indexService.findIndexBy(indexId)) + .withName("example.get-index") + .withMetadataFrom(query); + } +} diff --git a/example-context/example-service/example-query/example-query-view/src/main/java/uk/gov/justice/services/example/cakeshop/query/view/request/SearchIndex.java b/example-context/example-service/example-query/example-query-view/src/main/java/uk/gov/justice/services/example/cakeshop/query/view/request/SearchIndex.java new file mode 100644 index 00000000..54426dc4 --- /dev/null +++ b/example-context/example-service/example-query/example-query-view/src/main/java/uk/gov/justice/services/example/cakeshop/query/view/request/SearchIndex.java @@ -0,0 +1,24 @@ +package uk.gov.justice.services.example.cakeshop.query.view.request; + +import java.time.ZonedDateTime; +import java.util.UUID; + +public class SearchIndex { + + private UUID indexId; + private ZonedDateTime deliveryDate; + + public SearchIndex(final UUID indexId, final ZonedDateTime deliveryDate) { + this.indexId = indexId; + this.deliveryDate = deliveryDate; + } + + + public UUID getIndexId() { + return indexId; + } + + public ZonedDateTime getDeliveryDate() { + return deliveryDate; + } +} diff --git a/example-context/example-service/example-query/example-query-view/src/main/java/uk/gov/justice/services/example/cakeshop/query/view/response/IndexView.java b/example-context/example-service/example-query/example-query-view/src/main/java/uk/gov/justice/services/example/cakeshop/query/view/response/IndexView.java new file mode 100644 index 00000000..c1db2c88 --- /dev/null +++ b/example-context/example-service/example-query/example-query-view/src/main/java/uk/gov/justice/services/example/cakeshop/query/view/response/IndexView.java @@ -0,0 +1,24 @@ +package uk.gov.justice.services.example.cakeshop.query.view.response; + +import java.time.ZonedDateTime; +import java.util.UUID; + +public class IndexView { + + private UUID indexId; + private ZonedDateTime deliveryDate; + + public IndexView(final UUID indexId, final ZonedDateTime deliveryDate) { + this.indexId = indexId; + this.deliveryDate = deliveryDate; + } + + + public UUID getIndexId() { + return indexId; + } + + public ZonedDateTime getDeliveryDate() { + return deliveryDate; + } +} diff --git a/example-context/example-service/example-query/example-query-view/src/main/java/uk/gov/justice/services/example/cakeshop/query/view/service/IndexService.java b/example-context/example-service/example-query/example-query-view/src/main/java/uk/gov/justice/services/example/cakeshop/query/view/service/IndexService.java new file mode 100644 index 00000000..c706cc00 --- /dev/null +++ b/example-context/example-service/example-query/example-query-view/src/main/java/uk/gov/justice/services/example/cakeshop/query/view/service/IndexService.java @@ -0,0 +1,21 @@ +package uk.gov.justice.services.example.cakeshop.query.view.service; + +import uk.gov.justice.services.example.cakeshop.persistence.IndexRepository; +import uk.gov.justice.services.example.cakeshop.persistence.entity.Index; +import uk.gov.justice.services.example.cakeshop.query.view.response.IndexView; + +import java.util.UUID; + +import javax.inject.Inject; + +public class IndexService { + + @Inject + private IndexRepository indexRepository; + + public IndexView findIndexBy(final String indexId) { + final Index index = indexRepository.findBy(UUID.fromString(indexId)); + return index != null ? new IndexView(index.getIndexId(), index.getDeliveryDate()) : null; + } +} + diff --git a/example-context/example-service/example-query/example-query-view/src/raml/example-query-view.raml b/example-context/example-service/example-query/example-query-view/src/raml/example-query-view.raml index be49a4ce..2c71a29e 100644 --- a/example-context/example-service/example-query/example-query-view/src/raml/example-query-view.raml +++ b/example-context/example-service/example-query/example-query-view/src/raml/example-query-view.raml @@ -112,4 +112,20 @@ protocols: [ HTTP, HTTPS ] body: application/vnd.example.cakes+json: schema: !include json/schema/example.search-cakes.json - example: !include json/cakes.json \ No newline at end of file + example: !include json/cakes.json + +/index/{indexId}: + description: return a index + get: + description: | + ... + (mapping): + responseType: application/vnd.example.index+json + name: example.get-index + ... + responses: + 200: + body: + application/vnd.example.index+json: + schema: !include json/schema/example.search-index.json + example: !include json/index.json \ No newline at end of file diff --git a/example-context/example-service/example-query/example-query-view/src/raml/json/index.json b/example-context/example-service/example-query/example-query-view/src/raml/json/index.json new file mode 100644 index 00000000..784a4188 --- /dev/null +++ b/example-context/example-service/example-query/example-query-view/src/raml/json/index.json @@ -0,0 +1,4 @@ +{ + "indexId": "79d0c503-052f-4105-9b05-b49d9c4cf6a2", + "deliveryDate":"2016-07-25T23:09:01.795+07:00" +} \ No newline at end of file diff --git a/example-context/example-service/example-query/example-query-view/src/raml/json/schema/example.search-index.json b/example-context/example-service/example-query/example-query-view/src/raml/json/schema/example.search-index.json new file mode 100644 index 00000000..6cfd1814 --- /dev/null +++ b/example-context/example-service/example-query/example-query-view/src/raml/json/schema/example.search-index.json @@ -0,0 +1,17 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "http://justice.gov.uk/example/query/example.search-index.json", + "type": "object", + "properties": { + "indexId": { + "type": "string" + }, + "deliveryDate": { + "type": "string" + } + }, + "required": [ + "indexId", + "deliveryDate" + ] +} diff --git a/example-context/example-service/example-query/example-query-view/src/test/java/uk/gov/justice/services/example/cakeshop/query/view/IndexQueryViewTest.java b/example-context/example-service/example-query/example-query-view/src/test/java/uk/gov/justice/services/example/cakeshop/query/view/IndexQueryViewTest.java new file mode 100644 index 00000000..3006ce53 --- /dev/null +++ b/example-context/example-service/example-query/example-query-view/src/test/java/uk/gov/justice/services/example/cakeshop/query/view/IndexQueryViewTest.java @@ -0,0 +1,61 @@ +package uk.gov.justice.services.example.cakeshop.query.view; + +import static java.util.UUID.randomUUID; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.when; +import static uk.gov.justice.services.core.annotation.Component.QUERY_VIEW; +import static uk.gov.justice.services.messaging.Envelope.envelopeFrom; +import static uk.gov.justice.services.test.utils.core.matchers.HandlerMatcher.isHandler; +import static uk.gov.justice.services.test.utils.core.matchers.HandlerMethodMatcher.method; +import static uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataWithDefaults; + +import uk.gov.justice.services.example.cakeshop.query.view.request.SearchIndex; +import uk.gov.justice.services.example.cakeshop.query.view.response.IndexView; +import uk.gov.justice.services.example.cakeshop.query.view.service.IndexService; +import uk.gov.justice.services.messaging.Envelope; + +import java.time.ZonedDateTime; +import java.util.UUID; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class IndexQueryViewTest { + + @Mock + private IndexService indexService; + + @InjectMocks + private IndexQueryView indexQueryView; + + @Test + public void shouldHaveCorrectHandlerMethod() throws Exception { + assertThat(indexQueryView, isHandler(QUERY_VIEW) + .with(method("findIndex").thatHandles("example.get-index"))); + } + + @Test + public void shouldReturnPojoIndex() { + + final UUID indexId = randomUUID(); + final ZonedDateTime deliveryDate = ZonedDateTime.now(); + + + final SearchIndex searchIndex = new SearchIndex(indexId, deliveryDate); + + final Envelope query = envelopeFrom(metadataWithDefaults(), searchIndex); + + when(indexService.findIndexBy(indexId.toString())).thenReturn(new IndexView(indexId, deliveryDate)); + + final Envelope response = indexQueryView.findIndex(query); + + assertThat(response.payload().getIndexId(), equalTo(indexId)); + assertThat(response.payload().getDeliveryDate(), equalTo(deliveryDate)); + } + +} \ No newline at end of file diff --git a/example-context/example-service/example-query/example-query-view/src/test/java/uk/gov/justice/services/example/cakeshop/query/view/service/IndexServiceTest.java b/example-context/example-service/example-query/example-query-view/src/test/java/uk/gov/justice/services/example/cakeshop/query/view/service/IndexServiceTest.java new file mode 100644 index 00000000..dfdb4cfd --- /dev/null +++ b/example-context/example-service/example-query/example-query-view/src/test/java/uk/gov/justice/services/example/cakeshop/query/view/service/IndexServiceTest.java @@ -0,0 +1,57 @@ +package uk.gov.justice.services.example.cakeshop.query.view.service; + +import static java.time.ZonedDateTime.now; +import static java.util.UUID.*; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.when; + +import uk.gov.justice.services.example.cakeshop.persistence.IndexRepository; +import uk.gov.justice.services.example.cakeshop.persistence.entity.Index; +import uk.gov.justice.services.example.cakeshop.query.view.response.IndexView; + +import java.time.ZonedDateTime; +import java.util.UUID; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class IndexServiceTest { + + @Mock + private IndexRepository repository; + + @InjectMocks + private IndexService service; + + @Test + public void shouldReturnIndexView() throws Exception { + + final UUID indexId = randomUUID(); + final ZonedDateTime deliveryDate = now(); + + when(repository.findBy(indexId)).thenReturn(new Index(indexId, deliveryDate)); + + final IndexView view = service.findIndexBy(indexId.toString()); + + assertThat(view.getIndexId(), is(indexId)); + assertThat(view.getDeliveryDate(), is(deliveryDate)); + } + + @Test + public void shouldReturnNullIfIndexNotFoundInRepo() throws Exception { + + when(repository.findBy(any(UUID.class))).thenReturn(null); + + final IndexView view = service.findIndexBy(randomUUID().toString()); + + assertThat(view, nullValue()); + } + +} \ No newline at end of file diff --git a/example-context/example-service/example-single/pom.xml b/example-context/example-service/example-single/pom.xml index d7e6070c..4e6517af 100644 --- a/example-context/example-service/example-single/pom.xml +++ b/example-context/example-service/example-single/pom.xml @@ -29,6 +29,12 @@ ${project.version} classes + + ${project.groupId} + example-event-indexer + ${project.version} + classes + ${project.groupId} example-event-processor @@ -59,6 +65,7 @@ ${event-store.version} + uk.gov.justice.utils diff --git a/pom.xml b/pom.xml index 670090a4..b7b2c287 100644 --- a/pom.xml +++ b/pom.xml @@ -45,6 +45,7 @@ 1.23.0 1.18.0 1.0.1 + 1.0.4