HttpDataClient.java
class is sending dataId
to external service and should receive something in response. For my test I should test that RestTemplate and check if I am getting good response.
public class HttpDataClient implements DataClient{
private final static Logger LOGGER = LoggerFactory.getLogger(HttpDataClient.class);
private final RestTemplate restTemplate;
private final ObjectMapper objectMapper = new ObjectMapper();
public HttpDataClient(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@Override
public DataResponse getData(String dataId) {
try{
JsonNode node = restTemplate.exchange(
String.format("/data/{0}", dataId),
HttpMethod.POST,
new HttpEntity<>(buildRequest(dataId), headers()),
JsonNode.class
).getBody();
return dataResponse(node);
}catch (HttpStatusCodeException e) {
String msg = String.format(
"Error getting data for dataId: {0}",
dataId,
e.getStatusCode(),
e.getResponseBodyAsString());
LOGGER.error(msg);
return dataResponse.failed();
}
}
private MultiValueMap<String, String> headers() {
final LinkedMultiValueMap<String, String> mv = new LinkedMultiValueMap<>();
mv.set(HttpHeaders.CONTENT_TYPE, "application/json");
return mv;
}
private DataResponse dataResponse(JsonNode node) {
return DataResponse.dataResponse(
asString(node, "dataId"),
asString(node, "author"),
asString(node, "authorDataId"),
asString(node, "serverSideDataId")
);
}
private JsonNode buildRequest(String dataId) {
ObjectNode root = objectMapper.createObjectNode();
root.put("dataId", dataId);
return root;
}
}
DataResponse.java
public class DataResponse {
public final String dataId;
public final String author;
public final String authorDataId;
public final String serverSideDataId;
public DataResponse(
String dataId,
String author,
String authorDataId,
String serverSideDataId) {
notNull(dataId, "dataId must be set");
notNull(author, "author must be set");
notNull(authorDataId, "authorDataId must be set");
notNull(serverSideDataId, "serverSideDataId must be set");
this.dataId = dataId;
this.author = author;
this.authorDataId = authorDataId;
this.serverSideDataId = serverSideDataId;
}
public static GameDataResponse gameDataResponse(
String gameId,
String vendor,
String vendorGameId,
String serverSideGameId){
return new GameDataResponse(
gameId,
vendor,
vendorGameId,
serverSideGameId);
}
//TODO PD-849 - add faild method
public static GameDataResponse failed() {
return new GameDataResponse(
failed().gameId,
failed().vendor,
failed().vendorGameId,
failed().serverSideGameId
);
}
}
Interface:
public interface DataService {
DataResponse getData(String dataId);
}
Here is the test class:
@RunWith(MockitoJUnitRunner.class)
public class HttpDataServiceTest {
RestTemplate restTemplate = Mockito.mock(RestTemplate.class);
private final HttpDataService httpDataService = new HttpDataService(restTemplate);
@Test
public void getData(){
ObjectNode responseNode = JsonNodeFactory.instance.objectNode();
responseNode.put("dataId", "");
responseNode.put("author", "");
responseNode.put("authorDataId", "");
responseNode.put("serverSideDataId", "");
Mockito
.when(restTemplate.exchange(
ArgumentMatchers.eq("/data/dataIdTest"),
ArgumentMatchers.eq(HttpMethod.POST),
ArgumentMatchers.any(),
ArgumentMatchers.<Class<JsonNode>>any()))
.thenReturn(new ResponseEntity<>(responseNode, HttpStatus.OK));
httpDataService.getData("dataIdTest");
}
}
So, when I debug I go trough test class and everything works fine, then it goes to HttpDataClient.java
class to getData() method and when I am in
restTemplate.exchangeI can see that
dataIdis sent, then it goes down to
.getBody();` and it breaks. And I don’t get anything in return.
What am I missing here? Any advice appreciated.
based just on a quick look - try changing
String.format("/data/{0}", dataId)
to
String.format("/data/%s", dataId)
in your DataResponse getData(String dataId) method