Skip to content

Commit

Permalink
test: adds test verifying smartlinking that requires claiming a case
Browse files Browse the repository at this point in the history
  • Loading branch information
Jtang-1 committed Oct 3, 2024
1 parent 3e129e7 commit 9a8c91f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.commcare.formplayer.beans.menus.CommandUtils.NavIconState;
import org.commcare.formplayer.mocks.FormPlayerPropertyManagerMock;
import org.commcare.formplayer.utils.FileUtils;
import org.commcare.formplayer.utils.MockRequestUtils;
import org.commcare.formplayer.utils.WithHqUser;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -34,13 +35,16 @@ public class EndpointLaunchTest extends BaseTestClass {

private final String APP_NAME = "endpoint";

private MockRequestUtils mockRequest;

@Override
@BeforeEach
public void setUp() throws Exception {
super.setUp();
configureRestoreFactory("endpointdomain", "endpointusername");
storageFactoryMock.configure("endpointusername", "endpointdomain", "app_id", "asUser");
FormPlayerPropertyManagerMock.mockAutoAdvanceMenu(storageFactoryMock);
mockRequest = new MockRequestUtils(webClientMock, restoreFactoryMock);
}

@Override
Expand Down Expand Up @@ -275,6 +279,20 @@ public void testPersistentMenuForEndpointLaunchWithoutRespectRelevancy() throws
assertEquals(expectedMenu, formResponse.getPersistentMenu());
}

@Test
@WithHqUser(enabledToggles = {TOGGLE_SESSION_ENDPOINTS})
public void testEndpointsWithSync() throws Exception {
mockRequest.mockPostandUpdateRestore("restores/caseclaim3.xml");
HashMap<String, String> endpointArgs = new HashMap<>();
endpointArgs.put("case_id", "0156fa3e-093e-4136-b95c-01b13dae66c6"); // from case claim response

NewFormResponse formResponse = sessionNavigateWithEndpoint(APP_NAME,
"add_child",
endpointArgs,
NewFormResponse.class);
assert formResponse.getTitle().contentEquals("Add Child");
}

private void configureQueryMock() {
when(webClientMock.postFormData(anyString(), any(Multimap.class)))
.thenReturn(FileUtils.getFile(this.getClass(),
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/commcare/formplayer/utils/MockRequestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.commcare.formplayer.services.RestoreFactory;
import org.commcare.formplayer.web.client.WebClient;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
import org.mockito.verification.VerificationMode;

/**
Expand Down Expand Up @@ -43,6 +44,22 @@ public VerifiedMock mockPost(boolean returnValue, int times) {
};
}

/**
* Mock the post request, verifies it happened, and updates the mocked restore.
*/
public VerifiedMock mockPostandUpdateRestore(String restoreFile) {
Mockito.reset(webClientMock);
Answer<Boolean> answer = invocation -> {
mockRestore(restoreFile);
return true;
};
Mockito.doAnswer(answer).when(webClientMock).caseClaimPost(anyString(), any());

return () -> {
verify(webClientMock, Mockito.times(1)).caseClaimPost(anyString(), any());
};
}

/**
* Mock the restore and verify it happened
*/
Expand Down
20 changes: 20 additions & 0 deletions src/test/resources/archives/endpoint/suite.xml
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,22 @@
</push>
</stack>
</remote-request>
<remote-request>
<post url="http://localhost:8000/a/test-1/phone/claim-case/" relevant="count(instance('casedb')/casedb/case[@case_id=instance('commcaresession')/session/data/case_id]) = 0">
<data key="case_id" ref="instance('commcaresession')/session/data/case_id"/>
</post>
<command id="claim_command.add_child.case_id">
<display>
<text/>
</display>
</command>
<instance id="casedb" src="jr://instance/casedb"/>
<instance id="commcaresession" src="jr://instance/session"/>
<session>
<datum id="case_id" function="instance('commcaresession')/session/data/case_id"/>
</session>
<stack/>
</remote-request>
<endpoint id="caselist">
<stack>
<push>
Expand Down Expand Up @@ -708,6 +724,10 @@
<endpoint id="add_child">
<argument id="case_id"/>
<stack>
<push>
<datum id="case_id" value="$case_id"/>
<command value="'claim_command.add_child.case_id'"/>
</push>
<push>
<command value="'m1'"/>
<datum id="case_id" value="$case_id"/>
Expand Down

0 comments on commit 9a8c91f

Please sign in to comment.