Skip to content
This repository has been archived by the owner on Nov 11, 2024. It is now read-only.

converted error list to json array to match openapi spec for /resources endpoint #599

Merged
merged 5 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public HttpServletResponse handlePostRequest(String pathInfo, QueryParameters qu
body.close();
KirbyKatcher marked this conversation as resolved.
Show resolved Hide resolved
List<String> errorsList = processRequest(jsonBody);
if (errorsList.size() >0){
response = getResponseBuilder().buildResponse(request, response, "application/json", gson.toJson(errorsList), HttpServletResponse.SC_BAD_REQUEST);
response = getResponseBuilder().buildResponse(request, response, "application/json", getErrorsAsJson(errorsList), HttpServletResponse.SC_BAD_REQUEST);
} else {
response = getResponseBuilder().buildResponse(request, response, "application/json", "", HttpServletResponse.SC_OK);
}
Expand Down Expand Up @@ -105,6 +105,20 @@ private void checkJsonElementIsValidJSON(JsonElement element) throws InternalSer
}
}

/**
*
*
* @param errorsList
* @return String containing the JSON Array of Errors
*/
KirbyKatcher marked this conversation as resolved.
Show resolved Hide resolved
protected String getErrorsAsJson(List<String> errorsList){
JsonArray json = new JsonArray();
for (String error : errorsList){
json.add( gson.fromJson(error, JsonObject.class));
}
return gson.toJson(json);
}

protected void processDataArray(JsonArray jsonArray, String action) throws InternalServletException{
for (JsonElement element: jsonArray){
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static org.assertj.core.api.Assertions.*;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -1322,4 +1323,61 @@ public void TestHandlePOSTwithApplyMultipleExistingAndNewAndNullPropertiesReturn
checkPropertyInNamespace(namespace, propertynametwo, valuetwo);
}

@Test
public void TestGetErrorsAsJsonReturnsJsonString() throws Exception{
// Given...
List<String> errors = new ArrayList<String>();
errors.add("{\"error_code\":5030,\"error_message\":\"GAL5030E: Error occured when trying to delete Property 'property.5'. Report the problem to your Galasa Ecosystem owner.\"}");
errors.add("{\"error_code\":5030,\"error_message\":\"GAL5030E: Error occured when trying to delete Property 'property.1'. Report the problem to your Galasa Ecosystem owner.\"}");
setServlet("framework");
MockResourcesServlet servlet = getServlet();
IFramework framework = servlet.getFramework();
ResourcesRoute resourcesRoute = new ResourcesRoute(null, framework);

// When...
String json = resourcesRoute.getErrorsAsJson(errors);

// Then...
String expectedJson = "[\n {\n \"error_code\": 5030,\n \"error_message\": \"GAL5030E: Error occured when trying to delete Property 'property.5'. Report the problem to your Galasa Ecosystem owner.\"\n },"
+"\n {\n \"error_code\": 5030,\n \"error_message\": \"GAL5030E: Error occured when trying to delete Property 'property.1'. Report the problem to your Galasa Ecosystem owner.\"\n }\n]";
KirbyKatcher marked this conversation as resolved.
Show resolved Hide resolved
assertThat(json).isEqualTo(expectedJson);


}


@Test
public void TestHandlePOSTwithDeleteMultipleExistingPropertiesRaisesExceptionsReturnsErrorArray() throws Exception {
// Given...
String namespace = "framework";
String propertyname = "property.5";
String value = "value5";
String propertynametwo = "property.1";
String valuetwo = "value1";
String apiVersion = "galasa-dev/v1alpha1";
String action = "delete";
String propertyone = generatePropertyJSON(namespace, propertyname, value, apiVersion);
String propertytwo = generatePropertyJSON(namespace, propertynametwo, valuetwo, apiVersion);
String propertyJSON = "{\n \"action\":\""+action+"\", \"data\":["+propertyone+","+propertytwo+"]\n}";
setServlet("/", null, propertyJSON , "POST");
MockResourcesServlet servlet = getServlet();
HttpServletRequest req = getRequest();
HttpServletResponse resp = getResponse();
ServletOutputStream outStream = resp.getOutputStream();

// When...
servlet.init();
servlet.doPost(req, resp);

// Then...
String expectedJson = "[\n {\n \"error_code\": 5030,\n \"error_message\": \"GAL5030E: Error occured when trying to delete Property 'property.5'. Report the problem to your Galasa Ecosystem owner.\"\n },"
+"\n {\n \"error_code\": 5030,\n \"error_message\": \"GAL5030E: Error occured when trying to delete Property 'property.1'. Report the problem to your Galasa Ecosystem owner.\"\n }\n]";
Integer status = resp.getStatus();
String output = outStream.toString();
assertThat(status).isEqualTo(400);
assertThat(resp.getContentType()).isEqualTo("application/json");
assertThat(output).isEqualTo(expectedJson);
checkPropertyNotInNamespace(namespace, propertyname, value);
checkPropertyNotInNamespace(namespace, propertynametwo, valuetwo);
}
}