
com.bebound.spring.debug.RequestSimulator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-utils Show documentation
Show all versions of spring-utils Show documentation
Handle Be-Bound requests with a spring server
The newest version!
package com.bebound.spring.debug;
import com.bebound.spring.model.Failure;
import com.bebound.spring.model.Request;
import com.bebound.spring.model.Response;
import com.bebound.spring.model.Success;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
/**
* Created by mbiamont on 07/01/2016.
*/
public class RequestSimulator {
public Response performRequest(Request request, String url) {
Gson gson = new GsonBuilder().create();
String requestBody = gson.toJson(request);
ResponseEntity entity = new TestRestTemplate().postForEntity(url, requestBody, String.class);
Request.MapRequest mapRequest = new GsonBuilder().create().fromJson(requestBody, Request.MapRequest.class);
mapRequest.setJsonParams(gson.toJson(mapRequest.getParameters()));
if(entity == null || !HttpStatus.OK.equals(entity.getStatusCode()))
throw new RuntimeException("Error while performing request.");
JsonParser parser = new JsonParser();
JsonElement jsonRequest = parser.parse(entity.getBody());
return new GsonBuilder().create().fromJson(entity.getBody(), (jsonRequest.getAsJsonObject().has("params")) ? Success.MapSuccess.class : Failure.class);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy