Skip to content
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
43 changes: 43 additions & 0 deletions src/test/java/com/rest/api/test/ArithmeticServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.rest.api.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Logger;
public class ArithmeticServiceTest {
private static final String ADDITION_SERVICE_URL = "http://localhost:2222/add?addend1=5&addend2=3";
private static final String SUBTRACTION_SERVICE_URL = "http://localhost:3333/subtract?minuend=10&subtrahend=4";
private static final Logger logger = Logger.getLogger(ArithmeticServiceTest.class.getName());
public static void main(String[] args) {
try {
// Call addition service
String additionResponse = callService(ADDITION_SERVICE_URL);
logger.info("Response from Addition Service: " + additionResponse);
// Call subtraction service
String subtractionResponse = callService(SUBTRACTION_SERVICE_URL);
logger.info("Response from Subtraction Service: " + subtractionResponse);
} catch (IOException e) {
logger.severe("Error occurred while calling services: " + e.getMessage());
e.printStackTrace();
}
}
private static String callService(String serviceUrl) throws IOException {
URL url = new URL(serviceUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/json");
if (connection.getResponseCode() != 200) {
logger.severe("Failed : HTTP error code : " + connection.getResponseCode());
throw new RuntimeException("Failed : HTTP error code : " + connection.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
response.append(line);
}
br.close();
return response.toString();
}
}
132 changes: 57 additions & 75 deletions src/test/java/com/rest/api/test/TestMyRESTAPI.java
Original file line number Diff line number Diff line change
@@ -1,75 +1,57 @@
package com.rest.api.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class TestMyRESTAPI {

@SuppressWarnings("deprecation")
DefaultHttpClient httpClient = null;
HttpResponse response = null;
String serviceURL = null;
public TestMyRESTAPI(String serviceURL) {
httpClient = new DefaultHttpClient();
this.serviceURL = serviceURL;
}

public String executeGETMethod() {
StringBuffer sb = null;
try {

HttpGet getRequest = new HttpGet(serviceURL);
getRequest.addHeader("accept", "application/json");

response = httpClient.execute(getRequest);

if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
}

BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

String output;
sb = new StringBuffer();
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
//System.out.println(output);
sb.append(output);
}

httpClient.getConnectionManager().shutdown();

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();
}


return sb.toString();
}
public static void main(String[] args) {
TestMyRESTAPI tmr = new TestMyRESTAPI("http://localhost:2222/add?addend1=1111&addend2=1123");
//TestMyRESTAPI tmr = new TestMyRESTAPI("http://localhost:3333/subtract?subtract?minuend=12&subtrahend=14");
//TestMyRESTAPI tmr = new TestMyRESTAPI("http://localhost:2222/add?addend1=10&addend2=11");
//TestMyRESTAPI tmr = new TestMyRESTAPI("http://localhost:4444/add?addend1=11&addend2=12");
//TestMyRESTAPI tmr = new TestMyRESTAPI("https://www.msn.com/en-in");
//http://localhost:8765/api/accounts
//TestMyRESTAPI tmr = new TestMyRESTAPI("http://localhost:8765/api/accounts");
String response =tmr.executeGETMethod();
System.out.println("===================================");
System.out.println(response);
System.out.println("===================================");

}

}
package com.rest.api.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Logger;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class TestMyRESTAPI {
@SuppressWarnings("deprecation")
DefaultHttpClient httpClient = null;
HttpResponse response = null;
String serviceURL = null;
private static final Logger logger = Logger.getLogger(TestMyRESTAPI.class.getName());
public TestMyRESTAPI(String serviceURL) {
httpClient = new DefaultHttpClient();
this.serviceURL = serviceURL;
}
public String executeGETMethod() {
StringBuffer sb = null;
try {
HttpGet getRequest = new HttpGet(serviceURL);
getRequest.addHeader("accept", "application/json");
response = httpClient.execute(getRequest);
if (response.getStatusLine().getStatusCode() != 200) {
logger.severe("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
String output;
sb = new StringBuffer();
logger.info("Output from Server .... \n");
while ((output = br.readLine()) != null) {
sb.append(output);
}
httpClient.getConnectionManager().shutdown();
} catch (ClientProtocolException e) {
logger.severe("ClientProtocolException occurred: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
logger.severe("IOException occurred: " + e.getMessage());
e.printStackTrace();
}
return sb.toString();
}
public static void main(String[] args) {
// Existing addition service test
TestMyRESTAPI additionTest = new TestMyRESTAPI("http://localhost:2222/add?addend1=1111&addend2=1123");
String additionResponse = additionTest.executeGETMethod();
logger.info("Addition Response: " + additionResponse);
// New subtraction service test
TestMyRESTAPI subtractionTest = new TestMyRESTAPI("http://localhost:3333/subtract?minuend=12&subtrahend=4");
String subtractionResponse = subtractionTest.executeGETMethod();
logger.info("Subtraction Response: " + subtractionResponse);
}
}