Skip to content
Snippets Groups Projects
Commit 1bc9d9de authored by Harshika Dhoot's avatar Harshika Dhoot
Browse files

logs for negative test case scenarios added

parent 1dcb603e
No related branches found
No related tags found
1 merge request!645logs for negative test case scenarios added
......@@ -14,16 +14,18 @@
package org.opengroup.osdu.util;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import lombok.ToString;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import javax.ws.rs.core.Response;
import java.net.SocketTimeoutException;
import java.util.Map;
@Log
import java.util.UUID;
@Slf4j
@ToString
public class AzureHTTPClient extends HTTPClient {
......@@ -41,42 +43,42 @@ public class AzureHTTPClient extends HTTPClient {
return token;
}
public ClientResponse send(String httpMethod, String url, String payLoad, Map<String, String> headers, String token) {
ClientResponse response;
ClientResponse response = null;
System.out.println("in Azure send method");
String correlationId = java.util.UUID.randomUUID().toString();
log.info(String.format("Request correlation id: %s", correlationId));
headers.put(HEADER_CORRELATION_ID, correlationId);
Client client = getClient();
client.setReadTimeout(300000);
client.setConnectTimeout(300000);
log.info(String.format("httpMethod: %s", httpMethod));
log.info(String.format("payLoad: %s", payLoad));
log.info(String.format("headers: %s", headers));
log.info(String.format("URL: %s", url));
WebResource webResource = client.resource(url);
log.info("waiting on response in azure send");
int retryCount = 2;
try{
response = this.getClientResponse(httpMethod, payLoad, webResource, headers, token);
while (retryCount > 0) {
int count = 1;
int MaxRetry = 3;
while (count < MaxRetry) {
try {
headers.put("correlation-id", headers.getOrDefault("correlation-id", UUID.randomUUID().toString()));
//removing Auth header before logging
headers.remove("Authorization");
log.info(String.format("Request method: %s\nHeaders: %s\nRequest Body: %s", httpMethod, headers, indentatedResponseBody(payLoad)));
log.info(String.format("Attempt: #%s/%s, CorrelationId: %s", count, MaxRetry, headers.get("correlation-id")));
response = this.getClientResponse(httpMethod, payLoad, webResource, headers, token);
if (response.getStatusInfo().getFamily().equals(Response.Status.Family.valueOf("SERVER_ERROR"))) {
log.info(String.format("got resoponse : %s", response.getStatusInfo()));
count++;
Thread.sleep(5000);
log.info(String.format("Retrying --- "));
response = this.getClientResponse(httpMethod, payLoad, webResource, headers, token);
} else
continue;
} else {
break;
retryCount--;
}
System.out.println("sending response from azure send method");
return response;
} catch (Exception e) {
if (e.getCause() instanceof SocketTimeoutException) {
System.out.println("Retrying in case of socket timeout exception");
return this.getClientResponse(httpMethod, payLoad, webResource, headers, token);
}
} catch (Exception ex) {
log.error("Exception While Making Request: ", ex);
count++;
if (count == MaxRetry) {
throw new AssertionError("Error: Send request error", ex);
}
} finally {
if (response != null) {
log.info(String.format("This is the response received : %s\nHeaders: %s\nStatus code: %s", response, response.getHeaders(), response.getStatus()));
}
}
e.printStackTrace();
throw new AssertionError("Error: Send request error", e);
}
return response;
}
}
......@@ -31,6 +31,7 @@ import java.util.logging.Level;
import static org.junit.Assert.*;
import static org.opengroup.osdu.util.Config.getEntitlementsDomain;
import static org.opengroup.osdu.util.Config.getStorageBaseURL;
import static org.opengroup.osdu.util.HTTPClient.indentatedResponseBody;
@Log
public class RecordSteps extends TestsBase {
......@@ -127,6 +128,7 @@ public class RecordSteps extends TestsBase {
String payLoad = new Gson().toJson(records);
log.log(Level.INFO, "Start ingesting records={0}", payLoad);
ClientResponse clientResponse = httpClient.send(HttpMethod.PUT, getStorageBaseURL() + "records", payLoad, headers, httpClient.getAccessToken());
log.info(String.format("Response body: %s\n Correlation id: %s\nResponse Status code: %s", indentatedResponseBody(clientResponse.getEntity(String.class)), clientResponse.getHeaders().get("correlation-id"), clientResponse.getStatus()));
assertEquals(201, clientResponse.getStatus());
} catch (Exception ex) {
throw new AssertionError(ex.getMessage());
......
......@@ -21,6 +21,7 @@ import java.util.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.opengroup.osdu.util.Config.*;
import static org.opengroup.osdu.util.HTTPClient.indentatedResponseBody;
@Log
public abstract class TestsBase {
......@@ -120,10 +121,10 @@ public abstract class TestsBase {
T response = new Gson().fromJson(responseEntity, typeParameterClass);
response.setHeaders(clientResponse.getHeaders());
response.setResponseCode(clientResponse.getStatus());
log.info(String.format("Response body: %s\nCorrelation id: %s\nResponse Status code: %s", indentatedResponseBody(responseEntity), response.getHeaders().get("correlation-id"), response.getResponseCode()));
return response;
}
private void logCorrelationIdWithFunctionName(MultivaluedMap<String, String> headers) {
log.info(String.format("Scenario Name: %s, Correlation-Id: %s", scenario.getId(), headers.get("correlation-id")));
}
......
......@@ -8,11 +8,11 @@ import lombok.Data;
import javax.ws.rs.HttpMethod;
import org.opengroup.osdu.util.HTTPClient;
import org.opengroup.osdu.core.common.model.entitlements.Acl;
import org.opengroup.osdu.core.common.model.legal.Legal;
import org.opengroup.osdu.util.ElasticUtils;
import org.opengroup.osdu.util.FileHandler;
import org.opengroup.osdu.util.HTTPClient;
import org.opengroup.osdu.util.IndexerClientUtil;
import java.util.HashSet;
......@@ -23,6 +23,7 @@ import java.util.logging.Logger;
import static org.junit.Assert.assertEquals;
import static org.opengroup.osdu.util.Config.*;
import static org.opengroup.osdu.util.HTTPClient.indentatedResponseBody;
@Data
public class TestIndex {
......@@ -60,8 +61,9 @@ public class TestIndex {
public void setupSchema() {
ClientResponse clientResponse = this.httpClient.send(HttpMethod.POST, getStorageBaseURL() + "schemas", this.getStorageSchemaFromJson(), headers, httpClient.getAccessToken());
if (clientResponse.getType() != null)
LOGGER.info(String.format("Response status: %s, type: %s", clientResponse.getStatus(), clientResponse.getType().toString()));
if (clientResponse.getType() != null) {
LOGGER.info(String.format("Response status: %s, type: %s\nResponse body: %s", clientResponse.getStatus(), clientResponse.getType().toString(), indentatedResponseBody(clientResponse.getEntity(String.class))));
}
}
public void deleteSchema(String kind) {
......@@ -142,4 +144,4 @@ public class TestIndex {
return legal;
}
}
\ No newline at end of file
}
package org.opengroup.osdu.util;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
......@@ -91,4 +94,14 @@ public abstract class HTTPClient {
currentHeaders.put("data-partition-id", value);
return currentHeaders;
}
public static String indentatedResponseBody(String responseBody) {
JsonParser jsonParser = new JsonParser();
if(responseBody!= null) {
JsonElement jsonElement = jsonParser.parse(responseBody);
String indentedResponseEntity = new GsonBuilder().setPrettyPrinting().create().toJson(jsonElement);
return indentedResponseEntity;
}
return responseBody;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment