improved connection exception and http response code handling

parent cfc94e04
No preview for this file type
......@@ -2,7 +2,7 @@
############# BUNDESVERBAND ENERGIESPEICHER API Validator ##################
############################################################################
Date: 2020-05-06T18:00:28.655789600
Date: 2020-05-11T17:59:13.483607300
Validation Status: FAILED
****************************************************************************
......@@ -13,49 +13,14 @@ Detailed Info:
+ MAIN ENDPOINTS +
++++++++++++++++++++++++++++++++++++++++++++
----------------------------------
ENDPOINT: https://httpstat.us/401
STATUS: BAD - HTTP CODE 401
----------------------------------
ENDPOINT: https://httpstat.us/nameplate
STATUS: BAD - INVALID JSON RESPONSE
----------------------------------
ENDPOINT: https://httpstat.us/data
STATUS: BAD - INVALID JSON RESPONSE
----------------------------------
ENDPOINT: https://httpstat.us/command
ENDPOINT: https://sukisuki.free.beeceptor.com/nameplate
STATUS: OK - HTTP CODE 200
--- ATTRIBUTE LIST ---
NAME STATUS CAUSE
status OK -
++++++++++++++++++++++++++++++++++++++++++++
+ INDIVIDUAL ENDPOINTS +
++++++++++++++++++++++++++++++++++++++++++++
NAME STATUS CAUSE
https://httpstat.us/grid_code BAD invalid json response
https://httpstat.us/status BAD invalid json response
https://httpstat.us/firmware_version BAD invalid json response
https://httpstat.us/uvuvwevwevwe BAD invalid json response
https://httpstat.us/serial_number BAD invalid json response
https://httpstat.us/temperature BAD invalid json response
https://httpstat.us/p_setpoint_discharge_max BAD invalid json response
https://httpstat.us/control_mode BAD invalid json response
https://httpstat.us/hardware_version BAD invalid json response
https://httpstat.us/available_control_modes BAD invalid json response
https://httpstat.us/BVESProtVersion BAD invalid json response
https://httpstat.us/e_capacity_nominal BAD invalid json response
https://httpstat.us/description BAD invalid json response
https://httpstat.us/p_gcp_setpoint BAD invalid json response
https://httpstat.us/p_gcp_l1 BAD invalid json response
https://httpstat.us/p_gcp_l2 BAD invalid json response
https://httpstat.us/p_gcp_l3 BAD invalid json response
https://httpstat.us/cycles BAD invalid json response
https://httpstat.us/p BAD invalid json response
https://httpstat.us/p_setpoint_charge_max BAD invalid json response
https://httpstat.us/p_charge_nominal_max BAD invalid json response
https://httpstat.us/soh BAD invalid json response
https://httpstat.us/p_gcp BAD invalid json response
https://httpstat.us/soc BAD invalid json response
https://httpstat.us/model BAD invalid json response
https://httpstat.us/product_code BAD invalid json response
https://httpstat.us/manufacturer BAD invalid json response
https://httpstat.us/p_discharge_nominal_max BAD invalid json response
https://sukisuki.free.beeceptor.com/status BAD HTTP CODE 401
This diff is collapsed.
......@@ -12,6 +12,7 @@ public class Reporter {
//main endpoints variables
private HashMap<Integer, String> mUrlnoUrls; //endpoint number and url
private HashMap<Integer, Integer> mUrlnoHttpcode; //endpoint number and http response code
private HashMap<Integer, String> mUrlnoConnectionerror; //endpoint number and connection error if any
private HashMap<String, Integer> mAttnameUrlno; //endpoint number and attribute name
private HashMap<String, Boolean> mAttnameStatus; //attribute name and status
private HashMap<String, String> mAttnameCause; //attribute name and cause of error
......@@ -22,14 +23,16 @@ public class Reporter {
public Reporter(HashMap<Integer, String> mUrlnoUrls,
HashMap<Integer, Integer> mUrlnoHttpcode,
HashMap<Integer, String> mUrlnoConnectionerror,
HashMap<String, Integer> mAttnameUrlno,
HashMap<String, Boolean> mAttnameStatus,
HashMap<String, String> mAttnameCause,
HashMap<String, Boolean> iUrlStatus,
HashMap<String, String> iUrlCause) {
this.mAttnameUrlno = mAttnameUrlno;
this.mUrlnoHttpcode = mUrlnoHttpcode;
this.mUrlnoUrls = mUrlnoUrls;
this.mUrlnoConnectionerror = mUrlnoConnectionerror;
this.mAttnameUrlno = mAttnameUrlno;
this.mAttnameStatus = mAttnameStatus;
this.mAttnameCause = mAttnameCause;
this.iUrlStatus = iUrlStatus;
......@@ -77,8 +80,10 @@ public class Reporter {
fw.write(String.format("%-10s \t %-80s \r\n","ENDPOINT:", e.getValue()));
fw.write(String.format("%-10s \t","STATUS: "));
if(!(this.mUrlnoHttpcode.get(e.getKey()) == 200 || this.mUrlnoHttpcode.get(e.getKey()) == 201)) {
if(this.mUrlnoHttpcode.get(e.getKey()) == -117) //-117 internally set to represent an invalid json response
if(this.mUrlnoHttpcode.get(e.getKey()) == -117) //-117 internally set to represent an invalid json response, not an actual http response code
fw.write(String.format("%-80s \r\n", "BAD - INVALID JSON RESPONSE"));
else if(this.mUrlnoHttpcode.get(e.getKey()) == -118) //-118 internally set to represent a connection error, not an actual http response code
fw.write(String.format("%-80s \r\n", "BAD - CONNECTION ERROR: " + this.mUrlnoConnectionerror.get(e.getKey())));
else
fw.write(String.format("%-80s \r\n", "BAD - HTTP CODE " + this.mUrlnoHttpcode.get(e.getKey())));
continue; //do not write anything else if the http response code is not 200 OK
......
......@@ -38,6 +38,7 @@ public class validatorMain {
//main endpoints variables
private static HashMap<Integer, String> mUrlnoUrls = new HashMap<Integer,String>(); //endpoint number and url
private static HashMap<Integer, Integer> mUrlnoHttpcode = new HashMap<Integer,Integer>(); //endpoint number and http response code
private static HashMap<Integer, String> mUrlnoConnectionerror = new HashMap<Integer,String>(); //endpoint number and connection error if any
private static HashMap<String, Integer> mAttnameUrlno = new HashMap<String, Integer>(); //endpoint number and attribute name
private static HashMap<String, Boolean> mAttnameStatus = new HashMap<String, Boolean>(); //attribute name and status
private static HashMap<String, String> mAttnameCause = new HashMap<String,String>(); //attribute name and cause of error
......@@ -71,6 +72,8 @@ public class validatorMain {
System.out.println("Requesting token from server... ");
//Note: I think username/password are only needed in header parameters
//when requesting the token! Afterwards only token must be passed down in header
token = requestToken(username,password);
for(Endpoint endpoint : config.getEndpoints().getEndpoint())
......@@ -87,25 +90,31 @@ public class validatorMain {
con.setRequestProperty("Content-Type", "application/json");
if(endpoint.isAuthRequired())
{
con.setRequestProperty("username", username);
con.setRequestProperty("password", password);
//con.setRequestProperty("username", username);
//con.setRequestProperty("password", password);
con.setRequestProperty("api_token", token);
}
if(endpoint.getOperation().equalsIgnoreCase("GET"))
{
con.setRequestMethod("GET");
int responsecode = con.getResponseCode();
int responsecode = 0;
try {
responsecode = con.getResponseCode();
} catch (IOException e) {
responsecode = -118;
System.out.println("Error connecting to server. Cause: " + e.getMessage());
mUrlnoConnectionerror.put(mEndpointIt, e.getMessage());
}
mUrlnoHttpcode.put(mEndpointIt, responsecode);
if(responsecode != 200)
{
error = true;
//reportw.write(String.format("%-10s \t %-80s \r\n","STATUS:", "BAD - HTTP CODE " + responsecode));
System.out.println("Endpoint not found");
System.out.println("Endpoint not found or endpoint error! Http Response code: "+ responsecode);
}
else
{
Scanner sc = new Scanner(url.openStream());
Scanner sc = new Scanner(con.getInputStream());
while(sc.hasNext())
{
inputLine+=sc.nextLine();
......@@ -114,7 +123,6 @@ public class validatorMain {
con.disconnect();
System.out.println(inputLine);
JSONObject jobj = new JSONObject();
JSONParser parse = new JSONParser();
try {
......@@ -281,7 +289,6 @@ public class validatorMain {
e.printStackTrace();
}
}
}
}
}
......@@ -321,26 +328,33 @@ public class validatorMain {
con.setRequestProperty("Content-Type", "application/json");
if(endpoint.isAuthRequired())
{
con.setRequestProperty("username", username);
con.setRequestProperty("password", password);
//con.setRequestProperty("username", username);
//con.setRequestProperty("password", password);
con.setRequestProperty("api_token", token);
}
if(endpoint.getOperation().equalsIgnoreCase("GET"))
{
con.setRequestMethod("GET");
int responsecode = con.getResponseCode();
int responsecode = 0;
try {
responsecode = con.getResponseCode();
} catch (IOException e) {
responsecode = -118;
System.out.println("Error connecting to server. Cause: " + e.getMessage());
iUrlCause.put(url.toString(), "CONNECTION ERROR: " + e.getMessage());
}
if(responsecode != 200)
{
aterr = true;
System.out.println("Endpoint not found or endpoint error! Http Response code:"+ responsecode);
System.out.println("Endpoint not found or endpoint error! Http Response code: "+ responsecode);
iUrlStatus.put(url.toString(), true);
iUrlCause.put(url.toString(), "HTTP CODE " + responsecode);
//reportw.write(String.format("%-25s \t %-25s \r\n", "BAD", "HTTP CODE " + responsecode));
}
else
{
Scanner sc = new Scanner(url.openStream());
//Scanner sc = new Scanner(url.openStream());
Scanner sc = new Scanner(con.getInputStream());
while(sc.hasNext())
{
inputLine+=sc.nextLine();
......@@ -513,8 +527,8 @@ public class validatorMain {
}
}
}
Reporter reporter = new Reporter(mUrlnoUrls, mUrlnoHttpcode,mAttnameUrlno,mAttnameStatus,
mAttnameCause, iUrlStatus, iUrlCause);
Reporter reporter = new Reporter(mUrlnoUrls, mUrlnoHttpcode, mUrlnoConnectionerror,
mAttnameUrlno,mAttnameStatus,mAttnameCause, iUrlStatus, iUrlCause);
reporter.generateReport(file);
//reportw.close();
}
......@@ -574,12 +588,19 @@ public class validatorMain {
}
}
int responsecode = con.getResponseCode();
int responsecode = 0;
try {
responsecode = con.getResponseCode();
} catch (IOException e) {
responsecode = -118;
System.out.println("Error connecting to server. Cause: " + e.getMessage());
mUrlnoConnectionerror.put(it, e.getMessage());
}
mUrlnoHttpcode.put(it, responsecode);
if(responsecode >= 300) {
posterr = true;
//fw.write(String.format("%-10s \t %-80s \r\n","STATUS:", "BAD - HTTP CODE " + responsecode));
System.out.println("Endpoint not found or endpoint error! Http Response code:"+ responsecode);
System.out.println("Endpoint not found or endpoint error! Http Response code: "+ responsecode);
}
else
{
......@@ -656,7 +677,7 @@ public class validatorMain {
public static String requestToken (String username, String password) throws Exception {
String token = null;
URL url = new URL(config.getRootURL() + "/auth");
URL url = new URL(config.getRootURL() + "auth");
String inputLine=new String();
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("username", username);
......@@ -669,7 +690,8 @@ public class validatorMain {
}
else
{
Scanner sc = new Scanner(url.openStream());
//Scanner sc = new Scanner(url.openStream());
Scanner sc = new Scanner(con.getInputStream());
while(sc.hasNext())
{
inputLine+=sc.nextLine();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment