From 74b3b38ae704a7a0047d3c0ee56806d6962f7ce5 Mon Sep 17 00:00:00 2001 From: Mark Price Date: Sat, 15 Apr 2023 21:51:39 +0100 Subject: [PATCH 01/20] Allow GCS and Pub/Sub emulators --- .../casesvc/message/UploadObjectGCS.java | 18 ++- .../casesvc/utility/PubSubEmulator.java | 27 +--- .../ons/ctp/response/casesvc/CaseCreator.java | 3 +- .../response/casesvc/PubSubTestEmulator.java | 136 ++++++++++++++++++ .../casesvc/endpoint/CaseActionEventIT.java | 4 +- .../casesvc/endpoint/CaseEndpointIT.java | 4 +- .../casesvc/endpoint/CaseIACEndpointIT.java | 4 +- .../message/CaseReceiptReceiverIT.java | 75 ---------- .../casesvc/message/TestPubSubMessage.java | 4 +- 9 files changed, 166 insertions(+), 109 deletions(-) create mode 100644 src/test/java/uk/gov/ons/ctp/response/casesvc/PubSubTestEmulator.java delete mode 100644 src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java diff --git a/src/main/java/uk/gov/ons/ctp/response/casesvc/message/UploadObjectGCS.java b/src/main/java/uk/gov/ons/ctp/response/casesvc/message/UploadObjectGCS.java index 86d47ce2e..1043b4cf2 100644 --- a/src/main/java/uk/gov/ons/ctp/response/casesvc/message/UploadObjectGCS.java +++ b/src/main/java/uk/gov/ons/ctp/response/casesvc/message/UploadObjectGCS.java @@ -2,10 +2,8 @@ import com.godaddy.logging.Logger; import com.godaddy.logging.LoggerFactory; -import com.google.cloud.storage.BlobId; -import com.google.cloud.storage.BlobInfo; -import com.google.cloud.storage.Storage; -import com.google.cloud.storage.StorageException; +import com.google.cloud.NoCredentials; +import com.google.cloud.storage.*; import java.io.File; import org.springframework.stereotype.Component; import uk.gov.ons.ctp.response.casesvc.config.AppConfig; @@ -35,6 +33,18 @@ public boolean uploadObject(String filename, String bucket, byte[] data) { Boolean isSuccess = false; log.info("file_name: " + bucketFilename + " bucket: " + bucket + ", Uploading to GCS bucket"); try { + // Override the wired Google Storage object (GCS) to use a local emulator + Storage storage = + StorageOptions.newBuilder() + .setCredentials(NoCredentials.getInstance()) + .setProjectId("local") + .setHost("http://storage-emulator:9023") + .build() + .getService(); + System.out.println( + "Storage Client initialized with storage-emulator endpoint " + + storage.getOptions().getHost()); + storage.create(blobInfo, data); isSuccess = true; log.info("file_name: " + bucketFilename + " bucket: " + bucket + ", Upload Successful!"); diff --git a/src/main/java/uk/gov/ons/ctp/response/casesvc/utility/PubSubEmulator.java b/src/main/java/uk/gov/ons/ctp/response/casesvc/utility/PubSubEmulator.java index f1dfa70f5..7dbf872ff 100644 --- a/src/main/java/uk/gov/ons/ctp/response/casesvc/utility/PubSubEmulator.java +++ b/src/main/java/uk/gov/ons/ctp/response/casesvc/utility/PubSubEmulator.java @@ -24,17 +24,18 @@ */ public class PubSubEmulator { private static final Logger log = LoggerFactory.getLogger(PubSubEmulator.class); - private static final String HOST_PORT = "localhost:18681"; + private static final String HOST_PORT = "pubsub-emulator:8681"; + // private static final String HOST_PORT = "localhost:18681"; public static final ManagedChannel CHANNEL = ManagedChannelBuilder.forTarget(HOST_PORT).usePlaintext().build(); public static final TransportChannelProvider CHANNEL_PROVIDER = FixedTransportChannelProvider.create(GrpcTransportChannel.create(CHANNEL)); public static final CredentialsProvider CREDENTIAL_PROVIDER = NoCredentialsProvider.create(); - private static final String PROJECT_ID = "ras-rm-dev"; - private static final String TOPIC_ID = "test_topic"; - private static final String CASE_CREATION_TOPIC_ID = "test_case_creation_topic"; - private static final String SUBSCRIPTION_ID = "test_subscription"; - private static final String CASE_CREATION_SUBSCRIPTION_ID = "test_case_creation_subscription"; + private static final String PROJECT_ID = "local"; + private static final String TOPIC_ID = "ras-rm-notify-local"; + private static final String CASE_CREATION_TOPIC_ID = "case-notification-local"; + private static final String SUBSCRIPTION_ID = "sdx-receipt-local"; + private static final String CASE_CREATION_SUBSCRIPTION_ID = "case-notification-local"; private final TopicAdminClient topicClient = TopicAdminClient.create( TopicAdminSettings.newBuilder() @@ -119,18 +120,4 @@ public void publishSampleCaseCreationMessage(String message) { public void shutdown() { CHANNEL.shutdown(); } - - public void testInit() { - Topic topic = topicClient.createTopic(topicName); - System.out.println("Created topic: " + topic.getName()); - Subscription subscription = - subscriptionAdminClient.createSubscription( - subscriptionName, topicName, PushConfig.getDefaultInstance(), 10); - System.out.println("Created pull subscription: " + subscription.getName()); - } - - public void testTeardown() { - subscriptionAdminClient.deleteSubscription(subscriptionName); - topicClient.deleteTopic(topicName); - } } diff --git a/src/test/java/uk/gov/ons/ctp/response/casesvc/CaseCreator.java b/src/test/java/uk/gov/ons/ctp/response/casesvc/CaseCreator.java index b6d9e768e..d6e5cb5dd 100644 --- a/src/test/java/uk/gov/ons/ctp/response/casesvc/CaseCreator.java +++ b/src/test/java/uk/gov/ons/ctp/response/casesvc/CaseCreator.java @@ -8,7 +8,6 @@ import org.springframework.core.io.ResourceLoader; import org.springframework.stereotype.Component; import uk.gov.ons.ctp.response.casesvc.message.sampleunitnotification.SampleUnitParent; -import uk.gov.ons.ctp.response.casesvc.utility.PubSubEmulator; @Component public class CaseCreator { @@ -21,7 +20,7 @@ public class CaseCreator { public void postSampleUnit( String sampleUnitRef, String sampleUnitType, UUID sampleUnitId, UUID collectionExerciseId) throws Exception { - PubSubEmulator pubSubEmulator = new PubSubEmulator(); + PubSubTestEmulator pubSubEmulator = new PubSubTestEmulator(); iacServiceStub.createIACStub(); SampleUnitParent sampleUnit = new SampleUnitParent(); diff --git a/src/test/java/uk/gov/ons/ctp/response/casesvc/PubSubTestEmulator.java b/src/test/java/uk/gov/ons/ctp/response/casesvc/PubSubTestEmulator.java new file mode 100644 index 000000000..d5b229c71 --- /dev/null +++ b/src/test/java/uk/gov/ons/ctp/response/casesvc/PubSubTestEmulator.java @@ -0,0 +1,136 @@ +package uk.gov.ons.ctp.response.casesvc; + +import com.godaddy.logging.Logger; +import com.godaddy.logging.LoggerFactory; +import com.google.api.core.ApiFuture; +import com.google.api.gax.core.CredentialsProvider; +import com.google.api.gax.core.NoCredentialsProvider; +import com.google.api.gax.grpc.GrpcTransportChannel; +import com.google.api.gax.rpc.FixedTransportChannelProvider; +import com.google.api.gax.rpc.TransportChannelProvider; +import com.google.cloud.pubsub.v1.*; +import com.google.cloud.pubsub.v1.stub.GrpcSubscriberStub; +import com.google.cloud.pubsub.v1.stub.SubscriberStubSettings; +import com.google.protobuf.ByteString; +import com.google.pubsub.v1.*; +import io.grpc.ManagedChannel; +import io.grpc.ManagedChannelBuilder; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +/** + * * This is a PubSub Emulator class. This is a utility class which is used for testing pubsub + * function + */ +public class PubSubTestEmulator { + private static final Logger log = LoggerFactory.getLogger(PubSubTestEmulator.class); + private static final String HOST_PORT = "localhost:18681"; + public static final ManagedChannel CHANNEL = + ManagedChannelBuilder.forTarget(HOST_PORT).usePlaintext().build(); + public static final TransportChannelProvider CHANNEL_PROVIDER = + FixedTransportChannelProvider.create(GrpcTransportChannel.create(CHANNEL)); + public static final CredentialsProvider CREDENTIAL_PROVIDER = NoCredentialsProvider.create(); + private static final String PROJECT_ID = "ras-rm-dev"; + private static final String TOPIC_ID = "test_topic"; + private static final String CASE_CREATION_TOPIC_ID = "test_case_creation_topic"; + private static final String SUBSCRIPTION_ID = "test_subscription"; + private static final String CASE_CREATION_SUBSCRIPTION_ID = "test_case_creation_subscription"; + private final TopicAdminClient topicClient = + TopicAdminClient.create( + TopicAdminSettings.newBuilder() + .setTransportChannelProvider(PubSubTestEmulator.CHANNEL_PROVIDER) + .setCredentialsProvider(PubSubTestEmulator.CREDENTIAL_PROVIDER) + .build()); + SubscriptionAdminClient subscriptionAdminClient = + SubscriptionAdminClient.create( + SubscriptionAdminSettings.newBuilder() + .setTransportChannelProvider(PubSubTestEmulator.CHANNEL_PROVIDER) + .setCredentialsProvider(PubSubTestEmulator.CREDENTIAL_PROVIDER) + .build()); + TopicName topicName = TopicName.of(PROJECT_ID, TOPIC_ID); + TopicName caseCreationTopicName = TopicName.of(PROJECT_ID, CASE_CREATION_TOPIC_ID); + ProjectSubscriptionName subscriptionName = + ProjectSubscriptionName.of(PROJECT_ID, SUBSCRIPTION_ID); + ProjectSubscriptionName caseCreationSubscriptionName = + ProjectSubscriptionName.of(PROJECT_ID, CASE_CREATION_SUBSCRIPTION_ID); + + public PubSubTestEmulator() throws IOException {} + + public Publisher getEmulatorPublisher(TopicName topicName) throws IOException { + return Publisher.newBuilder(topicName) + .setChannelProvider(CHANNEL_PROVIDER) + .setCredentialsProvider(CREDENTIAL_PROVIDER) + .build(); + } + + public Subscriber getEmulatorSubscriber(MessageReceiver receiver) { + return Subscriber.newBuilder(ProjectSubscriptionName.of(PROJECT_ID, SUBSCRIPTION_ID), receiver) + .setChannelProvider(CHANNEL_PROVIDER) + .setCredentialsProvider(CREDENTIAL_PROVIDER) + .build(); + } + + public Subscriber getEmulatorSubscriberForCaseActionEventStatus(MessageReceiver receiver) { + return Subscriber.newBuilder( + ProjectSubscriptionName.of(PROJECT_ID, "test_event_status_subscription"), receiver) + .setChannelProvider(CHANNEL_PROVIDER) + .setCredentialsProvider(CREDENTIAL_PROVIDER) + .build(); + } + + public GrpcSubscriberStub getEmulatorSubscriberStub() throws IOException { + return GrpcSubscriberStub.create( + SubscriberStubSettings.newBuilder() + .setTransportChannelProvider(CHANNEL_PROVIDER) + .setCredentialsProvider(CREDENTIAL_PROVIDER) + .build()); + } + + public void publishMessage(String message) { + try { + ByteString data = ByteString.copyFromUtf8(message); + PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); + TopicName topicName = TopicName.of(PROJECT_ID, TOPIC_ID); + Publisher publisher = getEmulatorPublisher(topicName); + log.with("publisher", publisher).info("Publishing message to pubsub emulator"); + ApiFuture messageIdFuture = publisher.publish(pubsubMessage); + String messageId = messageIdFuture.get(); + log.with("messageId", messageId).info("Published message to pubsub emulator"); + } catch (IOException | InterruptedException | ExecutionException e) { + log.error("Failed to publish message", e); + } + } + + public void publishSampleCaseCreationMessage(String message) { + try { + ByteString data = ByteString.copyFromUtf8(message); + PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); + TopicName topicName = TopicName.of(PROJECT_ID, CASE_CREATION_TOPIC_ID); + Publisher publisher = getEmulatorPublisher(topicName); + log.with("publisher", publisher).info("Publishing case creation message to pubsub emulator"); + ApiFuture messageIdFuture = publisher.publish(pubsubMessage); + String messageId = messageIdFuture.get(); + log.with("messageId", messageId).info("Published case creation message to pubsub emulator"); + } catch (IOException | InterruptedException | ExecutionException e) { + log.error("Failed to publish message", e); + } + } + + public void shutdown() { + CHANNEL.shutdown(); + } + + public void testInit() { + Topic topic = topicClient.createTopic(topicName); + System.out.println("Created topic: " + topic.getName()); + Subscription subscription = + subscriptionAdminClient.createSubscription( + subscriptionName, topicName, PushConfig.getDefaultInstance(), 10); + System.out.println("Created pull subscription: " + subscription.getName()); + } + + public void testTeardown() { + subscriptionAdminClient.deleteSubscription(subscriptionName); + topicClient.deleteTopic(topicName); + } +} diff --git a/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseActionEventIT.java b/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseActionEventIT.java index a94aae153..dafb2288a 100644 --- a/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseActionEventIT.java +++ b/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseActionEventIT.java @@ -27,6 +27,7 @@ import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import uk.gov.ons.ctp.response.casesvc.CaseCreator; +import uk.gov.ons.ctp.response.casesvc.PubSubTestEmulator; import uk.gov.ons.ctp.response.casesvc.client.CollectionExerciseSvcClient; import uk.gov.ons.ctp.response.casesvc.domain.model.CaseActionEventRequest; import uk.gov.ons.ctp.response.casesvc.domain.repository.CaseActionEventRequestRepository; @@ -37,7 +38,6 @@ import uk.gov.ons.ctp.response.casesvc.representation.action.CaseActionEvent; import uk.gov.ons.ctp.response.casesvc.service.action.email.ProcessEmailActionService; import uk.gov.ons.ctp.response.casesvc.service.action.letter.ProcessLetterActionService; -import uk.gov.ons.ctp.response.casesvc.utility.PubSubEmulator; import uk.gov.ons.ctp.response.lib.collection.exercise.CollectionExerciseDTO; import uk.gov.ons.ctp.response.lib.common.UnirestInitialiser; @@ -53,7 +53,7 @@ public class CaseActionEventIT { private UUID collectionExerciseId; private Map metadata; - private PubSubEmulator pubSubEmulator = new PubSubEmulator(); + private PubSubTestEmulator pubSubEmulator = new PubSubTestEmulator(); @MockBean private ProcessLetterActionService processLetterActionService; @MockBean private ProcessEmailActionService processEmailActionService; diff --git a/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseEndpointIT.java b/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseEndpointIT.java index 1856734e8..01f6826d1 100644 --- a/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseEndpointIT.java +++ b/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseEndpointIT.java @@ -25,13 +25,13 @@ import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import uk.gov.ons.ctp.response.casesvc.CaseCreator; +import uk.gov.ons.ctp.response.casesvc.PubSubTestEmulator; import uk.gov.ons.ctp.response.casesvc.client.CollectionExerciseSvcClient; import uk.gov.ons.ctp.response.casesvc.domain.repository.CaseEventRepository; import uk.gov.ons.ctp.response.casesvc.domain.repository.CaseGroupRepository; import uk.gov.ons.ctp.response.casesvc.domain.repository.CaseRepository; import uk.gov.ons.ctp.response.casesvc.representation.*; import uk.gov.ons.ctp.response.casesvc.representation.CategoryDTO.CategoryName; -import uk.gov.ons.ctp.response.casesvc.utility.PubSubEmulator; import uk.gov.ons.ctp.response.lib.collection.exercise.CollectionExerciseDTO; import uk.gov.ons.ctp.response.lib.common.UnirestInitialiser; @@ -45,7 +45,7 @@ public class CaseEndpointIT { private UUID collectionExerciseId; private Map metadata; - private PubSubEmulator pubSubEmulator = new PubSubEmulator(); + private PubSubTestEmulator pubSubEmulator = new PubSubTestEmulator(); @ClassRule public static WireMockRule wireMockRule = diff --git a/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseIACEndpointIT.java b/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseIACEndpointIT.java index efe4847a6..7163d6ce1 100644 --- a/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseIACEndpointIT.java +++ b/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseIACEndpointIT.java @@ -34,13 +34,13 @@ import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import uk.gov.ons.ctp.response.casesvc.CaseCreator; +import uk.gov.ons.ctp.response.casesvc.PubSubTestEmulator; import uk.gov.ons.ctp.response.casesvc.client.CollectionExerciseSvcClient; import uk.gov.ons.ctp.response.casesvc.domain.repository.CaseEventRepository; import uk.gov.ons.ctp.response.casesvc.domain.repository.CaseGroupRepository; import uk.gov.ons.ctp.response.casesvc.domain.repository.CaseRepository; import uk.gov.ons.ctp.response.casesvc.representation.CaseDetailsDTO; import uk.gov.ons.ctp.response.casesvc.representation.CaseIACDTO; -import uk.gov.ons.ctp.response.casesvc.utility.PubSubEmulator; import uk.gov.ons.ctp.response.lib.collection.exercise.CollectionExerciseDTO; import uk.gov.ons.ctp.response.lib.common.UnirestInitialiser; @@ -56,7 +56,7 @@ public class CaseIACEndpointIT { private Map metadata; private static final Logger log = LoggerFactory.getLogger(CaseIACEndpointIT.class); - private PubSubEmulator pubSubEmulator = new PubSubEmulator(); + private PubSubTestEmulator pubSubEmulator = new PubSubTestEmulator(); @ClassRule public static WireMockRule wireMockRule = diff --git a/src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java b/src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java deleted file mode 100644 index eb54dfaa4..000000000 --- a/src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java +++ /dev/null @@ -1,75 +0,0 @@ -package uk.gov.ons.ctp.response.casesvc.message; - -import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer; -import com.github.tomakehurst.wiremock.junit.WireMockRule; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import org.junit.*; -import org.junit.contrib.java.lang.system.EnvironmentVariables; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import uk.gov.ons.ctp.response.casesvc.message.feedback.CaseReceipt; -import uk.gov.ons.ctp.response.casesvc.utility.PubSubEmulator; - -@ContextConfiguration -@ActiveProfiles("test") -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@RunWith(SpringJUnit4ClassRunner.class) -@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) -@TestPropertySource(locations = "classpath:/application-test.yml") -public class CaseReceiptReceiverIT { - - private static PubSubEmulator PUBSUBEMULATOR; - - private String receiptFile = - "src/test/resources/uk/gov/ons/ctp/response/casesvc/message/receiptPubsub.json"; - - @ClassRule - public static final EnvironmentVariables environmentVariables = - new EnvironmentVariables().set("PUBSUB_EMULATOR_HOST", "127.0.0.1:18681"); - - @ClassRule - public static WireMockRule wireMockRule = - new WireMockRule(options().extensions(new ResponseTemplateTransformer(false)).port(18002)); - - @MockBean private CaseReceiptReceiver caseReceiptReceiver; - - public CaseReceiptReceiverIT() throws IOException {} - - @BeforeClass - public static void testSetup() throws IOException { - PUBSUBEMULATOR = new PubSubEmulator(); - PUBSUBEMULATOR.testInit(); - } - - @AfterClass - public static void testTearDown() { - PUBSUBEMULATOR.testTeardown(); - } - - @Test - public void testCaseNotificationReceiverIsReceivingMessageFromPubSub() throws Exception { - String json = readFileAsString(receiptFile); - PUBSUBEMULATOR.publishMessage(json); - Thread.sleep(2000); - ObjectMapper objectMapper = new ObjectMapper(); - CaseReceipt caseReceipt = objectMapper.readValue(json, CaseReceipt.class); - - Mockito.verify(caseReceiptReceiver, Mockito.times(1)).process(caseReceipt); - } - - private static String readFileAsString(String file) throws Exception { - return new String(Files.readAllBytes(Paths.get(file))); - } -} diff --git a/src/test/java/uk/gov/ons/ctp/response/casesvc/message/TestPubSubMessage.java b/src/test/java/uk/gov/ons/ctp/response/casesvc/message/TestPubSubMessage.java index e9c807e2e..79ee505d1 100644 --- a/src/test/java/uk/gov/ons/ctp/response/casesvc/message/TestPubSubMessage.java +++ b/src/test/java/uk/gov/ons/ctp/response/casesvc/message/TestPubSubMessage.java @@ -10,12 +10,12 @@ import java.io.IOException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import uk.gov.ons.ctp.response.casesvc.PubSubTestEmulator; import uk.gov.ons.ctp.response.casesvc.representation.action.CaseActionEvent; -import uk.gov.ons.ctp.response.casesvc.utility.PubSubEmulator; public class TestPubSubMessage { private static final Logger log = LoggerFactory.getLogger(TestPubSubMessage.class); - private final PubSubEmulator pubsubEmulator = new PubSubEmulator(); + private final PubSubTestEmulator pubsubEmulator = new PubSubTestEmulator(); private String receivedMessage = null; public TestPubSubMessage() throws IOException {} From eb3800e19591b5b88e6898fbd4d28f45ae1be2c5 Mon Sep 17 00:00:00 2001 From: Mark Price Date: Sat, 15 Apr 2023 23:29:45 +0100 Subject: [PATCH 02/20] Adding failing test --- .../message/CaseReceiptReceiverIT.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java diff --git a/src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java b/src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java new file mode 100644 index 000000000..dbfe24da3 --- /dev/null +++ b/src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java @@ -0,0 +1,70 @@ +package uk.gov.ons.ctp.response.casesvc.message; + +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer; +import com.github.tomakehurst.wiremock.junit.WireMockRule; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import org.junit.*; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import uk.gov.ons.ctp.response.casesvc.PubSubTestEmulator; +import uk.gov.ons.ctp.response.casesvc.message.feedback.CaseReceipt; + +@ContextConfiguration +@ActiveProfiles("test") +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@RunWith(SpringJUnit4ClassRunner.class) +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) +@TestPropertySource(locations = "classpath:/application-test.yml") +public class CaseReceiptReceiverIT { + + private final PubSubTestEmulator pubSubEmulator = new PubSubTestEmulator(); + + @ClassRule + public static WireMockRule wireMockRule = + new WireMockRule(options().extensions(new ResponseTemplateTransformer(false)).port(18002)); + + @MockBean private CaseReceiptReceiver caseReceiptReceiver; + + public CaseReceiptReceiverIT() throws IOException {} + + @Before + public void testSetup() { + pubSubEmulator.testInit(); + } + + @After + public void testTearDown() { + pubSubEmulator.testTeardown(); + } + + @Test + public void testCaseNotificationReceiverIsReceivingMessageFromPubSub() throws Exception { + String receiptFile = + "src/test/resources/uk/gov/ons/ctp/response/casesvc/message/receiptPubsub.json"; + String json = readFileAsString(receiptFile); + System.out.println("************PUBLISHING****************"); + pubSubEmulator.publishMessage(json); + System.out.println("^^^^^^^^^^^^PUBLISHED^^^^^^^^^^^^^^^^"); + Thread.sleep(2000); + ObjectMapper objectMapper = new ObjectMapper(); + CaseReceipt caseReceipt = objectMapper.readValue(json, CaseReceipt.class); + + Mockito.verify(caseReceiptReceiver, Mockito.times(1)).process(caseReceipt); + } + + private static String readFileAsString(String file) throws Exception { + return new String(Files.readAllBytes(Paths.get(file))); + } +} From 802e056800d8778a8dcf26faf50afd4460d640cf Mon Sep 17 00:00:00 2001 From: Mark Price Date: Sun, 16 Apr 2023 09:46:24 +0100 Subject: [PATCH 03/20] Refactored pubsub receiver tests to allow emulator to be used for app --- .../response/casesvc/PubSubTestEmulator.java | 31 ++++++----------- .../message/CaseReceiptReceiverIT.java | 25 ++++---------- .../casesvc/message/TestPubSubMessage.java | 34 ++++++++++++++++++- 3 files changed, 50 insertions(+), 40 deletions(-) diff --git a/src/test/java/uk/gov/ons/ctp/response/casesvc/PubSubTestEmulator.java b/src/test/java/uk/gov/ons/ctp/response/casesvc/PubSubTestEmulator.java index d5b229c71..81633515d 100644 --- a/src/test/java/uk/gov/ons/ctp/response/casesvc/PubSubTestEmulator.java +++ b/src/test/java/uk/gov/ons/ctp/response/casesvc/PubSubTestEmulator.java @@ -18,10 +18,7 @@ import java.io.IOException; import java.util.concurrent.ExecutionException; -/** - * * This is a PubSub Emulator class. This is a utility class which is used for testing pubsub - * function - */ +/** * This is a PubSub Emulator class which is used for testing pubsub function */ public class PubSubTestEmulator { private static final Logger log = LoggerFactory.getLogger(PubSubTestEmulator.class); private static final String HOST_PORT = "localhost:18681"; @@ -31,9 +28,9 @@ public class PubSubTestEmulator { FixedTransportChannelProvider.create(GrpcTransportChannel.create(CHANNEL)); public static final CredentialsProvider CREDENTIAL_PROVIDER = NoCredentialsProvider.create(); private static final String PROJECT_ID = "ras-rm-dev"; - private static final String TOPIC_ID = "test_topic"; + private static final String RECEIPT_TOPIC_ID = "test_receipt_topic"; private static final String CASE_CREATION_TOPIC_ID = "test_case_creation_topic"; - private static final String SUBSCRIPTION_ID = "test_subscription"; + private static final String RECEIPT_SUBSCRIPTION_ID = "test_receipt_subscription"; private static final String CASE_CREATION_SUBSCRIPTION_ID = "test_case_creation_subscription"; private final TopicAdminClient topicClient = TopicAdminClient.create( @@ -47,10 +44,10 @@ public class PubSubTestEmulator { .setTransportChannelProvider(PubSubTestEmulator.CHANNEL_PROVIDER) .setCredentialsProvider(PubSubTestEmulator.CREDENTIAL_PROVIDER) .build()); - TopicName topicName = TopicName.of(PROJECT_ID, TOPIC_ID); + TopicName topicName = TopicName.of(PROJECT_ID, RECEIPT_TOPIC_ID); TopicName caseCreationTopicName = TopicName.of(PROJECT_ID, CASE_CREATION_TOPIC_ID); ProjectSubscriptionName subscriptionName = - ProjectSubscriptionName.of(PROJECT_ID, SUBSCRIPTION_ID); + ProjectSubscriptionName.of(PROJECT_ID, RECEIPT_SUBSCRIPTION_ID); ProjectSubscriptionName caseCreationSubscriptionName = ProjectSubscriptionName.of(PROJECT_ID, CASE_CREATION_SUBSCRIPTION_ID); @@ -63,16 +60,8 @@ public Publisher getEmulatorPublisher(TopicName topicName) throws IOException { .build(); } - public Subscriber getEmulatorSubscriber(MessageReceiver receiver) { - return Subscriber.newBuilder(ProjectSubscriptionName.of(PROJECT_ID, SUBSCRIPTION_ID), receiver) - .setChannelProvider(CHANNEL_PROVIDER) - .setCredentialsProvider(CREDENTIAL_PROVIDER) - .build(); - } - - public Subscriber getEmulatorSubscriberForCaseActionEventStatus(MessageReceiver receiver) { - return Subscriber.newBuilder( - ProjectSubscriptionName.of(PROJECT_ID, "test_event_status_subscription"), receiver) + public Subscriber getEmulatorSubscriber(MessageReceiver receiver, String subscription) { + return Subscriber.newBuilder(ProjectSubscriptionName.of(PROJECT_ID, subscription), receiver) .setChannelProvider(CHANNEL_PROVIDER) .setCredentialsProvider(CREDENTIAL_PROVIDER) .build(); @@ -90,7 +79,7 @@ public void publishMessage(String message) { try { ByteString data = ByteString.copyFromUtf8(message); PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); - TopicName topicName = TopicName.of(PROJECT_ID, TOPIC_ID); + TopicName topicName = TopicName.of(PROJECT_ID, RECEIPT_TOPIC_ID); Publisher publisher = getEmulatorPublisher(topicName); log.with("publisher", publisher).info("Publishing message to pubsub emulator"); ApiFuture messageIdFuture = publisher.publish(pubsubMessage); @@ -122,11 +111,11 @@ public void shutdown() { public void testInit() { Topic topic = topicClient.createTopic(topicName); - System.out.println("Created topic: " + topic.getName()); + log.info("Created topic: " + topic.getName()); Subscription subscription = subscriptionAdminClient.createSubscription( subscriptionName, topicName, PushConfig.getDefaultInstance(), 10); - System.out.println("Created pull subscription: " + subscription.getName()); + log.info("Created pull subscription: " + subscription.getName()); } public void testTeardown() { diff --git a/src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java b/src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java index dbfe24da3..2d66ef2c2 100644 --- a/src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java +++ b/src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java @@ -1,18 +1,11 @@ package uk.gov.ons.ctp.response.casesvc.message; -import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer; -import com.github.tomakehurst.wiremock.junit.WireMockRule; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import org.junit.*; import org.junit.runner.RunWith; -import org.mockito.Mockito; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; @@ -31,12 +24,6 @@ public class CaseReceiptReceiverIT { private final PubSubTestEmulator pubSubEmulator = new PubSubTestEmulator(); - @ClassRule - public static WireMockRule wireMockRule = - new WireMockRule(options().extensions(new ResponseTemplateTransformer(false)).port(18002)); - - @MockBean private CaseReceiptReceiver caseReceiptReceiver; - public CaseReceiptReceiverIT() throws IOException {} @Before @@ -51,17 +38,19 @@ public void testTearDown() { @Test public void testCaseNotificationReceiverIsReceivingMessageFromPubSub() throws Exception { + // Publish a test receipt String receiptFile = "src/test/resources/uk/gov/ons/ctp/response/casesvc/message/receiptPubsub.json"; String json = readFileAsString(receiptFile); - System.out.println("************PUBLISHING****************"); pubSubEmulator.publishMessage(json); - System.out.println("^^^^^^^^^^^^PUBLISHED^^^^^^^^^^^^^^^^"); + + // Call the receiver to get the receipt message + TestPubSubMessage message = new TestPubSubMessage(); Thread.sleep(2000); - ObjectMapper objectMapper = new ObjectMapper(); - CaseReceipt caseReceipt = objectMapper.readValue(json, CaseReceipt.class); + CaseReceipt caseReceipt = message.getCaseReceipt(); - Mockito.verify(caseReceiptReceiver, Mockito.times(1)).process(caseReceipt); + // Test the case ID of the receipt received is the one we published + Assert.assertEquals(caseReceipt.getCaseId(), "9b872c6c-3339-4db9-9ef6-ff37a4446321"); } private static String readFileAsString(String file) throws Exception { diff --git a/src/test/java/uk/gov/ons/ctp/response/casesvc/message/TestPubSubMessage.java b/src/test/java/uk/gov/ons/ctp/response/casesvc/message/TestPubSubMessage.java index 79ee505d1..2df59ef00 100644 --- a/src/test/java/uk/gov/ons/ctp/response/casesvc/message/TestPubSubMessage.java +++ b/src/test/java/uk/gov/ons/ctp/response/casesvc/message/TestPubSubMessage.java @@ -11,6 +11,7 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import uk.gov.ons.ctp.response.casesvc.PubSubTestEmulator; +import uk.gov.ons.ctp.response.casesvc.message.feedback.CaseReceipt; import uk.gov.ons.ctp.response.casesvc.representation.action.CaseActionEvent; public class TestPubSubMessage { @@ -31,7 +32,8 @@ public CaseActionEvent getPubSubCaseActionEventStatus() throws IOException { log.info("Received : " + receivedMessage); consumer.ack(); }; - Subscriber subscriber = pubsubEmulator.getEmulatorSubscriberForCaseActionEventStatus(receiver); + Subscriber subscriber = + pubsubEmulator.getEmulatorSubscriber(receiver, "test_event_status_subscription"); Thread t = new Thread(() -> subscriber.startAsync().awaitRunning()); ExecutorService service = Executors.newSingleThreadExecutor(); service.execute(t); @@ -48,4 +50,34 @@ public CaseActionEvent getPubSubCaseActionEventStatus() throws IOException { ObjectMapper objectMapper = new ObjectMapper(); return objectMapper.readValue(receivedMessage, CaseActionEvent.class); } + + public CaseReceipt getCaseReceipt() throws IOException { + + MessageReceiver receiver = + (PubsubMessage message, AckReplyConsumer consumer) -> { + // Handle incoming message, then ack the received message. + log.info("Id: " + message.getMessageId()); + log.info("Data: " + message.getData().toStringUtf8()); + receivedMessage = message.getData().toStringUtf8(); + log.info("Received : " + receivedMessage); + consumer.ack(); + }; + Subscriber subscriber = + pubsubEmulator.getEmulatorSubscriber(receiver, "test_receipt_subscription"); + Thread t = new Thread(() -> subscriber.startAsync().awaitRunning()); + ExecutorService service = Executors.newSingleThreadExecutor(); + service.execute(t); + while (receivedMessage == null) { + try { + Thread.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + subscriber.stopAsync().awaitTerminated(); + service.shutdown(); + System.out.println(receivedMessage); + ObjectMapper objectMapper = new ObjectMapper(); + return objectMapper.readValue(receivedMessage, CaseReceipt.class); + } } From feae4d11ac170b61c84796d9439919203bfb1c6c Mon Sep 17 00:00:00 2001 From: ras-rm-pr-bot Date: Sun, 16 Apr 2023 09:02:33 +0000 Subject: [PATCH 04/20] auto patch increment --- _infra/helm/case/Chart.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_infra/helm/case/Chart.yaml b/_infra/helm/case/Chart.yaml index 8d86e59c4..1f2619ffb 100644 --- a/_infra/helm/case/Chart.yaml +++ b/_infra/helm/case/Chart.yaml @@ -14,8 +14,8 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. -version: 12.0.6 +version: 12.0.7 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. -appVersion: 12.0.6 +appVersion: 12.0.7 From 7f5de81ff19b2a5cfa81f6908cf991bc997ee787 Mon Sep 17 00:00:00 2001 From: ras-rm-pr-bot Date: Wed, 17 May 2023 07:25:56 +0000 Subject: [PATCH 05/20] auto patch increment --- _infra/helm/case/Chart.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_infra/helm/case/Chart.yaml b/_infra/helm/case/Chart.yaml index 1f2619ffb..17efe8aa1 100644 --- a/_infra/helm/case/Chart.yaml +++ b/_infra/helm/case/Chart.yaml @@ -14,8 +14,8 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. -version: 12.0.7 +version: 12.0.8 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. -appVersion: 12.0.7 +appVersion: 12.0.8 From f5bf8fce37089df42478a2cf2e98817c8c04e326 Mon Sep 17 00:00:00 2001 From: Mark Price Date: Sat, 15 Apr 2023 21:51:39 +0100 Subject: [PATCH 06/20] Allow GCS and Pub/Sub emulators --- .../casesvc/message/UploadObjectGCS.java | 18 ++- .../casesvc/utility/PubSubEmulator.java | 27 +--- .../ons/ctp/response/casesvc/CaseCreator.java | 3 +- .../response/casesvc/PubSubTestEmulator.java | 136 ++++++++++++++++++ .../casesvc/endpoint/CaseActionEventIT.java | 4 +- .../casesvc/endpoint/CaseEndpointIT.java | 4 +- .../casesvc/endpoint/CaseIACEndpointIT.java | 4 +- .../message/CaseReceiptReceiverIT.java | 75 ---------- .../casesvc/message/TestPubSubMessage.java | 4 +- 9 files changed, 166 insertions(+), 109 deletions(-) create mode 100644 src/test/java/uk/gov/ons/ctp/response/casesvc/PubSubTestEmulator.java delete mode 100644 src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java diff --git a/src/main/java/uk/gov/ons/ctp/response/casesvc/message/UploadObjectGCS.java b/src/main/java/uk/gov/ons/ctp/response/casesvc/message/UploadObjectGCS.java index 86d47ce2e..1043b4cf2 100644 --- a/src/main/java/uk/gov/ons/ctp/response/casesvc/message/UploadObjectGCS.java +++ b/src/main/java/uk/gov/ons/ctp/response/casesvc/message/UploadObjectGCS.java @@ -2,10 +2,8 @@ import com.godaddy.logging.Logger; import com.godaddy.logging.LoggerFactory; -import com.google.cloud.storage.BlobId; -import com.google.cloud.storage.BlobInfo; -import com.google.cloud.storage.Storage; -import com.google.cloud.storage.StorageException; +import com.google.cloud.NoCredentials; +import com.google.cloud.storage.*; import java.io.File; import org.springframework.stereotype.Component; import uk.gov.ons.ctp.response.casesvc.config.AppConfig; @@ -35,6 +33,18 @@ public boolean uploadObject(String filename, String bucket, byte[] data) { Boolean isSuccess = false; log.info("file_name: " + bucketFilename + " bucket: " + bucket + ", Uploading to GCS bucket"); try { + // Override the wired Google Storage object (GCS) to use a local emulator + Storage storage = + StorageOptions.newBuilder() + .setCredentials(NoCredentials.getInstance()) + .setProjectId("local") + .setHost("http://storage-emulator:9023") + .build() + .getService(); + System.out.println( + "Storage Client initialized with storage-emulator endpoint " + + storage.getOptions().getHost()); + storage.create(blobInfo, data); isSuccess = true; log.info("file_name: " + bucketFilename + " bucket: " + bucket + ", Upload Successful!"); diff --git a/src/main/java/uk/gov/ons/ctp/response/casesvc/utility/PubSubEmulator.java b/src/main/java/uk/gov/ons/ctp/response/casesvc/utility/PubSubEmulator.java index f1dfa70f5..7dbf872ff 100644 --- a/src/main/java/uk/gov/ons/ctp/response/casesvc/utility/PubSubEmulator.java +++ b/src/main/java/uk/gov/ons/ctp/response/casesvc/utility/PubSubEmulator.java @@ -24,17 +24,18 @@ */ public class PubSubEmulator { private static final Logger log = LoggerFactory.getLogger(PubSubEmulator.class); - private static final String HOST_PORT = "localhost:18681"; + private static final String HOST_PORT = "pubsub-emulator:8681"; + // private static final String HOST_PORT = "localhost:18681"; public static final ManagedChannel CHANNEL = ManagedChannelBuilder.forTarget(HOST_PORT).usePlaintext().build(); public static final TransportChannelProvider CHANNEL_PROVIDER = FixedTransportChannelProvider.create(GrpcTransportChannel.create(CHANNEL)); public static final CredentialsProvider CREDENTIAL_PROVIDER = NoCredentialsProvider.create(); - private static final String PROJECT_ID = "ras-rm-dev"; - private static final String TOPIC_ID = "test_topic"; - private static final String CASE_CREATION_TOPIC_ID = "test_case_creation_topic"; - private static final String SUBSCRIPTION_ID = "test_subscription"; - private static final String CASE_CREATION_SUBSCRIPTION_ID = "test_case_creation_subscription"; + private static final String PROJECT_ID = "local"; + private static final String TOPIC_ID = "ras-rm-notify-local"; + private static final String CASE_CREATION_TOPIC_ID = "case-notification-local"; + private static final String SUBSCRIPTION_ID = "sdx-receipt-local"; + private static final String CASE_CREATION_SUBSCRIPTION_ID = "case-notification-local"; private final TopicAdminClient topicClient = TopicAdminClient.create( TopicAdminSettings.newBuilder() @@ -119,18 +120,4 @@ public void publishSampleCaseCreationMessage(String message) { public void shutdown() { CHANNEL.shutdown(); } - - public void testInit() { - Topic topic = topicClient.createTopic(topicName); - System.out.println("Created topic: " + topic.getName()); - Subscription subscription = - subscriptionAdminClient.createSubscription( - subscriptionName, topicName, PushConfig.getDefaultInstance(), 10); - System.out.println("Created pull subscription: " + subscription.getName()); - } - - public void testTeardown() { - subscriptionAdminClient.deleteSubscription(subscriptionName); - topicClient.deleteTopic(topicName); - } } diff --git a/src/test/java/uk/gov/ons/ctp/response/casesvc/CaseCreator.java b/src/test/java/uk/gov/ons/ctp/response/casesvc/CaseCreator.java index b6d9e768e..d6e5cb5dd 100644 --- a/src/test/java/uk/gov/ons/ctp/response/casesvc/CaseCreator.java +++ b/src/test/java/uk/gov/ons/ctp/response/casesvc/CaseCreator.java @@ -8,7 +8,6 @@ import org.springframework.core.io.ResourceLoader; import org.springframework.stereotype.Component; import uk.gov.ons.ctp.response.casesvc.message.sampleunitnotification.SampleUnitParent; -import uk.gov.ons.ctp.response.casesvc.utility.PubSubEmulator; @Component public class CaseCreator { @@ -21,7 +20,7 @@ public class CaseCreator { public void postSampleUnit( String sampleUnitRef, String sampleUnitType, UUID sampleUnitId, UUID collectionExerciseId) throws Exception { - PubSubEmulator pubSubEmulator = new PubSubEmulator(); + PubSubTestEmulator pubSubEmulator = new PubSubTestEmulator(); iacServiceStub.createIACStub(); SampleUnitParent sampleUnit = new SampleUnitParent(); diff --git a/src/test/java/uk/gov/ons/ctp/response/casesvc/PubSubTestEmulator.java b/src/test/java/uk/gov/ons/ctp/response/casesvc/PubSubTestEmulator.java new file mode 100644 index 000000000..d5b229c71 --- /dev/null +++ b/src/test/java/uk/gov/ons/ctp/response/casesvc/PubSubTestEmulator.java @@ -0,0 +1,136 @@ +package uk.gov.ons.ctp.response.casesvc; + +import com.godaddy.logging.Logger; +import com.godaddy.logging.LoggerFactory; +import com.google.api.core.ApiFuture; +import com.google.api.gax.core.CredentialsProvider; +import com.google.api.gax.core.NoCredentialsProvider; +import com.google.api.gax.grpc.GrpcTransportChannel; +import com.google.api.gax.rpc.FixedTransportChannelProvider; +import com.google.api.gax.rpc.TransportChannelProvider; +import com.google.cloud.pubsub.v1.*; +import com.google.cloud.pubsub.v1.stub.GrpcSubscriberStub; +import com.google.cloud.pubsub.v1.stub.SubscriberStubSettings; +import com.google.protobuf.ByteString; +import com.google.pubsub.v1.*; +import io.grpc.ManagedChannel; +import io.grpc.ManagedChannelBuilder; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +/** + * * This is a PubSub Emulator class. This is a utility class which is used for testing pubsub + * function + */ +public class PubSubTestEmulator { + private static final Logger log = LoggerFactory.getLogger(PubSubTestEmulator.class); + private static final String HOST_PORT = "localhost:18681"; + public static final ManagedChannel CHANNEL = + ManagedChannelBuilder.forTarget(HOST_PORT).usePlaintext().build(); + public static final TransportChannelProvider CHANNEL_PROVIDER = + FixedTransportChannelProvider.create(GrpcTransportChannel.create(CHANNEL)); + public static final CredentialsProvider CREDENTIAL_PROVIDER = NoCredentialsProvider.create(); + private static final String PROJECT_ID = "ras-rm-dev"; + private static final String TOPIC_ID = "test_topic"; + private static final String CASE_CREATION_TOPIC_ID = "test_case_creation_topic"; + private static final String SUBSCRIPTION_ID = "test_subscription"; + private static final String CASE_CREATION_SUBSCRIPTION_ID = "test_case_creation_subscription"; + private final TopicAdminClient topicClient = + TopicAdminClient.create( + TopicAdminSettings.newBuilder() + .setTransportChannelProvider(PubSubTestEmulator.CHANNEL_PROVIDER) + .setCredentialsProvider(PubSubTestEmulator.CREDENTIAL_PROVIDER) + .build()); + SubscriptionAdminClient subscriptionAdminClient = + SubscriptionAdminClient.create( + SubscriptionAdminSettings.newBuilder() + .setTransportChannelProvider(PubSubTestEmulator.CHANNEL_PROVIDER) + .setCredentialsProvider(PubSubTestEmulator.CREDENTIAL_PROVIDER) + .build()); + TopicName topicName = TopicName.of(PROJECT_ID, TOPIC_ID); + TopicName caseCreationTopicName = TopicName.of(PROJECT_ID, CASE_CREATION_TOPIC_ID); + ProjectSubscriptionName subscriptionName = + ProjectSubscriptionName.of(PROJECT_ID, SUBSCRIPTION_ID); + ProjectSubscriptionName caseCreationSubscriptionName = + ProjectSubscriptionName.of(PROJECT_ID, CASE_CREATION_SUBSCRIPTION_ID); + + public PubSubTestEmulator() throws IOException {} + + public Publisher getEmulatorPublisher(TopicName topicName) throws IOException { + return Publisher.newBuilder(topicName) + .setChannelProvider(CHANNEL_PROVIDER) + .setCredentialsProvider(CREDENTIAL_PROVIDER) + .build(); + } + + public Subscriber getEmulatorSubscriber(MessageReceiver receiver) { + return Subscriber.newBuilder(ProjectSubscriptionName.of(PROJECT_ID, SUBSCRIPTION_ID), receiver) + .setChannelProvider(CHANNEL_PROVIDER) + .setCredentialsProvider(CREDENTIAL_PROVIDER) + .build(); + } + + public Subscriber getEmulatorSubscriberForCaseActionEventStatus(MessageReceiver receiver) { + return Subscriber.newBuilder( + ProjectSubscriptionName.of(PROJECT_ID, "test_event_status_subscription"), receiver) + .setChannelProvider(CHANNEL_PROVIDER) + .setCredentialsProvider(CREDENTIAL_PROVIDER) + .build(); + } + + public GrpcSubscriberStub getEmulatorSubscriberStub() throws IOException { + return GrpcSubscriberStub.create( + SubscriberStubSettings.newBuilder() + .setTransportChannelProvider(CHANNEL_PROVIDER) + .setCredentialsProvider(CREDENTIAL_PROVIDER) + .build()); + } + + public void publishMessage(String message) { + try { + ByteString data = ByteString.copyFromUtf8(message); + PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); + TopicName topicName = TopicName.of(PROJECT_ID, TOPIC_ID); + Publisher publisher = getEmulatorPublisher(topicName); + log.with("publisher", publisher).info("Publishing message to pubsub emulator"); + ApiFuture messageIdFuture = publisher.publish(pubsubMessage); + String messageId = messageIdFuture.get(); + log.with("messageId", messageId).info("Published message to pubsub emulator"); + } catch (IOException | InterruptedException | ExecutionException e) { + log.error("Failed to publish message", e); + } + } + + public void publishSampleCaseCreationMessage(String message) { + try { + ByteString data = ByteString.copyFromUtf8(message); + PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); + TopicName topicName = TopicName.of(PROJECT_ID, CASE_CREATION_TOPIC_ID); + Publisher publisher = getEmulatorPublisher(topicName); + log.with("publisher", publisher).info("Publishing case creation message to pubsub emulator"); + ApiFuture messageIdFuture = publisher.publish(pubsubMessage); + String messageId = messageIdFuture.get(); + log.with("messageId", messageId).info("Published case creation message to pubsub emulator"); + } catch (IOException | InterruptedException | ExecutionException e) { + log.error("Failed to publish message", e); + } + } + + public void shutdown() { + CHANNEL.shutdown(); + } + + public void testInit() { + Topic topic = topicClient.createTopic(topicName); + System.out.println("Created topic: " + topic.getName()); + Subscription subscription = + subscriptionAdminClient.createSubscription( + subscriptionName, topicName, PushConfig.getDefaultInstance(), 10); + System.out.println("Created pull subscription: " + subscription.getName()); + } + + public void testTeardown() { + subscriptionAdminClient.deleteSubscription(subscriptionName); + topicClient.deleteTopic(topicName); + } +} diff --git a/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseActionEventIT.java b/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseActionEventIT.java index a94aae153..dafb2288a 100644 --- a/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseActionEventIT.java +++ b/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseActionEventIT.java @@ -27,6 +27,7 @@ import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import uk.gov.ons.ctp.response.casesvc.CaseCreator; +import uk.gov.ons.ctp.response.casesvc.PubSubTestEmulator; import uk.gov.ons.ctp.response.casesvc.client.CollectionExerciseSvcClient; import uk.gov.ons.ctp.response.casesvc.domain.model.CaseActionEventRequest; import uk.gov.ons.ctp.response.casesvc.domain.repository.CaseActionEventRequestRepository; @@ -37,7 +38,6 @@ import uk.gov.ons.ctp.response.casesvc.representation.action.CaseActionEvent; import uk.gov.ons.ctp.response.casesvc.service.action.email.ProcessEmailActionService; import uk.gov.ons.ctp.response.casesvc.service.action.letter.ProcessLetterActionService; -import uk.gov.ons.ctp.response.casesvc.utility.PubSubEmulator; import uk.gov.ons.ctp.response.lib.collection.exercise.CollectionExerciseDTO; import uk.gov.ons.ctp.response.lib.common.UnirestInitialiser; @@ -53,7 +53,7 @@ public class CaseActionEventIT { private UUID collectionExerciseId; private Map metadata; - private PubSubEmulator pubSubEmulator = new PubSubEmulator(); + private PubSubTestEmulator pubSubEmulator = new PubSubTestEmulator(); @MockBean private ProcessLetterActionService processLetterActionService; @MockBean private ProcessEmailActionService processEmailActionService; diff --git a/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseEndpointIT.java b/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseEndpointIT.java index 1856734e8..01f6826d1 100644 --- a/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseEndpointIT.java +++ b/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseEndpointIT.java @@ -25,13 +25,13 @@ import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import uk.gov.ons.ctp.response.casesvc.CaseCreator; +import uk.gov.ons.ctp.response.casesvc.PubSubTestEmulator; import uk.gov.ons.ctp.response.casesvc.client.CollectionExerciseSvcClient; import uk.gov.ons.ctp.response.casesvc.domain.repository.CaseEventRepository; import uk.gov.ons.ctp.response.casesvc.domain.repository.CaseGroupRepository; import uk.gov.ons.ctp.response.casesvc.domain.repository.CaseRepository; import uk.gov.ons.ctp.response.casesvc.representation.*; import uk.gov.ons.ctp.response.casesvc.representation.CategoryDTO.CategoryName; -import uk.gov.ons.ctp.response.casesvc.utility.PubSubEmulator; import uk.gov.ons.ctp.response.lib.collection.exercise.CollectionExerciseDTO; import uk.gov.ons.ctp.response.lib.common.UnirestInitialiser; @@ -45,7 +45,7 @@ public class CaseEndpointIT { private UUID collectionExerciseId; private Map metadata; - private PubSubEmulator pubSubEmulator = new PubSubEmulator(); + private PubSubTestEmulator pubSubEmulator = new PubSubTestEmulator(); @ClassRule public static WireMockRule wireMockRule = diff --git a/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseIACEndpointIT.java b/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseIACEndpointIT.java index efe4847a6..7163d6ce1 100644 --- a/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseIACEndpointIT.java +++ b/src/test/java/uk/gov/ons/ctp/response/casesvc/endpoint/CaseIACEndpointIT.java @@ -34,13 +34,13 @@ import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import uk.gov.ons.ctp.response.casesvc.CaseCreator; +import uk.gov.ons.ctp.response.casesvc.PubSubTestEmulator; import uk.gov.ons.ctp.response.casesvc.client.CollectionExerciseSvcClient; import uk.gov.ons.ctp.response.casesvc.domain.repository.CaseEventRepository; import uk.gov.ons.ctp.response.casesvc.domain.repository.CaseGroupRepository; import uk.gov.ons.ctp.response.casesvc.domain.repository.CaseRepository; import uk.gov.ons.ctp.response.casesvc.representation.CaseDetailsDTO; import uk.gov.ons.ctp.response.casesvc.representation.CaseIACDTO; -import uk.gov.ons.ctp.response.casesvc.utility.PubSubEmulator; import uk.gov.ons.ctp.response.lib.collection.exercise.CollectionExerciseDTO; import uk.gov.ons.ctp.response.lib.common.UnirestInitialiser; @@ -56,7 +56,7 @@ public class CaseIACEndpointIT { private Map metadata; private static final Logger log = LoggerFactory.getLogger(CaseIACEndpointIT.class); - private PubSubEmulator pubSubEmulator = new PubSubEmulator(); + private PubSubTestEmulator pubSubEmulator = new PubSubTestEmulator(); @ClassRule public static WireMockRule wireMockRule = diff --git a/src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java b/src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java deleted file mode 100644 index eb54dfaa4..000000000 --- a/src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java +++ /dev/null @@ -1,75 +0,0 @@ -package uk.gov.ons.ctp.response.casesvc.message; - -import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer; -import com.github.tomakehurst.wiremock.junit.WireMockRule; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import org.junit.*; -import org.junit.contrib.java.lang.system.EnvironmentVariables; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import uk.gov.ons.ctp.response.casesvc.message.feedback.CaseReceipt; -import uk.gov.ons.ctp.response.casesvc.utility.PubSubEmulator; - -@ContextConfiguration -@ActiveProfiles("test") -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@RunWith(SpringJUnit4ClassRunner.class) -@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) -@TestPropertySource(locations = "classpath:/application-test.yml") -public class CaseReceiptReceiverIT { - - private static PubSubEmulator PUBSUBEMULATOR; - - private String receiptFile = - "src/test/resources/uk/gov/ons/ctp/response/casesvc/message/receiptPubsub.json"; - - @ClassRule - public static final EnvironmentVariables environmentVariables = - new EnvironmentVariables().set("PUBSUB_EMULATOR_HOST", "127.0.0.1:18681"); - - @ClassRule - public static WireMockRule wireMockRule = - new WireMockRule(options().extensions(new ResponseTemplateTransformer(false)).port(18002)); - - @MockBean private CaseReceiptReceiver caseReceiptReceiver; - - public CaseReceiptReceiverIT() throws IOException {} - - @BeforeClass - public static void testSetup() throws IOException { - PUBSUBEMULATOR = new PubSubEmulator(); - PUBSUBEMULATOR.testInit(); - } - - @AfterClass - public static void testTearDown() { - PUBSUBEMULATOR.testTeardown(); - } - - @Test - public void testCaseNotificationReceiverIsReceivingMessageFromPubSub() throws Exception { - String json = readFileAsString(receiptFile); - PUBSUBEMULATOR.publishMessage(json); - Thread.sleep(2000); - ObjectMapper objectMapper = new ObjectMapper(); - CaseReceipt caseReceipt = objectMapper.readValue(json, CaseReceipt.class); - - Mockito.verify(caseReceiptReceiver, Mockito.times(1)).process(caseReceipt); - } - - private static String readFileAsString(String file) throws Exception { - return new String(Files.readAllBytes(Paths.get(file))); - } -} diff --git a/src/test/java/uk/gov/ons/ctp/response/casesvc/message/TestPubSubMessage.java b/src/test/java/uk/gov/ons/ctp/response/casesvc/message/TestPubSubMessage.java index e9c807e2e..79ee505d1 100644 --- a/src/test/java/uk/gov/ons/ctp/response/casesvc/message/TestPubSubMessage.java +++ b/src/test/java/uk/gov/ons/ctp/response/casesvc/message/TestPubSubMessage.java @@ -10,12 +10,12 @@ import java.io.IOException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import uk.gov.ons.ctp.response.casesvc.PubSubTestEmulator; import uk.gov.ons.ctp.response.casesvc.representation.action.CaseActionEvent; -import uk.gov.ons.ctp.response.casesvc.utility.PubSubEmulator; public class TestPubSubMessage { private static final Logger log = LoggerFactory.getLogger(TestPubSubMessage.class); - private final PubSubEmulator pubsubEmulator = new PubSubEmulator(); + private final PubSubTestEmulator pubsubEmulator = new PubSubTestEmulator(); private String receivedMessage = null; public TestPubSubMessage() throws IOException {} From bea675f35dd36f507dabc93eaa815a2c1f65057f Mon Sep 17 00:00:00 2001 From: Mark Price Date: Sat, 15 Apr 2023 23:29:45 +0100 Subject: [PATCH 07/20] Adding failing test --- .../message/CaseReceiptReceiverIT.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java diff --git a/src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java b/src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java new file mode 100644 index 000000000..dbfe24da3 --- /dev/null +++ b/src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java @@ -0,0 +1,70 @@ +package uk.gov.ons.ctp.response.casesvc.message; + +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer; +import com.github.tomakehurst.wiremock.junit.WireMockRule; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import org.junit.*; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import uk.gov.ons.ctp.response.casesvc.PubSubTestEmulator; +import uk.gov.ons.ctp.response.casesvc.message.feedback.CaseReceipt; + +@ContextConfiguration +@ActiveProfiles("test") +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@RunWith(SpringJUnit4ClassRunner.class) +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) +@TestPropertySource(locations = "classpath:/application-test.yml") +public class CaseReceiptReceiverIT { + + private final PubSubTestEmulator pubSubEmulator = new PubSubTestEmulator(); + + @ClassRule + public static WireMockRule wireMockRule = + new WireMockRule(options().extensions(new ResponseTemplateTransformer(false)).port(18002)); + + @MockBean private CaseReceiptReceiver caseReceiptReceiver; + + public CaseReceiptReceiverIT() throws IOException {} + + @Before + public void testSetup() { + pubSubEmulator.testInit(); + } + + @After + public void testTearDown() { + pubSubEmulator.testTeardown(); + } + + @Test + public void testCaseNotificationReceiverIsReceivingMessageFromPubSub() throws Exception { + String receiptFile = + "src/test/resources/uk/gov/ons/ctp/response/casesvc/message/receiptPubsub.json"; + String json = readFileAsString(receiptFile); + System.out.println("************PUBLISHING****************"); + pubSubEmulator.publishMessage(json); + System.out.println("^^^^^^^^^^^^PUBLISHED^^^^^^^^^^^^^^^^"); + Thread.sleep(2000); + ObjectMapper objectMapper = new ObjectMapper(); + CaseReceipt caseReceipt = objectMapper.readValue(json, CaseReceipt.class); + + Mockito.verify(caseReceiptReceiver, Mockito.times(1)).process(caseReceipt); + } + + private static String readFileAsString(String file) throws Exception { + return new String(Files.readAllBytes(Paths.get(file))); + } +} From b9a3fa1797ca35e86d172c2214f4e07fdc4932a0 Mon Sep 17 00:00:00 2001 From: Mark Price Date: Sun, 16 Apr 2023 09:46:24 +0100 Subject: [PATCH 08/20] Refactored pubsub receiver tests to allow emulator to be used for app --- .../response/casesvc/PubSubTestEmulator.java | 31 ++++++----------- .../message/CaseReceiptReceiverIT.java | 25 ++++---------- .../casesvc/message/TestPubSubMessage.java | 34 ++++++++++++++++++- 3 files changed, 50 insertions(+), 40 deletions(-) diff --git a/src/test/java/uk/gov/ons/ctp/response/casesvc/PubSubTestEmulator.java b/src/test/java/uk/gov/ons/ctp/response/casesvc/PubSubTestEmulator.java index d5b229c71..81633515d 100644 --- a/src/test/java/uk/gov/ons/ctp/response/casesvc/PubSubTestEmulator.java +++ b/src/test/java/uk/gov/ons/ctp/response/casesvc/PubSubTestEmulator.java @@ -18,10 +18,7 @@ import java.io.IOException; import java.util.concurrent.ExecutionException; -/** - * * This is a PubSub Emulator class. This is a utility class which is used for testing pubsub - * function - */ +/** * This is a PubSub Emulator class which is used for testing pubsub function */ public class PubSubTestEmulator { private static final Logger log = LoggerFactory.getLogger(PubSubTestEmulator.class); private static final String HOST_PORT = "localhost:18681"; @@ -31,9 +28,9 @@ public class PubSubTestEmulator { FixedTransportChannelProvider.create(GrpcTransportChannel.create(CHANNEL)); public static final CredentialsProvider CREDENTIAL_PROVIDER = NoCredentialsProvider.create(); private static final String PROJECT_ID = "ras-rm-dev"; - private static final String TOPIC_ID = "test_topic"; + private static final String RECEIPT_TOPIC_ID = "test_receipt_topic"; private static final String CASE_CREATION_TOPIC_ID = "test_case_creation_topic"; - private static final String SUBSCRIPTION_ID = "test_subscription"; + private static final String RECEIPT_SUBSCRIPTION_ID = "test_receipt_subscription"; private static final String CASE_CREATION_SUBSCRIPTION_ID = "test_case_creation_subscription"; private final TopicAdminClient topicClient = TopicAdminClient.create( @@ -47,10 +44,10 @@ public class PubSubTestEmulator { .setTransportChannelProvider(PubSubTestEmulator.CHANNEL_PROVIDER) .setCredentialsProvider(PubSubTestEmulator.CREDENTIAL_PROVIDER) .build()); - TopicName topicName = TopicName.of(PROJECT_ID, TOPIC_ID); + TopicName topicName = TopicName.of(PROJECT_ID, RECEIPT_TOPIC_ID); TopicName caseCreationTopicName = TopicName.of(PROJECT_ID, CASE_CREATION_TOPIC_ID); ProjectSubscriptionName subscriptionName = - ProjectSubscriptionName.of(PROJECT_ID, SUBSCRIPTION_ID); + ProjectSubscriptionName.of(PROJECT_ID, RECEIPT_SUBSCRIPTION_ID); ProjectSubscriptionName caseCreationSubscriptionName = ProjectSubscriptionName.of(PROJECT_ID, CASE_CREATION_SUBSCRIPTION_ID); @@ -63,16 +60,8 @@ public Publisher getEmulatorPublisher(TopicName topicName) throws IOException { .build(); } - public Subscriber getEmulatorSubscriber(MessageReceiver receiver) { - return Subscriber.newBuilder(ProjectSubscriptionName.of(PROJECT_ID, SUBSCRIPTION_ID), receiver) - .setChannelProvider(CHANNEL_PROVIDER) - .setCredentialsProvider(CREDENTIAL_PROVIDER) - .build(); - } - - public Subscriber getEmulatorSubscriberForCaseActionEventStatus(MessageReceiver receiver) { - return Subscriber.newBuilder( - ProjectSubscriptionName.of(PROJECT_ID, "test_event_status_subscription"), receiver) + public Subscriber getEmulatorSubscriber(MessageReceiver receiver, String subscription) { + return Subscriber.newBuilder(ProjectSubscriptionName.of(PROJECT_ID, subscription), receiver) .setChannelProvider(CHANNEL_PROVIDER) .setCredentialsProvider(CREDENTIAL_PROVIDER) .build(); @@ -90,7 +79,7 @@ public void publishMessage(String message) { try { ByteString data = ByteString.copyFromUtf8(message); PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); - TopicName topicName = TopicName.of(PROJECT_ID, TOPIC_ID); + TopicName topicName = TopicName.of(PROJECT_ID, RECEIPT_TOPIC_ID); Publisher publisher = getEmulatorPublisher(topicName); log.with("publisher", publisher).info("Publishing message to pubsub emulator"); ApiFuture messageIdFuture = publisher.publish(pubsubMessage); @@ -122,11 +111,11 @@ public void shutdown() { public void testInit() { Topic topic = topicClient.createTopic(topicName); - System.out.println("Created topic: " + topic.getName()); + log.info("Created topic: " + topic.getName()); Subscription subscription = subscriptionAdminClient.createSubscription( subscriptionName, topicName, PushConfig.getDefaultInstance(), 10); - System.out.println("Created pull subscription: " + subscription.getName()); + log.info("Created pull subscription: " + subscription.getName()); } public void testTeardown() { diff --git a/src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java b/src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java index dbfe24da3..2d66ef2c2 100644 --- a/src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java +++ b/src/test/java/uk/gov/ons/ctp/response/casesvc/message/CaseReceiptReceiverIT.java @@ -1,18 +1,11 @@ package uk.gov.ons.ctp.response.casesvc.message; -import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer; -import com.github.tomakehurst.wiremock.junit.WireMockRule; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import org.junit.*; import org.junit.runner.RunWith; -import org.mockito.Mockito; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; @@ -31,12 +24,6 @@ public class CaseReceiptReceiverIT { private final PubSubTestEmulator pubSubEmulator = new PubSubTestEmulator(); - @ClassRule - public static WireMockRule wireMockRule = - new WireMockRule(options().extensions(new ResponseTemplateTransformer(false)).port(18002)); - - @MockBean private CaseReceiptReceiver caseReceiptReceiver; - public CaseReceiptReceiverIT() throws IOException {} @Before @@ -51,17 +38,19 @@ public void testTearDown() { @Test public void testCaseNotificationReceiverIsReceivingMessageFromPubSub() throws Exception { + // Publish a test receipt String receiptFile = "src/test/resources/uk/gov/ons/ctp/response/casesvc/message/receiptPubsub.json"; String json = readFileAsString(receiptFile); - System.out.println("************PUBLISHING****************"); pubSubEmulator.publishMessage(json); - System.out.println("^^^^^^^^^^^^PUBLISHED^^^^^^^^^^^^^^^^"); + + // Call the receiver to get the receipt message + TestPubSubMessage message = new TestPubSubMessage(); Thread.sleep(2000); - ObjectMapper objectMapper = new ObjectMapper(); - CaseReceipt caseReceipt = objectMapper.readValue(json, CaseReceipt.class); + CaseReceipt caseReceipt = message.getCaseReceipt(); - Mockito.verify(caseReceiptReceiver, Mockito.times(1)).process(caseReceipt); + // Test the case ID of the receipt received is the one we published + Assert.assertEquals(caseReceipt.getCaseId(), "9b872c6c-3339-4db9-9ef6-ff37a4446321"); } private static String readFileAsString(String file) throws Exception { diff --git a/src/test/java/uk/gov/ons/ctp/response/casesvc/message/TestPubSubMessage.java b/src/test/java/uk/gov/ons/ctp/response/casesvc/message/TestPubSubMessage.java index 79ee505d1..2df59ef00 100644 --- a/src/test/java/uk/gov/ons/ctp/response/casesvc/message/TestPubSubMessage.java +++ b/src/test/java/uk/gov/ons/ctp/response/casesvc/message/TestPubSubMessage.java @@ -11,6 +11,7 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import uk.gov.ons.ctp.response.casesvc.PubSubTestEmulator; +import uk.gov.ons.ctp.response.casesvc.message.feedback.CaseReceipt; import uk.gov.ons.ctp.response.casesvc.representation.action.CaseActionEvent; public class TestPubSubMessage { @@ -31,7 +32,8 @@ public CaseActionEvent getPubSubCaseActionEventStatus() throws IOException { log.info("Received : " + receivedMessage); consumer.ack(); }; - Subscriber subscriber = pubsubEmulator.getEmulatorSubscriberForCaseActionEventStatus(receiver); + Subscriber subscriber = + pubsubEmulator.getEmulatorSubscriber(receiver, "test_event_status_subscription"); Thread t = new Thread(() -> subscriber.startAsync().awaitRunning()); ExecutorService service = Executors.newSingleThreadExecutor(); service.execute(t); @@ -48,4 +50,34 @@ public CaseActionEvent getPubSubCaseActionEventStatus() throws IOException { ObjectMapper objectMapper = new ObjectMapper(); return objectMapper.readValue(receivedMessage, CaseActionEvent.class); } + + public CaseReceipt getCaseReceipt() throws IOException { + + MessageReceiver receiver = + (PubsubMessage message, AckReplyConsumer consumer) -> { + // Handle incoming message, then ack the received message. + log.info("Id: " + message.getMessageId()); + log.info("Data: " + message.getData().toStringUtf8()); + receivedMessage = message.getData().toStringUtf8(); + log.info("Received : " + receivedMessage); + consumer.ack(); + }; + Subscriber subscriber = + pubsubEmulator.getEmulatorSubscriber(receiver, "test_receipt_subscription"); + Thread t = new Thread(() -> subscriber.startAsync().awaitRunning()); + ExecutorService service = Executors.newSingleThreadExecutor(); + service.execute(t); + while (receivedMessage == null) { + try { + Thread.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + subscriber.stopAsync().awaitTerminated(); + service.shutdown(); + System.out.println(receivedMessage); + ObjectMapper objectMapper = new ObjectMapper(); + return objectMapper.readValue(receivedMessage, CaseReceipt.class); + } } From 13b20b1071a54ff879ffbc90d8d5bd7bf6ec480c Mon Sep 17 00:00:00 2001 From: ras-rm-pr-bot Date: Sun, 16 Apr 2023 09:02:33 +0000 Subject: [PATCH 09/20] auto patch increment --- _infra/helm/case/Chart.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_infra/helm/case/Chart.yaml b/_infra/helm/case/Chart.yaml index 3b2021685..1f2619ffb 100644 --- a/_infra/helm/case/Chart.yaml +++ b/_infra/helm/case/Chart.yaml @@ -14,8 +14,8 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. -version: 12.0.9 +version: 12.0.7 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. -appVersion: 12.0.9 +appVersion: 12.0.7 From 0c030eb5a9fc4221e8e67ad34f2881a1594d5bd3 Mon Sep 17 00:00:00 2001 From: ras-rm-pr-bot Date: Mon, 2 Oct 2023 10:05:19 +0000 Subject: [PATCH 10/20] auto patch increment --- _infra/helm/case/Chart.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_infra/helm/case/Chart.yaml b/_infra/helm/case/Chart.yaml index 6d4970295..9a14760eb 100644 --- a/_infra/helm/case/Chart.yaml +++ b/_infra/helm/case/Chart.yaml @@ -12,9 +12,9 @@ description: A Helm chart for Kubernetes # pipeline. Library charts do not define any templates and therefore cannot be deployed. type: application -version: 12.0.14 +version: 12.0.15 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. -appVersion: 12.0.14 +appVersion: 12.0.15 From 458cd3fbd66eb0c7c805c0f5592a7426df02d31e Mon Sep 17 00:00:00 2001 From: ras-rm-pr-bot Date: Fri, 1 Dec 2023 17:34:58 +0000 Subject: [PATCH 11/20] auto patch increment --- _infra/helm/case/Chart.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_infra/helm/case/Chart.yaml b/_infra/helm/case/Chart.yaml index 7b2c92299..94c20e7cd 100644 --- a/_infra/helm/case/Chart.yaml +++ b/_infra/helm/case/Chart.yaml @@ -14,8 +14,8 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. -version: 12.0.16 +version: 12.0.17 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. -appVersion: 12.0.16 +appVersion: 12.0.17 From 6980f211e9931f90c281f0713ec5188cf155869c Mon Sep 17 00:00:00 2001 From: ras-rm-pr-bot Date: Fri, 22 Dec 2023 14:06:44 +0000 Subject: [PATCH 12/20] auto patch increment --- _infra/helm/case/Chart.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_infra/helm/case/Chart.yaml b/_infra/helm/case/Chart.yaml index 94c20e7cd..d3f113b7b 100644 --- a/_infra/helm/case/Chart.yaml +++ b/_infra/helm/case/Chart.yaml @@ -14,8 +14,8 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. -version: 12.0.17 +version: 12.0.18 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. -appVersion: 12.0.17 +appVersion: 12.0.18 From c359d0f070ca056c74fd6b864423a1df335e2bea Mon Sep 17 00:00:00 2001 From: ras-rm-pr-bot Date: Mon, 5 Feb 2024 11:29:12 +0000 Subject: [PATCH 13/20] auto patch increment --- _infra/helm/case/Chart.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_infra/helm/case/Chart.yaml b/_infra/helm/case/Chart.yaml index d3f113b7b..891cc2500 100644 --- a/_infra/helm/case/Chart.yaml +++ b/_infra/helm/case/Chart.yaml @@ -14,8 +14,8 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. -version: 12.0.18 +version: 12.0.19 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. -appVersion: 12.0.18 +appVersion: 12.0.19 From 7f9cf778b7d1f850644153699f389b039242e005 Mon Sep 17 00:00:00 2001 From: ras-rm-pr-bot Date: Tue, 20 Feb 2024 08:24:42 +0000 Subject: [PATCH 14/20] auto patch increment --- _infra/helm/case/Chart.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_infra/helm/case/Chart.yaml b/_infra/helm/case/Chart.yaml index 05027bad7..6f1dc1ce1 100644 --- a/_infra/helm/case/Chart.yaml +++ b/_infra/helm/case/Chart.yaml @@ -14,9 +14,9 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. -version: 12.0.21 +version: 12.0.22 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. -appVersion: 12.0.21 +appVersion: 12.0.22 From 0cc59151639b92e1c160c327adbe67637f633b1b Mon Sep 17 00:00:00 2001 From: ras-rm-pr-bot Date: Tue, 27 Feb 2024 13:09:06 +0000 Subject: [PATCH 15/20] auto patch increment --- _infra/helm/case/Chart.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_infra/helm/case/Chart.yaml b/_infra/helm/case/Chart.yaml index 6f1dc1ce1..643b2d648 100644 --- a/_infra/helm/case/Chart.yaml +++ b/_infra/helm/case/Chart.yaml @@ -14,9 +14,9 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. -version: 12.0.22 +version: 12.0.23 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. -appVersion: 12.0.22 +appVersion: 12.0.23 From 6c45fe2e2304d60d6e94a0f98cd1668fcc2ceddd Mon Sep 17 00:00:00 2001 From: ras-rm-pr-bot Date: Tue, 3 Sep 2024 11:04:48 +0000 Subject: [PATCH 16/20] auto patch increment --- _infra/helm/case/Chart.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_infra/helm/case/Chart.yaml b/_infra/helm/case/Chart.yaml index 674c5d205..c8f8c67dd 100644 --- a/_infra/helm/case/Chart.yaml +++ b/_infra/helm/case/Chart.yaml @@ -14,8 +14,8 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. -version: 12.0.23 +version: 12.0.24 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. -appVersion: 12.0.23 +appVersion: 12.0.24 From 01743cad75039157f838de67aa95413a47553058 Mon Sep 17 00:00:00 2001 From: ras-rm-pr-bot Date: Tue, 19 Nov 2024 13:55:53 +0000 Subject: [PATCH 17/20] auto patch increment --- _infra/helm/case/Chart.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_infra/helm/case/Chart.yaml b/_infra/helm/case/Chart.yaml index c8f8c67dd..4ffcdd87f 100644 --- a/_infra/helm/case/Chart.yaml +++ b/_infra/helm/case/Chart.yaml @@ -14,8 +14,8 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. -version: 12.0.24 +version: 12.0.25 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. -appVersion: 12.0.24 +appVersion: 12.0.25 From 48f9a7a468943acab9d3e52142e2c53aa3851bdd Mon Sep 17 00:00:00 2001 From: Mark Price Date: Wed, 18 Dec 2024 18:22:29 +0000 Subject: [PATCH 18/20] Testing ras-rm-spinnaker-action@upgrade-packages --- .github/workflows/comment.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/comment.yml b/.github/workflows/comment.yml index c44a7a6ba..523e30293 100644 --- a/.github/workflows/comment.yml +++ b/.github/workflows/comment.yml @@ -22,11 +22,11 @@ jobs: uses: google-github-actions/setup-gcloud@v0 with: project_id: ${{ secrets.GOOGLE_PROJECT_ID }} - - uses: onsdigital/ras-rm-spinnaker-action@main + - uses: onsdigital/ras-rm-spinnaker-action@upgrade-packages with: comment-body: ${{ github.event.comment.body }} gcp-project: ${{ secrets.GOOGLE_PROJECT_ID }} bot-token: ${{ secrets.BOT_TOKEN }} spinnaker-topic: ${{ secrets.SPINNAKER_TOPIC }} artifact-bucket: ${{ secrets.ARTIFACT_BUCKET }} - helm-chart: ${{ env.SERVICE }}/${{ env.SERVICE }}-latest.tgz \ No newline at end of file + helm-chart: ${{ env.SERVICE }}/${{ env.SERVICE }}-latest.tgz From 7cec3ee2daef761ab930fe94c0f7d20fad015c76 Mon Sep 17 00:00:00 2001 From: Mark Price Date: Wed, 18 Dec 2024 18:29:02 +0000 Subject: [PATCH 19/20] Testing --- .github/workflows/comment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/comment.yml b/.github/workflows/comment.yml index 523e30293..6ba44b9ba 100644 --- a/.github/workflows/comment.yml +++ b/.github/workflows/comment.yml @@ -19,7 +19,7 @@ jobs: with: credentials_json: ${{ secrets.GCR_KEY }} - name: Setup Google Cloud SDK - uses: google-github-actions/setup-gcloud@v0 + uses: google-github-actions/setup-gcloud@v2 with: project_id: ${{ secrets.GOOGLE_PROJECT_ID }} - uses: onsdigital/ras-rm-spinnaker-action@upgrade-packages From b8c34951e59faf79f06c6997d29130aeee384db5 Mon Sep 17 00:00:00 2001 From: Mark Price Date: Thu, 19 Dec 2024 16:47:31 +0000 Subject: [PATCH 20/20] Revert GHA versions --- .github/workflows/comment.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/comment.yml b/.github/workflows/comment.yml index 6ba44b9ba..e1f9311ea 100644 --- a/.github/workflows/comment.yml +++ b/.github/workflows/comment.yml @@ -19,10 +19,10 @@ jobs: with: credentials_json: ${{ secrets.GCR_KEY }} - name: Setup Google Cloud SDK - uses: google-github-actions/setup-gcloud@v2 + uses: google-github-actions/setup-gcloud@v0 with: project_id: ${{ secrets.GOOGLE_PROJECT_ID }} - - uses: onsdigital/ras-rm-spinnaker-action@upgrade-packages + - uses: onsdigital/ras-rm-spinnaker-action@main with: comment-body: ${{ github.event.comment.body }} gcp-project: ${{ secrets.GOOGLE_PROJECT_ID }}