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

Remove remaining non-wired RestTemplate constructors #755

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<String, String> body = new LinkedMultiValueMap<String, String>();

body.add("instanceXml", instanceXml);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ public class FormplayerFormSendCalloutHandler implements FormSendCalloutHandler
@Autowired
RestoreFactory restoreFactory;

@Autowired
RestTemplate restTemplate;

@Override
public String performHttpCalloutForResponse(String url, Map<String, String> paramMap) {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = null;

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> response =
null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@

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<String> makeSyncRequest(String url, String params, HttpHeaders headers) {
log.info(String.format("SyncRequester with url %s and params %s and headers %s", url, params, headers));
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_FORM_URLENCODED));
HttpEntity<String> entity = new HttpEntity<>(params, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response =
restTemplate.exchange(url,
HttpMethod.POST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> response =
restTemplate.exchange(formUrl,
HttpMethod.GET,
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
48 changes: 48 additions & 0 deletions src/test/java/org/commcare/formplayer/tests/XFormServiceTests.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"form_data": "formdatatext",
"form_questions": ["one", "two"]
}