Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S28 3686: Handle when flows use different sig values #924

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion charts/pre-api/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ appVersion: "1.0"
description: A Helm chart for pre-api App
name: pre-api
home: https://github.com/hmcts/pre-api
version: 0.0.55
version: 0.0.56
maintainers:
- name: HMCTS pre team
dependencies:
Expand Down
2 changes: 2 additions & 0 deletions charts/pre-api/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ java: &javavalues #This is an anchor to reuse settings in Job mode.
alias: FLOW_CASE_STATE_NOTIFIER_HTTP_API
- name: flow-case-state-notifier-workflow-id
alias: CASE_STATE_CHANGE_NOTIFIER_WORKFLOW_ID
- name: flow-case-state-notifier-sig
alias: FLOW_CASE_STATE_CHANGE_NOTIFIER_SIG
- name: flow-close-live-event-email-workflow-id
alias: FLOW_CLOSE_LIVE_EVENT_EMAIL_WORKFLOW_ID
- name: flow-close-live-event-email-sig
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package uk.gov.hmcts.reform.preapi.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.beans.factory.annotation.Value;

public abstract class BaseFlowRequestInterceptor implements RequestInterceptor {
@Value("${flow.key}")
private String flowKey;

@Override
public void apply(RequestTemplate template) {
if (!template.path().contains(getFlowId())) {
return;
}

template.header("Content-Type", "application/json");
template.header("X-Flow-Key", getFlowKey());

template.query("api-version", "2016-06-01");
template.query("sp", "%2Ftriggers%2Fmanual%2Frun");
template.query("sv", "1.0");
template.query("sig", getFlowSig());
}

protected String getFlowKey() {
return flowKey;
}

protected abstract String getFlowSig();

protected abstract String getFlowId();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package uk.gov.hmcts.reform.preapi.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CaseStateChangeFlowHttpClientConfiguration {
@Bean
public CaseStateChangeFlowRequestInterceptor caseStateChangeFlowRequestInterceptor() {
return new CaseStateChangeFlowRequestInterceptor();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package uk.gov.hmcts.reform.preapi.config;

import org.springframework.beans.factory.annotation.Value;

public class CaseStateChangeFlowRequestInterceptor extends BaseFlowRequestInterceptor {
@Value("${flow.workflow.caseStateChangeNotifier.sig:}")
private String flowSig;

@Value("${flow.workflow.caseStateChangeNotifier.id}")
private String flowId;

@Override
protected String getFlowSig() {
return flowSig;
}

@Override
protected String getFlowId() {
return flowId;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package uk.gov.hmcts.reform.preapi.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class StopLiveEventFlowHttpClientConfiguration {

@Bean
public StopLiveEventFlowRequestInterceptor requestInterceptor() {
return new StopLiveEventFlowRequestInterceptor();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package uk.gov.hmcts.reform.preapi.config;

import org.springframework.beans.factory.annotation.Value;

public class StopLiveEventFlowRequestInterceptor extends BaseFlowRequestInterceptor {

@Value("${flow.key}")
private String flowKey;

@Value("${flow.workflow.stopLiveEventNotifier.sig:}")
private String flowSig;

@Value("${flow.workflow.stopLiveEventNotifier.id}")
private String flowId;

@Override
protected String getFlowKey() {
return flowKey;
}

@Override
protected String getFlowSig() {
return flowSig;
}

@Override
protected String getFlowId() {
return flowId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import uk.gov.hmcts.reform.preapi.config.FlowHttpClientConfiguration;
import uk.gov.hmcts.reform.preapi.config.CaseStateChangeFlowHttpClientConfiguration;
import uk.gov.hmcts.reform.preapi.dto.flow.CaseStateChangeNotificationDTO;

import java.util.List;

@FeignClient(
name = "caseStateChangeNotifierFlowClient",
url = "${flow.workflow.caseStateChangeNotifier.api}",
configuration = FlowHttpClientConfiguration.class
configuration = CaseStateChangeFlowHttpClientConfiguration.class
)
public interface CaseStateChangeNotifierFlowClient {
@PostMapping("/workflows/${flow.workflow.caseStateChangeNotifier.id}/triggers/manual/paths/invoke")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import uk.gov.hmcts.reform.preapi.config.FlowHttpClientConfiguration;
import uk.gov.hmcts.reform.preapi.config.StopLiveEventFlowHttpClientConfiguration;
import uk.gov.hmcts.reform.preapi.dto.flow.StoppedLiveEventsNotificationDTO;

import java.util.List;

@FeignClient(
name = "stopLiveEventNotifierFlowClient",
url = "${flow.workflow.stopLiveEventNotifier.api}",
configuration = FlowHttpClientConfiguration.class
configuration = StopLiveEventFlowHttpClientConfiguration.class
)
public interface StopLiveEventNotifierFlowClient {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,13 @@ public void onCaseClosed(Case c) {
.stream()
.map(share -> new CaseStateChangeNotificationDTO(EmailType.CLOSED, c, share))
.toList();
try {
caseStateChangeNotifierFlowClient.emailAfterCaseStateChange(notifications);
} catch (Exception e) {
log.error("Failed to notify users of case closure: {}", c.getId());

if (!notifications.isEmpty()) {
try {
caseStateChangeNotifierFlowClient.emailAfterCaseStateChange(notifications);
} catch (Exception e) {
log.error("Failed to notify users of case closure: {}", c.getId());
}
}

bookingRepository
Expand All @@ -295,6 +298,11 @@ public void onCaseClosureCancellation(Case c) {
.stream()
.map(share -> new CaseStateChangeNotificationDTO(EmailType.CLOSURE_CANCELLATION, c, share))
.toList();

if (notifications.isEmpty()) {
return;
}

try {
caseStateChangeNotifierFlowClient.emailAfterCaseStateChange(notifications);
} catch (Exception e) {
Expand All @@ -309,6 +317,11 @@ public void onCasePendingClosure(Case c) {
.stream()
.map(share -> new CaseStateChangeNotificationDTO(EmailType.PENDING_CLOSURE, c, share))
.toList();

if (notifications.isEmpty()) {
return;
}

try {
caseStateChangeNotifierFlowClient.emailAfterCaseStateChange(notifications);
} catch (Exception e) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,13 @@ flow:
stopLiveEventNotifier:
api: ${FLOW_CLOSE_LIVE_EVENT_HTTP_API:https://prod-01.uksouth.logic.azure.com}
id: ${FLOW_CLOSE_LIVE_EVENT_EMAIL_WORKFLOW_ID:3497b00b2e404a54bc53604aea1deaae}
sig: ${FLOW_CLOSE_LIVE_EVENT_EMAIL_SIG:123}
enabled: true
caseStateChangeNotifier:
api: ${FLOW_CASE_STATE_NOTIFIER_HTTP_API:https://prod-17.uksouth.logic.azure.com:443}
id: ${CASE_STATE_CHANGE_NOTIFIER_WORKFLOW_ID:8b976c9fdc7749d3a4b8c9d6a6ef9d0c}
sig: ${FLOW_CASE_STATE_CHANGE_NOTIFIER_SIG:123}
enabled: true
sig: ${FLOW_CLOSE_LIVE_EVENT_EMAIL_SIG:123}
key: ${FLOW_KEY:123}

#logging:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package uk.gov.hmcts.reform.preapi.config;

import feign.RequestTemplate;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class BaseFlowRequestInterceptorTest {

private BaseFlowRequestInterceptor interceptor;
private RequestTemplate requestTemplate;

@BeforeEach
void setUp() {
interceptor = new BaseFlowRequestInterceptor() {
@Override
protected String getFlowKey() {
return "flow-key";
}

protected String getFlowSig() {
return "flow-sig";
}

protected String getFlowId() {
return "test-flow-id";
}
};
requestTemplate = new RequestTemplate();
}

@Test
void shouldNotModifyRequestWhenFlowIdNotInPath() {
requestTemplate.uri("/some/other/path");
interceptor.apply(requestTemplate);

assertThat(requestTemplate.headers()).isEmpty();
}

@Test
void shouldModifyRequestWhenFlowIdInPath() {
requestTemplate.uri("/some/path/test-flow-id");
interceptor.apply(requestTemplate);

assertThat(requestTemplate.headers()).isNotEmpty();
assertThat(requestTemplate.headers().get("Content-Type").stream().findFirst().get())
.isEqualTo("application/json");
assertThat(requestTemplate.headers().get("X-Flow-Key").stream().findFirst().get())
.isEqualTo("flow-key");
assertThat(requestTemplate.queries().get("api-version").stream().findFirst().get())
.isEqualTo("2016-06-01");
assertThat(requestTemplate.queries().get("sp").stream().findFirst().get())
.isEqualTo("%2Ftriggers%2Fmanual%2Frun");
assertThat(requestTemplate.queries().get("sv").stream().findFirst().get())
.isEqualTo("1.0");
assertThat(requestTemplate.queries().get("sig").stream().findFirst().get())
.isEqualTo("flow-sig");

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,31 @@ void updateCaseCancelClosureSuccess() {
verify(caseRepository, times(0)).save(any());
}

@Test
@DisplayName("Should send not try to send email updating case and cancelling closure with no shares")
void updateCaseCancelClosureNoSharesSuccess() {
caseEntity.setState(CaseState.PENDING_CLOSURE);
var caseDTOModel = new CreateCaseDTO(caseEntity);
var share = createShare();
share.setId(UUID.randomUUID());
caseDTOModel.setState(CaseState.OPEN);
caseDTOModel.setClosedAt(null);

when(courtRepository.findById(caseEntity.getCourt().getId())).thenReturn(
Optional.of(caseEntity.getCourt()));
when(caseRepository.findById(caseEntity.getId())).thenReturn(Optional.of(caseEntity));
when(shareBookingService.getSharesForCase(any(Case.class))).thenReturn(Set.of());

caseService.upsert(caseDTOModel);

verify(courtRepository, times(1)).findById(caseDTOModel.getCourtId());
verify(caseRepository, times(1)).findById(caseDTOModel.getId());
verify(shareBookingService, times(1)).getSharesForCase(any(Case.class));
verify(caseStateChangeNotifierFlowClient, never()).emailAfterCaseStateChange(anyList());
verify(caseRepository, times(1)).saveAndFlush(any());
verify(caseRepository, times(0)).save(any());
}

@Test
@DisplayName("Should log when an error occurs attempting to send email notification when cancelling closure")
void updateCaseCancelClosureEmailNotificationError() {
Expand Down Expand Up @@ -451,7 +476,7 @@ void updateCasePendingClosureEmailNotificationError() {
verify(courtRepository, times(1)).findById(caseDTOModel.getCourtId());
verify(caseRepository, times(1)).findById(caseDTOModel.getId());
verify(shareBookingService, times(1)).getSharesForCase(any(Case.class));
verify(caseStateChangeNotifierFlowClient, times(1)).emailAfterCaseStateChange(anyList());
verify(caseStateChangeNotifierFlowClient, never()).emailAfterCaseStateChange(anyList());
verify(caseRepository, times(1)).saveAndFlush(any());
verify(caseRepository, times(0)).save(any());
}
Expand Down Expand Up @@ -700,7 +725,7 @@ void checkAndClosePendingCasesSuccess() {
verify(caseRepository).findAllByStateAndClosedAtBefore(eq(CaseState.PENDING_CLOSURE), any());
verify(caseRepository).save(pendingCase);
verify(shareBookingService).deleteCascade(pendingCase);
verify(caseStateChangeNotifierFlowClient, times(1)).emailAfterCaseStateChange(any());
verify(caseStateChangeNotifierFlowClient, never()).emailAfterCaseStateChange(any());
verify(bookingRepository, times(1)).findAllByCaseIdAndDeletedAtIsNull(pendingCase);
}

Expand Down