From 369cbff09680b01291e65d8f792d42acfabdc40a Mon Sep 17 00:00:00 2001 From: harshwardhan_karkar Date: Thu, 9 Jan 2025 10:58:47 +0530 Subject: [PATCH] Created a Fix --- .../rest/api/test/ArithmeticServiceTest.java | 43 ++++++ .../java/com/rest/api/test/TestMyRESTAPI.java | 132 ++++++++---------- 2 files changed, 100 insertions(+), 75 deletions(-) create mode 100644 src/test/java/com/rest/api/test/ArithmeticServiceTest.java diff --git a/src/test/java/com/rest/api/test/ArithmeticServiceTest.java b/src/test/java/com/rest/api/test/ArithmeticServiceTest.java new file mode 100644 index 0000000..33117bb --- /dev/null +++ b/src/test/java/com/rest/api/test/ArithmeticServiceTest.java @@ -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(); + } +} \ No newline at end of file diff --git a/src/test/java/com/rest/api/test/TestMyRESTAPI.java b/src/test/java/com/rest/api/test/TestMyRESTAPI.java index 2d809e1..fdc40f9 100644 --- a/src/test/java/com/rest/api/test/TestMyRESTAPI.java +++ b/src/test/java/com/rest/api/test/TestMyRESTAPI.java @@ -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); + } +} \ No newline at end of file