Skip to content

Commit

Permalink
Merge pull request eclipse-tractusx#552 from catenax-ng/feature/TRI-1…
Browse files Browse the repository at this point in the history
…523-fix-ess-state

feat(ess):[TRI-1523] Add state to BpnInvestigationJob which will be u…
  • Loading branch information
ds-jhartmann authored Sep 28, 2023
2 parents 98452b8 + cf2038a commit c26b3c4
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.eclipse.tractusx.irs.component.Jobs;
import org.eclipse.tractusx.irs.component.Submodel;
import org.eclipse.tractusx.irs.component.Summary;
import org.eclipse.tractusx.irs.component.enums.JobState;

/**
* Object to store in cache
Expand All @@ -52,11 +53,12 @@ public class BpnInvestigationJob {
private List<String> incidentBpns;
private List<String> unansweredNotifications;
private List<String> answeredNotifications;
private JobState state;

public static BpnInvestigationJob create(final Jobs jobSnapshot, final String owner,
final List<String> incidentBpns) {
return new BpnInvestigationJob(withOwner(jobSnapshot, owner), incidentBpns, new ArrayList<>(),
new ArrayList<>());
new ArrayList<>(), JobState.RUNNING);
}

private static Jobs withOwner(final Jobs jobSnapshot, final String owner) {
Expand Down Expand Up @@ -129,4 +131,8 @@ public BpnInvestigationJob withAnsweredNotification(final String notificationId)
return this;
}

public BpnInvestigationJob complete() {
this.state = JobState.COMPLETED;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,28 @@ private static Jobs updateState(final BpnInvestigationJob investigationJob) {
final Jobs jobSnapshot = investigationJob.getJobSnapshot();
log.debug("Unanswered Notifications '{}'", investigationJob.getUnansweredNotifications());
log.debug("Answered Notifications '{}'", investigationJob.getAnsweredNotifications());

final JobState jobState = updateJobState(investigationJob);

return jobSnapshot.toBuilder().job(jobSnapshot.getJob().toBuilder().state(jobState).build()).build();
}

private static JobState updateJobState(final BpnInvestigationJob investigationJob) {
if (hasUnansweredNotifications(investigationJob)) {
return jobSnapshot.toBuilder()
.job(jobSnapshot.getJob().toBuilder().state(JobState.RUNNING).build())
.build();
return JobState.RUNNING;
}
return jobSnapshot;
if (hasAnsweredNotifications(investigationJob)) {
return JobState.COMPLETED;
}
return investigationJob.getState();
}

private static boolean hasAnsweredNotifications(final BpnInvestigationJob investigationJob) {
return !investigationJob.getAnsweredNotifications().isEmpty();
}

private static boolean hasUnansweredNotifications(final BpnInvestigationJob job) {
return !job.getUnansweredNotifications().isEmpty();
private static boolean hasUnansweredNotifications(final BpnInvestigationJob investigationJob) {
return !investigationJob.getUnansweredNotifications().isEmpty();
}

public JobHandle startIrsJob(final RegisterBpnInvestigationJob request) {
Expand Down Expand Up @@ -123,6 +134,10 @@ public void handleNotificationCallback(final EdcNotification<ResponseNotificatio
log.debug("Unanswered notifications left: '{}'", job.getUnansweredNotifications());
final UUID jobId = job.getJobSnapshot().getJob().getId();

if (job.getUnansweredNotifications().isEmpty()) {
job = job.complete();
}

bpnInvestigationJobCache.store(jobId, job.update(job.getJobSnapshot(), supplyChainImpacted));
recursiveNotificationHandler.handleNotification(jobId, supplyChainImpacted);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public void handleJobProcessingFinishedEvent(final JobProcessingFinishedEvent jo
supplyChainImpacted);

if (leafNodeIsReached(completedJob) || supplyChainIsImpacted(supplyChainImpacted)) {
bpnInvestigationJobCache.store(completedJobId, investigationJobUpdate);
bpnInvestigationJobCache.store(completedJobId, investigationJobUpdate.complete());
recursiveNotificationHandler.handleNotification(investigationJob.getJobSnapshot().getJob().getId(),
supplyChainImpacted);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ void shouldUpdateJobSnapshotIfNotificationFound() {
.getPayload()
.get("supplyChainImpacted");
assertThat(supplyChainImpacted).isEqualTo("No");
assertThat(job.getState()).isEqualTo(JobState.RUNNING);

assertDoesNotThrow(() -> essService.handleNotificationCallback(edcNotification2));
assertThat(bpnInvestigationJobCache.findAll()).hasSize(1);
Expand All @@ -154,6 +155,7 @@ void shouldUpdateJobSnapshotIfNotificationFound() {
.getPayload()
.get("supplyChainImpacted");
assertThat(supplyChainImpacted2).isEqualTo("Yes");
assertThat(job.getState()).isEqualTo(JobState.COMPLETED);

}

Expand All @@ -164,10 +166,8 @@ void shouldKeepJobInRunningIfNotificationIsOpen() {
final String owner = securityHelperService.getClientIdClaim();
when(securityHelperService.isAdmin()).thenReturn(true);

final BpnInvestigationJob bpnInvestigationJob = BpnInvestigationJob.create(
Jobs.builder().job(Job.builder().id(jobId).owner(owner).build()).build(), owner, new ArrayList<>())
.withNotifications(Collections.singletonList(
notificationId));
final BpnInvestigationJob bpnInvestigationJob = BpnInvestigationJob.create(job(jobId, owner), owner,
new ArrayList<>()).withNotifications(Collections.singletonList(notificationId));
bpnInvestigationJobCache.store(jobId, bpnInvestigationJob);

assertThat(bpnInvestigationJobCache.findAll()).hasSize(1);
Expand All @@ -182,4 +182,64 @@ void shouldThrowResponseStatusExceptionWhenIdDoesntExists() {
assertThrows(ResponseStatusException.class, () -> essService.getIrsJob(jobIdNotExisting));
}

@Test
void shouldCompleteJobIfAllNotificationsSentWereAnswered() {
// Arrange
final String notificationId = UUID.randomUUID().toString();
final String owner = securityHelperService.getClientIdClaim();
when(securityHelperService.isAdmin()).thenReturn(true);

final UUID jobId = UUID.randomUUID();
final Jobs jobSnapshot = job(jobId, owner);
final BpnInvestigationJob bpnInvestigationJob = BpnInvestigationJob.create(jobSnapshot, owner, null)
.withAnsweredNotification(notificationId)
.withNotifications(List.of());
bpnInvestigationJobCache.store(jobId, bpnInvestigationJob);

// Act
final Jobs byJobId = essService.getIrsJob(jobId.toString());

// Assert
assertThat(bpnInvestigationJobCache.findAll()).hasSize(1);
assertThat(byJobId.getJob().getState()).isEqualTo(JobState.COMPLETED);
}

@Test
void shouldCompleteJobIfFinalNotificationWasReceived() {
// Arrange
final String owner = securityHelperService.getClientIdClaim();
when(securityHelperService.isAdmin()).thenReturn(true);

final UUID jobId = UUID.randomUUID();
final Jobs jobSnapshot = job(jobId, owner);
final String notificationId = UUID.randomUUID().toString();
final BpnInvestigationJob bpnInvestigationJob = BpnInvestigationJob.create(jobSnapshot, owner, null)
.update(jobSnapshot, SupplyChainImpacted.NO)
.withNotifications(List.of(notificationId));
bpnInvestigationJobCache.store(jobId, bpnInvestigationJob);
final ResponseNotificationContent resultNo = ResponseNotificationContent.builder().result("No").build();
final EdcNotificationHeader header1 = EdcNotificationHeader.builder()
.notificationId(notificationId)
.originalNotificationId(notificationId)
.build();
final EdcNotification<ResponseNotificationContent> edcNotification2 = EdcNotification.<ResponseNotificationContent>builder()
.header(header1)
.content(resultNo)
.build();

// Act
final Jobs jobBeforeNotificationReceive = essService.getIrsJob(jobId.toString());
essService.handleNotificationCallback(edcNotification2);
final Jobs jobAfterNotificationReceive = essService.getIrsJob(jobId.toString());

// Assert
assertThat(bpnInvestigationJobCache.findAll()).hasSize(1);
assertThat(jobBeforeNotificationReceive.getJob().getState()).isEqualTo(JobState.RUNNING);
assertThat(jobAfterNotificationReceive.getJob().getState()).isEqualTo(JobState.COMPLETED);
}

private Jobs job(final UUID jobId, final String owner) {
final Job job = Job.builder().id(jobId).owner(owner).build();
return Jobs.builder().job(job).build();
}
}

0 comments on commit c26b3c4

Please sign in to comment.