So I have a json file that has some request and response data, and what I want to accomplish is iterate through this data and create a pact file that uses each request and response.
So at the moment I am using a parameterized test in junit to kinda iterate through our json data, and this basically works except for because the producer name is the same for all pacts, it creates the same file and is overwriting the previous.
private JsonObject requestObject;
private static Gson gson = new Gson();
private static File jsonFile = readJsonFile();
private static int randValue = new Random().nextInt(500);
private static String consmerName = "phx-ev-consumer" + randValue;
#Rule
public PactProviderRuleMk2 provider = new PactProviderRuleMk2("phx-ev-svc-provider", "localhost", 8080, this);
final RestTemplate restTemplate = new RestTemplate();
public EligibilityApiConsumerPactTest(JsonObject requestObject) {
this.requestObject = requestObject;
}
#Parameterized.Parameters
public static Collection primeNumbers() throws JsonSyntaxException, JsonIOException, FileNotFoundException {
return getJson();
}
#Pact(state = "provider accets submit contact form", provider = "phx-ev-svc-provider" , consumer = "phx-ev-consumer")
public RequestResponsePact createFragment(PactDslWithProvider builder) {
Map<String, String> requestHeaders = new HashMap<>();
requestHeaders.put("Content-Type", "application/json");
requestHeaders.put("SM_USER", "wtadmin");
requestHeaders.put("Cookie", "SMCHALLENGE=YES");
// Auth headers
String authString = "wtadmin:labcorp1";
String authEncoded = Base64.getEncoder().encodeToString(authString.getBytes());
requestHeaders.put("Authorization", "Basic " + authEncoded);
Map<String, String> responseHeaders = new HashMap<>();
responseHeaders.put("Content-Type", "application/json");
String jsonRequest = requestObject.get("request").toString();
String jsonResponse = requestObject.get("response").toString();
RequestResponsePact pact = builder.given("phx-eligibility").uponReceiving("Phoenix Eligibility Request")
.method("POST").headers(requestHeaders).body(jsonRequest).path("/phx-rest/eligibility")
.willRespondWith().status(200).headers(responseHeaders).body(jsonResponse).toPact();
return pact;
}
#Test
#PactVerification("phx-ev-svc-provider")
public void runTest() throws IOException {
MultiValueMap<String, String> requestHeaders = new LinkedMultiValueMap<>();
requestHeaders.add("Content-Type", "application/json");
requestHeaders.add("SM_USER", "wtadmin");
requestHeaders.add("Cookie", "SMCHALLENGE=YES");
// Auth headers
String authString = "wtadmin:labcorp1";
String authEncoded = Base64.getEncoder().encodeToString(authString.getBytes());
requestHeaders.add("Authorization", "Basic " + authEncoded);
String jsonRequest = requestObject.get("request").toString();
restTemplate.exchange(provider.getConfig().url() + "/phx-rest/eligibility", HttpMethod.POST,
new HttpEntity<>(jsonRequest, requestHeaders), String.class);
}
public static List<JsonObject> getJson() throws JsonSyntaxException, JsonIOException, FileNotFoundException {
List<JsonObject> results = new ArrayList<JsonObject>();
JsonObject jsonObject = gson.fromJson(new FileReader(jsonFile), JsonObject.class);
JsonArray input = jsonObject.getAsJsonArray("input");
Iterator<JsonElement> iter = input.iterator();
while (iter.hasNext()) {
JsonObject obj = (JsonObject) iter.next();
results.add(obj);
}
return results;
}
public static File readJsonFile() {
File base = new File("");
File inputFile = new File(base.getAbsolutePath() + "/pact/input/eligibility.json");
return inputFile;
}
Not sure if there is a better way to accomplish this, I have looked at the Github for Pact Jvm and looked through stack overflow but have not been able to find someone creating pact files, without statically specifying all of the data.
A Pact file is essentially a JSON document that contains details about a consumer, a provider and a list of interactions. In your case, you seems to have the same consumer and provider, but a JSON file with the requests and responses that make up the interactions.
So you need to create a single pact file, but with an interaction added for each item in your JSON file.
There are a number of ways you can do that, but if you modify your example test, you can chain the calls using the DSL builder by calling .uponReceiving again after the last .body. You can do this in a loop, each additional call to .uponReceiving will start adding a new interaction to the pact. You will have to give each interaction a unique description.
Then call .toPact() at the end to create the final pact.
Related
This is a portion of code(somebody else wrote) that I am trying to understand. I want to print the request sent, but couldn't figure out a way to print url with all the params here.
//Obtain Response object returned by POST call to API with parameters passed in as HashMap
public static Response getPOSTResponse(String query, String endpoint, HashMap<String, String> params) {
//Initialize variables - for url and for data to be sent in POST request (need new hashmap for data due to type)
String base_url = getUrl(endpoint);
StringBuffer url = new StringBuffer();
HashMap<String, List<Integer>> postData = new HashMap<String, List<Integer>>();
url.append(base_url + "?");
params.forEach((k, v) -> {
String key = k;
String value = (params.get(k));
if (key.contains("list")) {
postData.put(key, Collections.singletonList(Integer.parseInt(value)));
} else { //should handle case for any [other] POST urls using parameters
url.append(key);
url.append("=");
url.append(value);
url.append("&");
}
});
final RequestSpecification sender = given().headers("Content-Type", ContentType.JSON,
"Accept", ContentType.JSON);
return sender.when().body(postData).post(url.toString()).then().contentType(ContentType.JSON).extract().response();
}
I am using RestTemplate exchange HttpMethod.POST method to POST to an endpoint. In my test file I am testing for success of the POST method. However with my current tests I am getting 401 Unauthorized error when POST request is made. I need help to Mock the API while making POST request in test file
Here is my main file
#Component
public class DataTestRepo {
private final RestTemplate restTemplate;
private final String url;
private final AllBuilder headersBuilder;
public DataTestRepo(
#Qualifier(Oauth.BEAN_NAME) AllBuilder headersBuilder,
RestTemplate restTemplate, String url) {
this.headersBuilder = headersBuilder;
this.restTemplate = restTemplate;
this.url = url;
}
public ResponseEntity<String> postJson(Set<String> results) {
ResponseEntity<String> result = null;
try {
JSONObject jsonObject = new JSONObject(body);
HttpEntity<String> request = new HttpEntity<String>(jsonObject.toString(), null);
restTemplate.getMessageConverters().add(stringConvertor);
result = restTemplate.exchange(url, HttpMethod.POST,
new HttpEntity<>(request, getHttpHeaders()), String.class);
}
return result;
}
}
Here is my test file
#RunWith(MockitoJUnitRunner.class)
#TestPropertySource
public class DataTestRepoTest {
private static final String url = "http://localhost:8080/data/name";
#Mock
private DataTestRepo DataTestRepo;
RestTemplate restTemplate = new RestTemplate();
#Test
public void restTemplateHttpPost_success() throws URISyntaxException {
URI uri = new URI(url);
Set<String> mockData = Stream.of("A","B").collect(Collectors.toSet());
Map<String, String> body = new HashMap<>();
body.put("Name", "Aws");
JSONObject jsonObject = new JSONObject(body);
HttpEntity<String> request = new HttpEntity<>(jsonObject.toString(), null);
ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.POST,
new HttpEntity<>(request, DataTestRepo.getHttpHeaders()), String.class);
Assert.assertEquals(201, result.getStatusCodeValue());
}
}
You are testing the logic inside DataTestRepo class, so you should not mock it.
RestTemplate is a dependency inside DataTestRepo, so this is exactly what you need to mock.
In general it should look like this inside your test:
#InjectMocks
private DataTestRepo DataTestRepo;
#Mock
RestTemplate restTemplate;
Also, you will have to provide a return value for your mocked dependency, like this:
Mockito.when(restTemplate.exchange(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(new ResponseEntity<>(yourExpectedDataHere, HttpStatus.OK));
enter code here
This is just a simple example. A good practice would be to check that the arguments passed to your mock equal to the expected ones. One way would be to replace ArgumentMatchers.any() with the real expected data. Another is to verify it separately, like this:
Mockito.verify(restTemplate, Mockito.times(1)).exchange(ArgumentsMatchers.eq(yourExpectedDataHere), ArgumentsMatchers.eq(yourExpectedDataHere), ArgumentsMatchers.eq(yourExpectedDataHere), ArgumentsMatchers.eq(yourExpectedDataHere));
This is a great read on this topic: https://reflectoring.io/spring-boot-web-controller-test/
I need to add a subscriber to a mailchimp audience. I am using Java and a Jersey client. I have no trouble fetching the members of the audience using the Mailchimp 3.0 API. What must I change about my post request in order to successfully add a new subscriber? When my code fails, there is no response. Then, when I check the Mailchimp account and see that no new subscriber was added.
class MailchimpClient {
Client client;
String mailchimpUrl;
public MailchimpClient() {
String mailchimpApikey = getAPIKey();
String datacenter = mailchimpApikey.substring(mailchimpApikey.lastIndexOf('-') + 1);
mailchimpUrl = "https://" + datacenter + ".api.mailchimp.com/3.0/";
JacksonJsonProvider jjp = new JacksonJsonProvider();
jjp.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
ClientConfig conf = new ClientConfig();
conf.property(ClientProperties.CONNECT_TIMEOUT, TIMEOUT);
conf.property(ClientProperties.READ_TIMEOUT, TIMEOUT);
conf.register(jjp);
HttpAuthenticationFeature httpAuth = HttpAuthenticationFeature.basic("username", mailchimpApikey);
client = ClientBuilder.newClient(conf).register(httpAuth);
}
public <T> T get(String path, Class<T> responseType) {
T t = client.target(mailchimpUrl).path(path).request(MediaType.APPLICATION_JSON).get(responseType);
client.close();
return t;
}
public <T> T post(String path, Object payload, Class<T> responseType) {
Entity<Object> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
T t = client.target(mailchimpUrl).path(path).request(MediaType.APPLICATION_JSON).post(entity, responseType);
client.close();
return t;
}
String audienceid = xxxxxxx;
MailchimpClient mcc = new MailchimpClient();
MembersResponse mr = mcc.get("lists/" + audienceid + "/members", MembersResponse.class);
addToMailchimpAudience(String audienceid);
private Message addToMailchimpAudience(String audienceid) {
HashMap<String, String> newMember = new HashMap<String, String>();
newMember.put("email_address", "bob#gmail.com");
newMember.put("status", "subscribed");
MailchimpClient mcc = new MailchimpClient();
MembersResponse mr = mcc.post("lists/" + audienceid + "/members", newMember, MembersResponse.class);
logger.info("response: " + mr);
return Message.success("Successfully added new member to mailchimp audience");
}
The request to add new members should be a JSON
Add a Contact to a List/Audience
To add a contact to a list/audience, send a POST request to the List Members endpoint: /3.0/lists/9e67587f52/members/. The request body should be a JSON object that contains the information you want to add, with status and any other required list fields.
{
"email_address": "urist.mcvankab#freddiesjokes.com",
"status": "subscribed",
"merge_fields": {
"FNAME": "Urist",
"LNAME": "McVankab"
}
}
I observed you are sending values in a HasMap, trying converting into a JSON and see if it works
Link for your reference:
https://developer.mailchimp.com/documentation/mailchimp/guides/manage-subscribers-with-the-mailchimp-api/
I want to send JSON as an input from Microservice M1 to a Microservice M2.
M1 and M2 both are on different machines.
I am new to Spring Boot,
I found some code but I am unable to get it.
Please help.
make a class on both microservices or make a jar of that class and add to both microservices so that they both can access the same data.
Lets say the class is
class TestData{
private String name;
private String id;
// getters and setters
}
Now you can send data from M1 to M2 as following
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
TestData data = new TestData();
HttpEntity<?> entity = new HttpEntity<Object>(data,headers);
ResponseEntity<Object> responseEntity = restTemplate.exchange("url", HttpMethod.POST, entity, Object.class);
In Microservice M2 you can write a controller to get the data and process it as follows
#RequestMapping(value="/url",method=RequestMethod.POST)
public Object do(#RequestBody TestData data){
// do something
return //something
}
Let's Say Your Have MicroService1 which needs to send JSONObject => JsonObject to another MicroService2 which is on different Machine but on same network .
Sender Side:
RestTemplate restTemplate = new RestTemplate();
String jsonString = restTemplate.postForObject("http://10.177.7.128:8080/user/insertJsonObject",jsonObject,String.class);
Syntax for restTemplate.postForObject is:
ResponseType var1 = restTemplate.postForObject("network ip Address:portnumber/path",JSONObject,ResponseType)
To Know the URI go to System Preferences > Network
To Receive the object at the receiver Side
#RequestMapping(value="/user/insertJsonObject", method=RequestMethod.POST)
public String updateProductSold(#RequestBody JSONObject jsonObject) {
...Body
...
...
return responseStatus;
Here is the sample code
public class Test {
public static void main(String[] args) {
String jsonString = "{\"id\" : \"123\",\"name\" : \"Tom\",\"class\" : {\"subject\" : \"Math\",\"teacher\" : \"Jack\"}}";
RestTemplate restTemplate = new RestTemplate();
String url = "http://192.1168.1.190:8080/test" // url for second service
System.out.println(responserEntityValue(jsonString,restTemplate,url,HttpMethod.POST,String.class));
}
public ResponseEntity<String> responserEntityValue(final String body, final RestTemplate restTemplate,
final String uRL, final HttpMethod requestMethod, final Class<String> stringClass) {
HttpHeaders headers = new HttpHeaders();
// Set all headers
headers.add(DatabaseConstants.CONTENT_TYPE, "application/json");
HttpEntity<String> request = new HttpEntity<>(body, headers);
return restTemplate.exchange(uRL, requestMethod, request, stringClass);
}
I am trying to update or create xml file if not present. Then I use code below to send the file using PUT method of a service.
public void importClusterProperties(RestManPropertyHolder propertyHolder,File file,String id) throws RestManServiceException {
testRestTemplate = new TestRestTemplate(propertyHolder.getSbusUserName(), propertyHolder.getSbusUserPassword());
String sbusUrl = utils.prepareGatewayURI(propertyHolder);
try {
HttpHeaders requestHeaders = new HttpHeaders();
List <MediaType> mediaTypeList = new ArrayList<MediaType>();
mediaTypeList.add(MediaType.APPLICATION_ATOM_XML);
requestHeaders.setAccept(mediaTypeList);
requestHeaders.setContentType(MediaType.APPLICATION_ATOM_XML);
HttpEntity<String> requestEntity = new HttpEntity<String>(requestHeaders);
// Create the HTTP PUT request,
ResponseEntity<String> response = testRestTemplate.exchange(sbusUrl + "/clusterproperty?",HttpMethod.PUT, requestEntity,String.class);
if (null != response) {
System.out.println("RESPONSE::" + response.toString());
}
} catch (RestClientException rce) {
System.out.println("REST EXCEPTION:::" + rce.getMessage());
}
}
How to pass raw xml file into RestTemplate without converting it first into a java object?
enter image description here
Convert file to byte array and send it using ByteArrayHttpMessageConverter.
RestTemplate restTemplate = new RestTemplate();
// Add ByteArrayHttpMessageConverter if not present by default.
restTemplate.getMessageConverters().add(
new ByteArrayHttpMessageConverter());
String fileName = "path + file name";
// FileUtils is from Apache Commons IO
// import org.apache.commons.io.FileUtils;
byte[] requestBody = FileUtils.readFileToByteArray(new File(fileName));
HttpEntity<byte[]> requestEntity = new HttpEntity<byte[]>(requestBody , requestHeaders);
// Create the HTTP PUT request,
ResponseEntity<byte[]> response =
restTemplate.exchange("URL ...." , HttpMethod.PUT ,
requestEntity , byte[].class);