Skip to content

Commit

Permalink
feat: P4ADEV-1678-add-wf-create-debt-position-sync (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
LarissaASLeite authored Dec 23, 2024
1 parent 4bfa024 commit bd8a174
Show file tree
Hide file tree
Showing 16 changed files with 253 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ val openApiToolsVersion = "0.2.6"
val micrometerVersion = "1.4.0"
val temporalVersion = "1.27.0"
val protobufJavaVersion = "3.25.5"
val activitiesVersion = "1.15.1"
val activitiesVersion = "1.21.1"

dependencies {
implementation("org.springframework.boot:spring-boot-starter")
Expand Down
2 changes: 1 addition & 1 deletion gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ io.temporal:temporal-spring-boot-autoconfigure:1.27.0=compileClasspath
io.temporal:temporal-spring-boot-starter:1.27.0=compileClasspath
io.temporal:temporal-test-server:1.27.0=compileClasspath
io.temporal:temporal-testing:1.27.0=compileClasspath
it.gov.pagopa.payhub:p4pa-payhub-activities:1.15.1=compileClasspath
it.gov.pagopa.payhub:p4pa-payhub-activities:1.21.1=compileClasspath
jakarta.activation:jakarta.activation-api:2.1.3=compileClasspath
jakarta.annotation:jakarta.annotation-api:2.1.1=compileClasspath
jakarta.validation:jakarta.validation-api:3.0.2=compileClasspath
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package it.gov.pagopa.pu.workflow.wf.debtposition.createdp;

import io.temporal.client.WorkflowClient;
import it.gov.pagopa.payhub.activities.dto.debtposition.DebtPositionDTO;
import it.gov.pagopa.pu.workflow.service.WorkflowService;
import it.gov.pagopa.pu.workflow.wf.debtposition.createdp.wfsync.CreateDebtPositionSyncWF;
import it.gov.pagopa.pu.workflow.wf.debtposition.createdp.wfsync.CreateDebtPositionSyncWFImpl;
import org.springframework.stereotype.Service;

@Service
public class CreateDebtPositionWfClient {
private final WorkflowService workflowService;

public CreateDebtPositionWfClient(WorkflowService workflowService) {
this.workflowService = workflowService;
}

public String createDPSync(DebtPositionDTO debtPosition) {
String workflowId = String.valueOf(debtPosition.getDebtPositionId());
CreateDebtPositionSyncWF workflow = workflowService.buildWorkflowStub(
CreateDebtPositionSyncWF.class,
CreateDebtPositionSyncWFImpl.TASK_QUEUE,
workflowId);
WorkflowClient.start(workflow::createDPSync, debtPosition);
return workflowId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package it.gov.pagopa.pu.workflow.wf.debtposition.createdp.config;

import io.temporal.workflow.Workflow;
import it.gov.pagopa.payhub.activities.activity.debtposition.ionotification.SendDebtPositionIONotificationActivity;
import it.gov.pagopa.pu.workflow.config.BaseWfConfig;
import it.gov.pagopa.pu.workflow.config.TemporalWFImplementationCustomizer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "workflow.debt-position-creation")
public class CreateDebtPositionWfConfig extends BaseWfConfig {

public SendDebtPositionIONotificationActivity buildSendDebtPositionIONotificationActivityStub() {
return Workflow.newActivityStub(SendDebtPositionIONotificationActivity.class, TemporalWFImplementationCustomizer.baseWfConfig2ActivityOptions(this));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package it.gov.pagopa.pu.workflow.wf.debtposition.createdp.wfsync;

import io.temporal.workflow.WorkflowInterface;
import io.temporal.workflow.WorkflowMethod;
import it.gov.pagopa.payhub.activities.dto.debtposition.DebtPositionDTO;

/**
* Workflow interface for creating a Sync Debt Position Workflow
* */
@WorkflowInterface
public interface CreateDebtPositionSyncWF {

/**
* Workflow method for the Sync Debt Position Workflow
* @param debtPosition the debt position to be created
* */
@WorkflowMethod
void createDPSync(DebtPositionDTO debtPosition);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package it.gov.pagopa.pu.workflow.wf.debtposition.createdp.wfsync;

import io.temporal.spring.boot.WorkflowImpl;
import it.gov.pagopa.payhub.activities.activity.debtposition.ionotification.SendDebtPositionIONotificationActivity;
import it.gov.pagopa.payhub.activities.dto.debtposition.DebtPositionDTO;
import it.gov.pagopa.pu.workflow.wf.debtposition.createdp.config.CreateDebtPositionWfConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import static it.gov.pagopa.pu.workflow.wf.debtposition.createdp.wfsync.CreateDebtPositionSyncWFImpl.TASK_QUEUE;


@Slf4j
@WorkflowImpl(taskQueues = TASK_QUEUE)
public class CreateDebtPositionSyncWFImpl implements CreateDebtPositionSyncWF, ApplicationContextAware {

public static final String TASK_QUEUE = "CreateDebtPositionWf";

private SendDebtPositionIONotificationActivity sendDebtPositionIONotificationActivity;

/**
* Temporal workflow will not allow to use injection in order to avoid <a href="https://docs.temporal.io/workflows#non-deterministic-change">non-deterministic changes</a> due to dynamic reconfiguration.<BR />
* Anyway it allows to override ActivityOptions, but actually it's not supporting the override based on the particular workflow.<BR />
* In {@link it.gov.pagopa.pu.workflow.config.TemporalWFImplementationCustomizer} we are already setting defaults to all workflows.<BR />
* Use this as an example to override based on the particular workflow.
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
CreateDebtPositionWfConfig wfConfig = applicationContext.getBean(CreateDebtPositionWfConfig.class);
sendDebtPositionIONotificationActivity = wfConfig.buildSendDebtPositionIONotificationActivityStub();
}

@Override
public void createDPSync(DebtPositionDTO debtPosition) {
log.info("Starting workflow for ingesting DebtPosition with ID: {}", debtPosition.getDebtPositionId());
sendDebtPositionIONotificationActivity.sendMessage(debtPosition);
log.info("Message to IO sent with payload {}", debtPosition);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import io.temporal.client.WorkflowClient;
import it.gov.pagopa.pu.workflow.service.WorkflowService;
import it.gov.pagopa.pu.workflow.wf.ingestionflow.paymentsreporting.def.PaymentsReportingIngestionWF;
import it.gov.pagopa.pu.workflow.wf.ingestionflow.paymentsreporting.def.PaymentsReportingIngestionWFImpl;
import it.gov.pagopa.pu.workflow.wf.ingestionflow.paymentsreporting.wfingestion.PaymentsReportingIngestionWF;
import it.gov.pagopa.pu.workflow.wf.ingestionflow.paymentsreporting.wfingestion.PaymentsReportingIngestionWFImpl;
import org.springframework.stereotype.Service;

@Service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package it.gov.pagopa.pu.workflow.wf.ingestionflow.paymentsreporting.def;
package it.gov.pagopa.pu.workflow.wf.ingestionflow.paymentsreporting.wfingestion;

import io.temporal.workflow.WorkflowInterface;
import io.temporal.workflow.WorkflowMethod;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package it.gov.pagopa.pu.workflow.wf.ingestionflow.paymentsreporting.def;
package it.gov.pagopa.pu.workflow.wf.ingestionflow.paymentsreporting.wfingestion;

import io.temporal.spring.boot.WorkflowImpl;
import it.gov.pagopa.payhub.activities.activity.ingestionflow.UpdateIngestionFlowStatusActivity;
Expand All @@ -11,7 +11,7 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import static it.gov.pagopa.pu.workflow.wf.ingestionflow.paymentsreporting.def.PaymentsReportingIngestionWFImpl.TASK_QUEUE;
import static it.gov.pagopa.pu.workflow.wf.ingestionflow.paymentsreporting.wfingestion.PaymentsReportingIngestionWFImpl.TASK_QUEUE;

/**
* Workflow implementation for the Payments Reporting Ingestion Workflow
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ workflow:
retry-initial-interval-in-millis: "\${PAYMENTS_REPORTING_ACTIVITY_CONFIG_RETRY_INITIAL_INTERVAL_IN_MILLIS:\${workflow.default.retryInitialIntervalInMillis}}"
retry-backoff-coefficient: "\${PAYMENTS_REPORTING_ACTIVITY_CONFIG_RETRY_BACKOFF_COEFFICIENT:\${workflow.default.retryBackoffCoefficient}}"
retry-maximum-attempts: "\${PAYMENTS_REPORTING_ACTIVITY_CONFIG_RETRY_MAXIMUM_ATTEMPTS:\${workflow.default.retryMaximumAttempts}}"

debt-position-creation:
start-to-close-timeout-in-seconds: "\${DEBT_POSITION_ACTIVITY_CONFIG_START_TO_CLOSE_TIMEOUT_IN_SECONDS:\${workflow.default.startToCloseTimeoutInSeconds}}"
retry-initial-interval-in-millis: "\${DEBT_POSITION_ACTIVITY_CONFIG_RETRY_INITIAL_INTERVAL_IN_MILLIS:\${workflow.default.retryInitialIntervalInMillis}}"
retry-backoff-coefficient: "\${DEBT_POSITION_ACTIVITY_CONFIG_RETRY_BACKOFF_COEFFICIENT:\${workflow.default.retryBackoffCoefficient}}"
retry-maximum-attempts: "\${DEBT_POSITION_ACTIVITY_CONFIG_RETRY_MAXIMUM_ATTEMPTS:\${workflow.default.retryMaximumAttempts}}"
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import io.temporal.client.WorkflowClient;
import io.temporal.client.WorkflowOptions;
import it.gov.pagopa.pu.workflow.wf.ingestionflow.paymentsreporting.def.PaymentsReportingIngestionWF;
import it.gov.pagopa.pu.workflow.wf.ingestionflow.paymentsreporting.def.PaymentsReportingIngestionWFImpl;
import it.gov.pagopa.pu.workflow.wf.ingestionflow.paymentsreporting.wfingestion.PaymentsReportingIngestionWF;
import it.gov.pagopa.pu.workflow.wf.ingestionflow.paymentsreporting.wfingestion.PaymentsReportingIngestionWFImpl;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package it.gov.pagopa.pu.workflow.utilities.faker;

import it.gov.pagopa.payhub.activities.dto.debtposition.DebtPositionDTO;

public class DebtPositionFaker {

public static DebtPositionDTO buildDebtPositionDTO(){
return DebtPositionDTO.builder()
.debtPositionId(1L)
.iupdOrg("codeIud")
.iupdPagopa("gpdIupd")
.status("statusCode")
.ingestionFlowFileLineNumber(1L)
.gpdStatus('G')
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package it.gov.pagopa.pu.workflow.wf.debtposition.createdp;

import it.gov.pagopa.payhub.activities.dto.debtposition.DebtPositionDTO;
import it.gov.pagopa.pu.workflow.service.WorkflowService;
import it.gov.pagopa.pu.workflow.wf.debtposition.createdp.wfsync.CreateDebtPositionSyncWF;
import it.gov.pagopa.pu.workflow.wf.debtposition.createdp.wfsync.CreateDebtPositionSyncWFImpl;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import static it.gov.pagopa.pu.workflow.utilities.faker.DebtPositionFaker.buildDebtPositionDTO;

@ExtendWith(MockitoExtension.class)
class CreateDebtPositionWfClientTest {

@Mock
private WorkflowService workflowServiceMock;
@Mock
private CreateDebtPositionSyncWF wfMock;

private CreateDebtPositionWfClient client;

@BeforeEach
void init(){
client = new CreateDebtPositionWfClient(workflowServiceMock);
}

@AfterEach
void verifyNoMoreInteractions(){
Mockito.verifyNoMoreInteractions(workflowServiceMock);
}

@Test
void whenCreateDPSyncThenSuccess(){
// Given
long id = 1L;
String expectedWorkflowId = String.valueOf(id);
DebtPositionDTO debtPosition = buildDebtPositionDTO();

Mockito.when(workflowServiceMock.buildWorkflowStub(CreateDebtPositionSyncWF.class, CreateDebtPositionSyncWFImpl.TASK_QUEUE, expectedWorkflowId))
.thenReturn(wfMock);

// When
String workflowId = client.createDPSync(debtPosition);

// Then
Assertions.assertEquals(expectedWorkflowId, workflowId);
Mockito.verify(wfMock).createDPSync(debtPosition);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package it.gov.pagopa.pu.workflow.wf.debtposition.createdp.wfsync;

import it.gov.pagopa.payhub.activities.activity.debtposition.ionotification.SendDebtPositionIONotificationActivity;
import it.gov.pagopa.payhub.activities.dto.debtposition.DebtPositionDTO;
import it.gov.pagopa.pu.workflow.wf.debtposition.createdp.config.CreateDebtPositionWfConfig;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ApplicationContext;

import static it.gov.pagopa.pu.workflow.utilities.faker.DebtPositionFaker.buildDebtPositionDTO;

@ExtendWith(MockitoExtension.class)
class CreateDebtPositionSyncWFTest {

@Mock
private SendDebtPositionIONotificationActivity sendDebtPositionIONotificationActivityMock;

private CreateDebtPositionSyncWFImpl wf;

@BeforeEach
void init() {
CreateDebtPositionWfConfig createDebtPositionWfConfigMock = Mockito.mock(CreateDebtPositionWfConfig.class);
ApplicationContext applicationContextMock = Mockito.mock(ApplicationContext.class);

Mockito.when(createDebtPositionWfConfigMock.buildSendDebtPositionIONotificationActivityStub())
.thenReturn(sendDebtPositionIONotificationActivityMock);

Mockito.when(applicationContextMock.getBean(CreateDebtPositionWfConfig.class))
.thenReturn(createDebtPositionWfConfigMock);

wf = new CreateDebtPositionSyncWFImpl();
wf.setApplicationContext(applicationContextMock);
}

@AfterEach
void verifyNoMoreInteractions() {
Mockito.verifyNoMoreInteractions(sendDebtPositionIONotificationActivityMock);
}

@Test
void givenCreateDPSyncThenSuccess() {
// Given
DebtPositionDTO debtPosition = buildDebtPositionDTO();

Mockito.doNothing().when(sendDebtPositionIONotificationActivityMock)
.sendMessage(debtPosition);

// When
wf.createDPSync(debtPosition);

// Then
Mockito.verify(sendDebtPositionIONotificationActivityMock).sendMessage(debtPosition);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package it.gov.pagopa.pu.workflow.wf.ingestionflow.paymentsreporting;

import it.gov.pagopa.pu.workflow.service.WorkflowService;
import it.gov.pagopa.pu.workflow.wf.ingestionflow.paymentsreporting.def.PaymentsReportingIngestionWF;
import it.gov.pagopa.pu.workflow.wf.ingestionflow.paymentsreporting.def.PaymentsReportingIngestionWFImpl;
import it.gov.pagopa.pu.workflow.wf.ingestionflow.paymentsreporting.wfingestion.PaymentsReportingIngestionWF;
import it.gov.pagopa.pu.workflow.wf.ingestionflow.paymentsreporting.wfingestion.PaymentsReportingIngestionWFImpl;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package it.gov.pagopa.pu.workflow.wf.ingestionflow.paymentsreporting.def;
package it.gov.pagopa.pu.workflow.wf.ingestionflow.paymentsreporting.wfingestion;

import it.gov.pagopa.payhub.activities.activity.ingestionflow.UpdateIngestionFlowStatusActivity;
import it.gov.pagopa.payhub.activities.activity.paymentsreporting.PaymentsReportingIngestionFlowFileActivity;
Expand Down

0 comments on commit bd8a174

Please sign in to comment.