diff --git a/src/main/java/org/commcare/formplayer/application/WebAppContext.java b/src/main/java/org/commcare/formplayer/application/WebAppContext.java index d3e2fee5b..fd084edcc 100644 --- a/src/main/java/org/commcare/formplayer/application/WebAppContext.java +++ b/src/main/java/org/commcare/formplayer/application/WebAppContext.java @@ -230,10 +230,6 @@ public FormplayerSentry raven() { @Bean public FallbackSentryReporter sentryReporter() { return new FallbackSentryReporter(); } - @Bean FormattedQuestionsService formattedQuestionsService() { - return new FormattedQuestionsService(); - } - @Bean @Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS) BrowserValuesProvider browserValuesProvider() { @@ -278,19 +274,4 @@ public SetBrowserValuesAspect setBrowserValuesAspect() { public ArchiveFileRoot formplayerArchiveFileRoot() { return new FormplayerArchiveFileRoot(); } - - @Bean - public QueryRequester queryRequester() { - return new QueryRequester(); - } - - @Bean - public SyncRequester syncRequester() { - return new SyncRequester(); - } - - @Bean - public FormplayerFormSendCalloutHandler formSendCalloutHandler() { - return new FormplayerFormSendCalloutHandler(); - } } diff --git a/src/main/java/org/commcare/formplayer/services/FormattedQuestionsService.java b/src/main/java/org/commcare/formplayer/services/FormattedQuestionsService.java index b6a6bb019..f239bbe97 100644 --- a/src/main/java/org/commcare/formplayer/services/FormattedQuestionsService.java +++ b/src/main/java/org/commcare/formplayer/services/FormattedQuestionsService.java @@ -7,6 +7,7 @@ import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; @@ -15,11 +16,18 @@ * Service that gets HTML formatted questions to display to the user * Implemented by by requesting HQ to generate template */ +@Component public class FormattedQuestionsService { @Autowired RestoreFactory restoreFactory; + @Value("${commcarehq.host}") + private String host; + + @Autowired + RestTemplate restTemplate; + public class QuestionResponse { private String formattedQuestions; private JSONArray questionList; @@ -36,11 +44,8 @@ public JSONArray getQuestionList() { return questionList; } } - @Value("${commcarehq.host}") - private String host; public QuestionResponse getFormattedQuestions(String domain, String appId, String xmlns, String instanceXml) { - RestTemplate restTemplate = new RestTemplate(); MultiValueMap body = new LinkedMultiValueMap(); body.add("instanceXml", instanceXml); diff --git a/src/main/java/org/commcare/formplayer/services/FormplayerFormSendCalloutHandler.java b/src/main/java/org/commcare/formplayer/services/FormplayerFormSendCalloutHandler.java index b746d3934..e58ccf609 100644 --- a/src/main/java/org/commcare/formplayer/services/FormplayerFormSendCalloutHandler.java +++ b/src/main/java/org/commcare/formplayer/services/FormplayerFormSendCalloutHandler.java @@ -26,9 +26,11 @@ public class FormplayerFormSendCalloutHandler implements FormSendCalloutHandler @Autowired RestoreFactory restoreFactory; + @Autowired + RestTemplate restTemplate; + @Override public String performHttpCalloutForResponse(String url, Map paramMap) { - RestTemplate restTemplate = new RestTemplate(); ResponseEntity response = null; UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url); diff --git a/src/main/java/org/commcare/formplayer/services/QueryRequester.java b/src/main/java/org/commcare/formplayer/services/QueryRequester.java index af17a2a17..69887d96b 100644 --- a/src/main/java/org/commcare/formplayer/services/QueryRequester.java +++ b/src/main/java/org/commcare/formplayer/services/QueryRequester.java @@ -2,22 +2,27 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; +@Component public class QueryRequester { + @Autowired + RestTemplate restTemplate; + private final Log log = LogFactory.getLog(QueryRequester.class); public String makeQueryRequest(String uri, HttpHeaders headers) { - RestTemplate restTemplate = new RestTemplate(); ResponseEntity response = null; try { diff --git a/src/main/java/org/commcare/formplayer/services/SyncRequester.java b/src/main/java/org/commcare/formplayer/services/SyncRequester.java index e8ee61c11..6d922fb54 100644 --- a/src/main/java/org/commcare/formplayer/services/SyncRequester.java +++ b/src/main/java/org/commcare/formplayer/services/SyncRequester.java @@ -2,13 +2,19 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.*; +import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import java.util.Arrays; +@Component public class SyncRequester { + @Autowired + RestTemplate restTemplate; + private final Log log = LogFactory.getLog(SyncRequester.class); public ResponseEntity makeSyncRequest(String url, String params, HttpHeaders headers) { @@ -16,7 +22,6 @@ public ResponseEntity makeSyncRequest(String url, String params, HttpHea headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); headers.setAccept(Arrays.asList(MediaType.APPLICATION_FORM_URLENCODED)); HttpEntity entity = new HttpEntity<>(params, headers); - RestTemplate restTemplate = new RestTemplate(); ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, diff --git a/src/main/java/org/commcare/formplayer/services/XFormService.java b/src/main/java/org/commcare/formplayer/services/XFormService.java index 4b4522f99..7fb4efca0 100644 --- a/src/main/java/org/commcare/formplayer/services/XFormService.java +++ b/src/main/java/org/commcare/formplayer/services/XFormService.java @@ -4,15 +4,19 @@ import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; +@Component public class XFormService { @Autowired RestoreFactory restoreFactory; + @Autowired + RestTemplate restTemplate; + public String getFormXml(String formUrl) { - RestTemplate restTemplate = new RestTemplate(); ResponseEntity response = restTemplate.exchange(formUrl, HttpMethod.GET, diff --git a/src/test/java/org/commcare/formplayer/tests/FormattedQuestionsServiceTests.java b/src/test/java/org/commcare/formplayer/tests/FormattedQuestionsServiceTests.java new file mode 100644 index 000000000..c3beb782d --- /dev/null +++ b/src/test/java/org/commcare/formplayer/tests/FormattedQuestionsServiceTests.java @@ -0,0 +1,62 @@ +package org.commcare.formplayer.tests; + +import org.commcare.formplayer.services.FormattedQuestionsService; +import org.commcare.formplayer.services.RestoreFactory; +import org.commcare.formplayer.utils.FileUtils; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.client.AutoConfigureWebClient; +import org.springframework.boot.test.autoconfigure.web.client.RestClientTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.HttpMethod; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.client.MockRestServiceServer; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.test.web.client.match.MockRestRequestMatchers.method; +import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; +import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; + +/** + * Regressions for the formatted questions service + */ +@RunWith(SpringRunner.class) +@RestClientTest({FormattedQuestionsService.class}) +@AutoConfigureWebClient(registerRestTemplate = true) +@TestPropertySource(properties = { + "commcarehq.host=https://test.com" +}) +public class FormattedQuestionsServiceTests { + + @MockBean + RestoreFactory restoreFactory; + + @Autowired + private FormattedQuestionsService service; + + @Autowired + private MockRestServiceServer server; + + @Before + public void setUp() throws Exception { + this.server.reset(); + } + + @Test + public void checkFormattedQuestionsServiceResponse() { + String serverUrl = "https://test.com/a/mydomain/cloudcare/api/readable_questions/"; + + this.server.expect(requestTo(serverUrl)).andExpect(method(HttpMethod.POST)).andRespond( + withSuccess(FileUtils.getFile(this.getClass(), "requests/formatted_questions/base_response.json"), null)); + + FormattedQuestionsService.QuestionResponse response = service.getFormattedQuestions("mydomain", "appid", "xmlns", "instancexml"); + + assertThat(response.getFormattedQuestions()).isEqualTo("formdatatext"); + assertThat(response.getQuestionList().length()).isEqualTo(2); + assertThat(response.getQuestionList().get(0)).isEqualTo("one"); + assertThat(response.getQuestionList().get(1)).isEqualTo("two"); + } +} \ No newline at end of file diff --git a/src/test/java/org/commcare/formplayer/tests/XFormServiceTests.java b/src/test/java/org/commcare/formplayer/tests/XFormServiceTests.java new file mode 100644 index 000000000..c5c9f242c --- /dev/null +++ b/src/test/java/org/commcare/formplayer/tests/XFormServiceTests.java @@ -0,0 +1,48 @@ +package org.commcare.formplayer.tests; + +import org.commcare.formplayer.services.RestoreFactory; +import org.commcare.formplayer.services.XFormService; +import org.commcare.formplayer.utils.FileUtils; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.client.AutoConfigureWebClient; +import org.springframework.boot.test.autoconfigure.web.client.RestClientTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.client.MockRestServiceServer; + +import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; +import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; + +@RunWith(SpringRunner.class) +@AutoConfigureWebClient(registerRestTemplate = true) +@RestClientTest({XFormService.class}) +/** + * Regressions for the submission service processing + */ +public class XFormServiceTests { + + @MockBean + RestoreFactory restoreFactory; + + @Autowired + private XFormService service; + + @Autowired + private MockRestServiceServer server; + @Before + public void setUp() throws Exception { + this.server.reset(); + } + + @Test + public void assertResponse() { + String requestPath = "https://formplayer.test/xform/request/xforms/oqps.xml"; + + this.server.expect(requestTo(requestPath)).andRespond(withSuccess(FileUtils.getFile(this.getClass(), "xforms/oqps.xml"), null)); + + this.service.getFormXml(requestPath); + } +} \ No newline at end of file diff --git a/src/test/resources/requests/formatted_questions/base_response.json b/src/test/resources/requests/formatted_questions/base_response.json new file mode 100644 index 000000000..76291028c --- /dev/null +++ b/src/test/resources/requests/formatted_questions/base_response.json @@ -0,0 +1,4 @@ +{ + "form_data": "formdatatext", + "form_questions": ["one", "two"] +} \ No newline at end of file