Skip to content

Commit

Permalink
intial commit for publishing collection exercise end event (#281)
Browse files Browse the repository at this point in the history
* intial commit for publishing collection exercise end event

* collection exercise end publisher

* formatting

* test fix

* format

* fix test for collection exericise end

* auto patch increment

* Add .editorconfig to .gitignore

* version bump

Co-authored-by: ras-rm-pr-bot <[email protected]>
Co-authored-by: Matt <[email protected]>
  • Loading branch information
3 people authored Nov 1, 2021
1 parent 36aba21 commit 7864322
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 11 deletions.
6 changes: 0 additions & 6 deletions .editorconfig

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ diagrams/*.svg

# Mac
*.DS_Store

.editorconfig
4 changes: 2 additions & 2 deletions _infra/helm/collection-exercise/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.1.5
version: 12.1.6

# 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.1.5
appVersion: 12.1.6
3 changes: 3 additions & 0 deletions _infra/helm/collection-exercise/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -281,5 +281,8 @@ spec:
value: "{{ .Values.gcp.sampleSummaryActivationStatusSubscription }}"
- name: GCP_SAMPLESUMMARYACTIVATIONTOPIC
value: "{{ .Values.gcp.sampleSummaryActivationTopic }}"
- name: GCP_COLLECTIONEXERCISEENDTOPIC
value: "{{ .Values.gcp.collectionExerciseEndTopic }}"

resources:
{{- toYaml .Values.resources.application | nindent 12 }}
1 change: 1 addition & 0 deletions _infra/helm/collection-exercise/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@ gcp:
caseNotificationTopic: "case-notification-topic"
sampleSummaryActivationStatusSubscription: sample-summary-activation-status
sampleSummaryActivationTopic: sample-summary-activation
collectionExerciseEndTopic: collection-exercise-end
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,18 @@ public interface PubsubOutboundGateway {
void sendToPubsub(String text);
}

@Bean
@ServiceActivator(inputChannel = "collectionExerciseEndChannel")
public MessageHandler collectionExerciseEndMessageSender(PubSubTemplate pubsubTemplate) {
return new PubSubMessageHandler(
pubsubTemplate, appConfig.getGcp().getCollectionExerciseEndTopic());
}

@MessagingGateway(defaultRequestChannel = "collectionExerciseEndChannel")
public interface CollectionExerciseEndOutboundGateway {
void sendToPubsub(String text);
}

/**
* Spring boot start-up
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public class GCP {
String caseNotificationTopic;
String sampleSummaryActivationStatusSubscription;
String sampleSummaryActivationTopic;
String collectionExerciseEndTopic;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package uk.gov.ons.ctp.response.collection.exercise.message;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import uk.gov.ons.ctp.response.collection.exercise.CollectionExerciseApplication.CollectionExerciseEndOutboundGateway;
import uk.gov.ons.ctp.response.collection.exercise.message.dto.CollectionExerciseEndEventDTO;

@Component
public class CollectionExerciseEndPublisher {

private static final Logger LOGGER =
LoggerFactory.getLogger(CollectionExerciseEndPublisher.class);

@Autowired private ObjectMapper objectMapper;

@Autowired private CollectionExerciseEndOutboundGateway messagingGateway;

public void sendCollectionExerciseEnd(UUID collectionExerciseId) {
CollectionExerciseEndEventDTO collectionExerciseEndEventDTO =
new CollectionExerciseEndEventDTO();
collectionExerciseEndEventDTO.setCollectionExerciseId(collectionExerciseId);

try {
String payload = objectMapper.writeValueAsString(collectionExerciseEndEventDTO);
messagingGateway.sendToPubsub(payload);
} catch (JsonProcessingException e) {
LOGGER.error("Failed to serialise collection exercise end event", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import uk.gov.ons.ctp.response.collection.exercise.CollectionExerciseApplication;
import uk.gov.ons.ctp.response.collection.exercise.CollectionExerciseApplication.PubsubOutboundGateway;
import uk.gov.ons.ctp.response.collection.exercise.representation.SampleSummaryActivationDTO;

@Component
public class SampleSummaryActivationPublisher {

private static final Logger LOGGER = LoggerFactory.getLogger(SampleSummaryActivationDTO.class);
private static final Logger LOGGER =
LoggerFactory.getLogger(SampleSummaryActivationPublisher.class);

@Autowired private ObjectMapper objectMapper;

@Autowired private CollectionExerciseApplication.PubsubOutboundGateway messagingGateway;
@Autowired private PubsubOutboundGateway messagingGateway;

public void sendSampleSummaryActivation(
UUID collectionExerciseId, UUID sampleSummaryId, UUID surveyId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package uk.gov.ons.ctp.response.collection.exercise.message.dto;

import java.util.UUID;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PUBLIC)
public class CollectionExerciseEndEventDTO {
private UUID collectionExerciseId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import uk.gov.ons.ctp.response.collection.exercise.domain.Event;
import uk.gov.ons.ctp.response.collection.exercise.lib.common.error.CTPException;
import uk.gov.ons.ctp.response.collection.exercise.lib.common.error.CTPException.Fault;
import uk.gov.ons.ctp.response.collection.exercise.message.CollectionExerciseEndPublisher;
import uk.gov.ons.ctp.response.collection.exercise.repository.EventRepository;
import uk.gov.ons.ctp.response.collection.exercise.representation.CollectionExerciseDTO;
import uk.gov.ons.ctp.response.collection.exercise.representation.EventDTO;
Expand Down Expand Up @@ -117,6 +118,8 @@ public static EventDTO createEventDTOFromEvent(Event event) {

@Autowired private CaseSvcClient caseSvcClient;

@Autowired private CollectionExerciseEndPublisher collectionExerciseEndPublisher;

public Event createEvent(EventDTO eventDto) throws CTPException {
UUID collexId = eventDto.getCollectionExerciseId();
CollectionExercise collex = getCollectionExercise(collexId, Fault.RESOURCE_NOT_FOUND);
Expand Down Expand Up @@ -394,6 +397,8 @@ public void processEvents() {
CollectionExerciseDTO.CollectionExerciseEvent.END_EXERCISE);
log.with("collection_exercise_id", event.getCollectionExercise().getId())
.info("Set collection exercise to ENDED state");
collectionExerciseEndPublisher.sendCollectionExerciseEnd(
event.getCollectionExercise().getId());
} catch (CTPException e) {
log.with("collection_exercise_id", event.getCollectionExercise().getId())
.error("Failed to set collection exercise to ENDED state", e);
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,4 @@ gcp:
caseNotificationTopic: "test_topic"
sampleSummaryActivationStatusSubscription: "sample-summary-activation-status"
sampleSummaryActivationTopic: "sample-summary-activation"
collectionExerciseEndTopic: "collect-exercise-end"
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package uk.gov.ons.ctp.response.collection.exercise.message;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.UUID;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import uk.gov.ons.ctp.response.collection.exercise.CollectionExerciseApplication;
import uk.gov.ons.ctp.response.collection.exercise.message.dto.CollectionExerciseEndEventDTO;

@RunWith(MockitoJUnitRunner.class)
public class CollectionExerciseEndPublisherTest {

@Mock private CollectionExerciseApplication.CollectionExerciseEndOutboundGateway messagingGateway;

@Mock private ObjectMapper objectMapper;

@InjectMocks CollectionExerciseEndPublisher collectionExerciseEndPublisher;

@Test
public void testSendCollectionExerciseEnd() throws Exception {
UUID collectionExerciseId = UUID.randomUUID();

ObjectMapper mapper = new ObjectMapper();
String payload =
mapper.writeValueAsString(new CollectionExerciseEndEventDTO(collectionExerciseId));
when(objectMapper.writeValueAsString(any(CollectionExerciseEndEventDTO.class)))
.thenReturn(payload);

collectionExerciseEndPublisher.sendCollectionExerciseEnd(collectionExerciseId);

verify(objectMapper, times(1)).writeValueAsString(any(CollectionExerciseEndEventDTO.class));
verify(messagingGateway, times(1)).sendToPubsub(payload);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import uk.gov.ons.ctp.response.collection.exercise.domain.Event;
import uk.gov.ons.ctp.response.collection.exercise.lib.common.error.CTPException;
import uk.gov.ons.ctp.response.collection.exercise.lib.common.error.CTPException.Fault;
import uk.gov.ons.ctp.response.collection.exercise.message.CollectionExerciseEndPublisher;
import uk.gov.ons.ctp.response.collection.exercise.repository.EventRepository;
import uk.gov.ons.ctp.response.collection.exercise.representation.CollectionExerciseDTO;
import uk.gov.ons.ctp.response.collection.exercise.representation.CollectionExerciseDTO.CollectionExerciseState;
Expand All @@ -56,6 +57,8 @@ public class EventServiceTest {

@Mock private EventRepository eventRepository;

@Mock private CollectionExerciseEndPublisher collectionExerciseEndPublisher;

@Spy private List<EventValidator> eventValidators = new ArrayList<>();

@InjectMocks private EventService eventService;
Expand Down

0 comments on commit 7864322

Please sign in to comment.