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

add method to check if event exists for case number & flag #499

Merged
merged 1 commit into from
Sep 2, 2021
Merged
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 pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<artifactId>opensrp-server-core</artifactId>
<packaging>jar</packaging>
<version>2.12.6-SNAPSHOT</version>
<version>2.12.7-SNAPSHOT</version>
<name>opensrp-server-core</name>
<description>OpenSRP Server Core module</description>
<url>https://github.com/OpenSRP/opensrp-server-core</url>
Expand Down
24 changes: 10 additions & 14 deletions src/main/java/org/opensrp/service/EventService.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,18 +205,6 @@ && getByBaseEntityAndFormSubmissionId(event.getBaseEntityId(), event.getFormSubm
"An event already exists with given baseEntity and formSubmission combination. Consider updating");
}

if (event.getEventType() != null
&& event.getEventType().equals(EVENT_TYPE_CASE_DETAILS)
&& event.getDetails() != null
&& StringUtils.isNotBlank(event.getDetails().get(CASE_NUMBER))){
String caseNumber = event.getDetails().get(CASE_NUMBER);
String flag = event.getDetails().get(FLAG);
boolean caseEventExists = checkIfEventExists(caseNumber, flag);
if (caseEventExists){
throw new DuplicateKeyException("An event already exists with case_number " + caseNumber + ", and flag '" + flag + "'");
}
}

event.setDateCreated(DateTime.now());
allEvents.add(event);
triggerPlanEvaluation(event, username);
Expand Down Expand Up @@ -702,8 +690,16 @@ public ExportImagesSummary getImagesMetadataForFlagProblemEvent(String planIdent

}

public boolean checkIfEventExists(@NonNull String caseNumber, @NonNull String flag){
return allEvents.checkEventExists(caseNumber, flag);
public boolean checkIfCaseTriggeredEventExists(@NonNull Event event){
if (StringUtils.isNotBlank(event.getEventType())
&& event.getEventType().equals(EVENT_TYPE_CASE_DETAILS)
&& event.getDetails() != null
&& StringUtils.isNotBlank(event.getDetails().get(CASE_NUMBER))) {
String caseNumber = event.getDetails().get(CASE_NUMBER);
String flag = event.getDetails().get(FLAG);
return allEvents.checkEventExists(caseNumber, flag);
}
return false;
}

}
1 change: 0 additions & 1 deletion src/main/java/org/opensrp/service/PlanService.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,5 +293,4 @@ public Long countPlansByUsernameAndServerVersion(String username, long serverVer
}
return 0l;
}

}
34 changes: 34 additions & 0 deletions src/test/java/org/opensrp/domain/LocationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.opensrp.domain;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class LocationTest {

private Location location;

@Before
public void setUp(){
location = new Location();
}

@Test
public void testGetLocationId() {
String id = "cc3386a7-1d58-41e4-9916-7e1f7908416b";
location.setLocationId(id);

assertEquals(id, location.getLocationId());
}

@Test
public void testWithLocationId() {
String id = "cc3386a7-1d58-41e4-9916-7e1f7908416b";
Location location1 = location.withLocationId(id);

assertEquals(location, location1);
assertEquals(id, location1.getLocationId());
}

}
33 changes: 25 additions & 8 deletions src/test/java/org/opensrp/service/EventServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.opensrp.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -172,6 +173,7 @@ public void testAddEvent() {
eventService.addEvent(event, username);

event = eventService.findByFormSubmissionId("gjhg34534 nvbnv3345345__4");
assertNotNull(eventService.getById(event.getId()));
assertEquals("435534534543", event.getBaseEntityId());
assertEquals("Growth Monitoring", event.getEventType());
assertEquals(1, event.getObs().size());
Expand Down Expand Up @@ -234,8 +236,8 @@ public void testAddEventShouldEvaluatePlan() {
verify(taskGenerator, times(1)).processPlanEvaluation(planDefinitionArgumentCaptor.capture(),stringArgumentCaptor.capture(),eventArgumentCaptor.capture());
}

@Test(expected = DuplicateKeyException.class)
public void testAddCaseTriggeredEventThrowsExceptionOnDuplicate() {
@Test
public void testCheckIfCaseTriggeredEventExistsReturnsForCaseTriggeredEvent() {
Obs obs = new Obs("concept", "decimal", "1730AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", null, "3.5", null, "weight");
Event event = new Event()
.withBaseEntityId("4355345345488")
Expand All @@ -252,14 +254,29 @@ public void testAddCaseTriggeredEventThrowsExceptionOnDuplicate() {
Mockito.doNothing().when(taskGenerator).processPlanEvaluation(any(PlanDefinition.class), anyString(), any(Event.class));
eventService.addEvent(event, username);

Event savedEvent = eventService.findByFormSubmissionId("gjhg34534 nvbnv3345345__16");
assertNotNull(savedEvent);
assertNotNull(savedEvent.getId());
assertNotNull(eventService.getById(savedEvent.getId()));

// add as duplicate with different FormSubmissionId
Event duplicateEvent = event.withFormSubmissionId("gjhg34534 nvbnv3345345__3444556");
eventService.addEvent(duplicateEvent, username);
assertTrue(eventService.checkIfCaseTriggeredEventExists(duplicateEvent));
}

@Test
public void testCheckIfCaseTriggeredEventExists() {
Obs obs = new Obs("concept", "decimal", "1730AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", null, "3.5", null, "weight");
Event event = new Event()
.withBaseEntityId("4355345")
.withFormSubmissionId("gjhg34534 nvb__16")
.withEventDate(new DateTime())
.withObs(obs);
PlanDefinition plan = new PlanDefinition();
plan.setIdentifier("identifier");

when(planRepository.get(anyString())).thenReturn(plan);
Mockito.doNothing().when(taskGenerator).processPlanEvaluation(any(PlanDefinition.class), anyString(), any(Event.class));
eventService.addEvent(event, username);

// add as duplicate with different FormSubmissionId
Event duplicateEvent = event.withFormSubmissionId("gjhg34534 nvb__16");
assertFalse(eventService.checkIfCaseTriggeredEventExists(duplicateEvent));
}

@Test
Expand Down