Commit 9e28dd43 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

generic test checking input data validation for endpoints

parent a780201c
Pipeline #11764 failed with stage
in 11 minutes and 29 seconds
......@@ -10,6 +10,7 @@ import org.junit.runners.Suite.SuiteClasses;
CommentControllerIntegrationTest.class,
CommentControllerIntegrationTestWithoutTransaction.class,
DrugControllerIntegrationTest.class,
EndPointsInputValidationTests.class,
FileControllerIntegrationTest.class,
FileControllerIntegrationTestWithoutTransaction.class,
MapControllerIntegrationTest.class,
......
package lcsb.mapviewer.web;
import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import java.util.*;
import javax.servlet.http.HttpServletResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
@RunWith(SpringJUnit4ClassRunner.class)
public class EndPointsInputValidationTests extends ControllerIntegrationTest {
Logger logger = LogManager.getLogger();
private static String[] testValues = { " ", "-1", "0", "sample", "admin", "1.00,2.00", "17.00", "1" };
private static List<HttpStatus> validResponses = Arrays.asList(HttpStatus.OK, HttpStatus.BAD_REQUEST,
HttpStatus.NOT_FOUND, HttpStatus.FORBIDDEN);
@Autowired
public RequestMappingHandlerMapping requestMappingHandlerMapping;
@Test
public void testResponseStatusCodeFromEndpoints() throws Exception {
// Configurator.setLevel(LogManager.getLogger(BaseController.class).getName(),
// Level.FATAL);
for (RequestMappingInfo t : requestMappingHandlerMapping.getHandlerMethods().keySet()) {
for (String url : t.getPatternsCondition().getPatterns()) {
for (RequestMethod method : t.getMethodsCondition().getMethods()) {
testUrl(url, method);
}
}
}
}
private void testUrl(String url, RequestMethod method) throws Exception {
for (String urlWithData : getAllPossibleUrls(url)) {
RequestBuilder request = null;
switch (method) {
case GET:
request = get(urlWithData);
break;
case PATCH:
request = patch(urlWithData).content("XX=YY");
break;
case POST:
request = post(urlWithData).content("XX=YY");
break;
case DELETE:
request = delete(urlWithData);
break;
default:
fail(method.toString());
}
logger.debug(method + ", " + urlWithData);
HttpServletResponse response = mockMvc.perform(request).andReturn().getResponse();
HttpStatus status = HttpStatus.valueOf(response.getStatus());
assertTrue("[" + method + " \"" + urlWithData + "\"]\tInvalid response: " + status.name(),
validResponses.contains(status));
}
}
private List<String> getAllPossibleUrls(String url) {
List<String> result = new ArrayList<>();
Set<String> parameters = getParameters(url);
if (parameters.size() == 0) {
result.add(url);
} else {
String parameter = "{" + parameters.iterator().next() + "}";
for (String s : testValues) {
result.addAll(getAllPossibleUrls(url.replace(parameter, s)));
}
}
return result;
}
private Set<String> getParameters(String url) {
Set<String> result = new HashSet<>();
int pos = url.indexOf("{");
while (pos > 0) {
int endPos = url.indexOf("}", pos);
result.add(url.substring(pos + 1, endPos));
pos = url.indexOf("{", pos + 1);
}
return result;
}
}
Supports Markdown
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