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

Inline Case Search Endpoint Support #1454

Merged
merged 4 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Vector;

import datadog.trace.api.Trace;
Expand Down Expand Up @@ -133,7 +134,7 @@ public void rebuildSessionFromFrame(MenuSession menuSession, CaseSearchHelper ca
throw new CommCareSessionException("Query URL format error: " + e.getMessage(), e);
}
ImmutableMultimap.Builder<String, String> dataBuilder = ImmutableMultimap.builder();
step.getExtras().forEach((key, value) -> dataBuilder.put(key, value.toString()));
step.getExtras().forEach((key, value) -> dataBuilder.putAll(key, ((Collection)value)));
try {
ExternalDataInstance searchDataInstance = caseSearchHelper.getRemoteDataInstance(
queryScreen.getQueryDatum().getDataId(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.commcare.formplayer.services;

import static org.commcare.data.xml.VirtualInstances.buildSelectedValuesInstance;
import static org.commcare.formplayer.util.Constants.TOGGLE_SESSION_ENDPOINTS;
import static org.commcare.formplayer.util.Constants.TOGGLE_SPLIT_SCREEN_CASE_SEARCH;
import static org.javarosa.core.model.instance.ExternalDataInstance.JR_SELECTED_ENTITIES_REFERENCE;
Expand Down Expand Up @@ -73,6 +72,7 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;

import javax.annotation.Resource;
Expand Down Expand Up @@ -705,7 +705,9 @@ public BaseResponseBean advanceSessionWithEndpoint(MenuSession menuSession, Stri
EvaluationContext evalContext = sessionWrapper.getEvaluationContext();
try {
if (endpointArgs != null) {
processEndpointArgumentsForVirualInstance(endpoint, endpointArgs);
Map<String, ExternalDataInstance> instances = processEndpointArgumentsForVirualInstance(endpoint,
endpointArgs);
evalContext = evalContext.spawnWithCleanLifecycle(instances);
Endpoint.populateEndpointArgumentsToEvaluationContext(endpoint, endpointArgs, evalContext);
}
} catch (Endpoint.InvalidEndpointArgumentsException ieae) {
Expand All @@ -722,7 +724,6 @@ public BaseResponseBean advanceSessionWithEndpoint(MenuSession menuSession, Stri
throw new RuntimeException(
String.format("Invalid arguments supplied for link.%s%s", missingMessage, unexpectedMessage));
}

// Sync requests aren't run when executing operations, so stop and check for them after each operation
for (StackOperation op : endpoint.getStackOperations()) {
sessionWrapper.executeStackOperations(new Vector<>(Arrays.asList(op)), evalContext);
Expand All @@ -745,30 +746,33 @@ public BaseResponseBean advanceSessionWithEndpoint(MenuSession menuSession, Stri
return advanceSessionWithSelections(menuSession, selections, null);
}

private void processEndpointArgumentsForVirualInstance(Endpoint endpoint,
private Map<String, ExternalDataInstance> processEndpointArgumentsForVirualInstance(Endpoint endpoint,
HashMap<String, String> argumentValues) {
Map<String, ExternalDataInstance> instances = new HashMap<>();
for (EndpointArgument argument : endpoint.getArguments()) {
if(argument.isInstanceArgument()){
String argumentValue = argumentValues.get(argument.getId());
String instanceSrc = argument.getInstanceSrc();
if (instanceSrc.contentEquals(JR_SELECTED_ENTITIES_REFERENCE)) {
String[] selectedEntites = argumentValue.split(",");
String uuid = storeInSelectedEntitiesInstance(argument, selectedEntites);
argumentValues.put(argument.getId(), uuid);
Pair<String, ExternalDataInstance> guidAndInstance = storeInSelectedEntitiesInstance(argument,
selectedEntites);
argumentValues.put(argument.getId(), guidAndInstance.first);
ExternalDataInstance instance = guidAndInstance.second;
instances.put(instance.getInstanceId(), instance);
} else {
throw new RuntimeException(
"Invalid instance-src defined for argument " + argument.getId() + " for endpoint "
+ endpoint.getId());
}
}
}
return instances;
}

private String storeInSelectedEntitiesInstance(EndpointArgument argument, String[] selectedEntites) {
Pair<String, ExternalDataInstance> guidAndInstance =
VirtualInstances.storeSelectedValuesInInstance(virtualDataInstanceService, selectedEntites,
private Pair<String, ExternalDataInstance> storeInSelectedEntitiesInstance(EndpointArgument argument, String[] selectedEntites) {
return VirtualInstances.storeSelectedValuesInInstance(virtualDataInstanceService, selectedEntites,
argument.getInstanceId());
return guidAndInstance.first;
}

public CaseSearchHelper getCaseSearchHelper() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@

import static org.commcare.formplayer.util.Constants.TOGGLE_SESSION_ENDPOINTS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.google.common.collect.Multimap;

import org.apache.commons.lang3.ArrayUtils;
import org.commcare.formplayer.beans.FormEntryResponseBean;
import org.commcare.formplayer.beans.NewFormResponse;
import org.commcare.formplayer.beans.menus.CommandListResponseBean;
import org.commcare.formplayer.beans.menus.EntityListResponse;
import org.commcare.formplayer.junit.RestoreFactoryAnswer;
import org.commcare.formplayer.mocks.FormPlayerPropertyManagerMock;
import org.commcare.formplayer.utils.MockRequestUtils;
import org.commcare.formplayer.utils.WithHqUser;
Expand All @@ -27,7 +25,6 @@
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.cache.CacheManager;
Expand All @@ -53,6 +50,10 @@ public class MultiSelectCaseClaimTest extends BaseTestClass {

@Captor
ArgumentCaptor<MultiValueMap<String, String>> requestDataCaptor;

@Captor
ArgumentCaptor<Multimap<String, String>> queryRequestDataCaptor;

private MockRequestUtils mockRequest;

@Override
Expand Down Expand Up @@ -332,4 +333,62 @@ public void testMultiSelectEndpointWithClaim_ValidSelection() throws Exception {
checkForSelectedEntitiesInstance(formResponse.getSessionId(), selectedValues);
}
}

@Test
@WithHqUser(enabledToggles = {TOGGLE_SESSION_ENDPOINTS})
public void testCaseListEndpointWithInlineCaseSearch() throws Exception {
try (MockRequestUtils.VerifiedMock ignore = mockRequest.mockQuery(
"query_responses/case_search_multi_select_response.xml")) {
EntityListResponse response = sessionNavigateWithEndpoint(APP_NAME,
"inline_case_search_list_without_selection",
null,
EntityListResponse.class);
assertNotNull(response.getEntities());
}
}


@Test
@WithHqUser(enabledToggles = {TOGGLE_SESSION_ENDPOINTS})
public void testCaseListSelectionEndpointWithInlineCaseSearch() throws Exception {
String[] selectedValues = new String[]{"94f8d030-c6f9-49e0-bc3f-5e0cdbf10c18",
"0156fa3e-093e-4136-b95c-01b13dae66c7",
"0156fa3e-093e-4136-b95c-01b13dae66c8"};
String selectedValuesArg = String.join(",", selectedValues);
HashMap<String, String> endpointArgs = new HashMap<>();
endpointArgs.put("selected_cases", selectedValuesArg);
try (MockRequestUtils.VerifiedMock ignore = mockRequest.mockQuery(
"query_responses/case_search_multi_select_response.xml", 2)) {
CommandListResponseBean response = sessionNavigateWithEndpoint(APP_NAME,
"inline_case_search_list",
endpointArgs,
CommandListResponseBean.class);
assertEquals(response.getCommands().length, 1);
}
verify(webClientMock, times(2)).postFormData(urlCaptor.capture(),
queryRequestDataCaptor.capture());

assertEquals("http://localhost:8000/a/shubham/phone/case_fixture/d54c955d883b4dd99f57571649271af1/", urlCaptor.getAllValues().get(0));
Multimap<String, String> requestData = queryRequestDataCaptor.getAllValues().get(0);
assertEquals(String.join(",",requestData.get("case_id")), String.join(",",selectedValues));
}

@Test
@WithHqUser(enabledToggles = {TOGGLE_SESSION_ENDPOINTS})
public void testFormEndpointWithInlineCaseSearch() throws Exception {
String[] selectedValues = new String[]{"94f8d030-c6f9-49e0-bc3f-5e0cdbf10c18",
"0156fa3e-093e-4136-b95c-01b13dae66c7",
"0156fa3e-093e-4136-b95c-01b13dae66c8"};
String selectedValuesArg = String.join(",", selectedValues);
HashMap<String, String> endpointArgs = new HashMap<>();
endpointArgs.put("selected_cases", selectedValuesArg);
try (MockRequestUtils.VerifiedMock ignore = mockRequest.mockQuery(
"query_responses/case_search_multi_select_response.xml", 2)) {
NewFormResponse formResponse = sessionNavigateWithEndpoint(APP_NAME,
"inline_case_search_form",
endpointArgs,
NewFormResponse.class);
checkForSelectedEntitiesInstance(formResponse.getSessionId(), selectedValues);
}
}
}
68 changes: 68 additions & 0 deletions src/test/resources/archives/case_claim_with_multi_select/suite.xml
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,33 @@
<instance-datum id="selected_cases" autoselect="true" nodeset="instance('casedb')/casedb/case[@case_type='case'][@status='open']" value="./@case_id" detail-select="m1_case_short_autolaunch_autoselect" detail-confirm="m1_case_long"/>
</session>
</entry>
<entry>
<form>http://openrosa.org/formdesigner/5CCB1614-68B3-44C0-A166-D63AA7C1D4FB</form>
<post url="http://localhost:8000/a/shubham/phone/claim-case/" relevant="$case_id != ''">
<data key="case_id" ref="." nodeset="instance('selected_cases')/results/value" exclude="count(instance('casedb')/casedb/case[@case_id=current()/.]) = 1"/>
</post>
<command id="m2-f0">
<text>
<locale id="forms.m1f1"/>
</text>
</command>
<instance id="casedb" src="jr://instance/casedb"/>
<instance id="results:inline" src="jr://instance/remote/results:inline"/>
<instance id="selected_cases" src="jr://instance/selected-entities/selected_cases"/>
<session>
<query url="http://localhost:8000/a/shubham/phone/search/87189ba3c8bb401490d63e7157f566c1/" storage-instance="results:inline" template="case" default_search="true">
<data key="case_type" ref="'case'"/>
<prompt key="name">
<display>
<text>
<locale id="search_property.m1.name"/>
</text>
</display>
</prompt>
</query>
<instance-datum id="selected_cases" nodeset="instance('results:inline')/results/case[@case_type='case'][@status='open'][not(commcare_is_related_case=true())]" value="./@case_id" detail-select="m1_case_short" detail-confirm="m1_case_long"/>
</session>
</entry>
<menu id="m1">
<text>
<locale id="modules.m1"/>
Expand All @@ -283,6 +310,47 @@
</text>
<command id="m0-f0"/>
</menu>
<menu id="m2-inline-search">
<text>
<locale id="modules.m1"/>
</text>
<command id="m2-f0"/>
</menu>
<endpoint id="inline_case_search_list_without_selection">
<stack>
<push>
<command value="'m2-inline-search'"/>
</push>
</stack>
</endpoint>
<endpoint id="inline_case_search_list">
<argument id="selected_cases" instance-id="selected_cases" instance-src="jr://instance/selected-entities"/>
<stack>
<push>
<command value="'m2-inline-search'"/>
<query id="results:inline" value="http://localhost:8000/a/shubham/phone/case_fixture/d54c955d883b4dd99f57571649271af1/">
<data key="case_type" ref="'case'"/>
<data key="case_id" nodeset="instance('selected_cases')/results/value" ref="."/>
</query>
<instance-datum id="selected_cases" value="$selected_cases"/>
</push>
</stack>
</endpoint>
<endpoint id="inline_case_search_form">
<argument id="selected_cases" instance-id="selected_cases" instance-src="jr://instance/selected-entities"/>
<stack>
<push>
<command value="'m2-inline-search'"/>
<query id="results:inline" value="http://localhost:8000/a/shubham/phone/case_fixture/d54c955d883b4dd99f57571649271af1/">
<data key="case_type" ref="'case'"/>
<data key="case_id" nodeset="instance('selected_cases')/results/value" ref="."/>
</query>
<instance-datum id="selected_cases" value="$selected_cases"/>
<command value="'m2-f0'"/>
</push>
</stack>
</endpoint>

<endpoint id="case_list">
<argument id="selected_cases" instance-id="selected_cases" instance-src="jr://instance/selected-entities"/>
<stack>
Expand Down