From 7bf777b912ea8da0c52dc2c79d01a972356f70c5 Mon Sep 17 00:00:00 2001 From: Onur D Date: Tue, 26 Sep 2023 10:55:16 +0200 Subject: [PATCH] Developed a simple REST application, --- .../EndpointFocusAndPrefixRest.java | 240 +++++++++++++++++- .../EndpointFocusAndPrefixRestDTO.java | 23 ++ .../EndpointFocusAndPrefixTest.java | 164 +++--------- 3 files changed, 280 insertions(+), 147 deletions(-) create mode 100644 e2e-tests/spring-rest-openapi-v2/src/main/java/com/foo/rest/examples/spring/endpointFocusAndPrefix/EndpointFocusAndPrefixRestDTO.java diff --git a/e2e-tests/spring-rest-openapi-v2/src/main/java/com/foo/rest/examples/spring/endpointFocusAndPrefix/EndpointFocusAndPrefixRest.java b/e2e-tests/spring-rest-openapi-v2/src/main/java/com/foo/rest/examples/spring/endpointFocusAndPrefix/EndpointFocusAndPrefixRest.java index 1f9a6d8bfa..775a165e43 100644 --- a/e2e-tests/spring-rest-openapi-v2/src/main/java/com/foo/rest/examples/spring/endpointFocusAndPrefix/EndpointFocusAndPrefixRest.java +++ b/e2e-tests/spring-rest-openapi-v2/src/main/java/com/foo/rest/examples/spring/endpointFocusAndPrefix/EndpointFocusAndPrefixRest.java @@ -1,22 +1,238 @@ package com.foo.rest.examples.spring.endpointFocusAndPrefix; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.servlet.view.RedirectView; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import org.springframework.web.bind.annotation.*; -import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.core.MediaType; +import java.util.Arrays; +import java.util.List; +/** + * Application for testing focus and prefix endpoints, inspired from petStore API: + * PetStore API + */ @RestController -@SpringBootApplication(exclude = SecurityAutoConfiguration.class) +@SuppressWarnings("unused") public class EndpointFocusAndPrefixRest { - final String petStoreURL = "https://petstore.swagger.io"; + @ApiOperation("Get a pet according to the given pet id ") + @RequestMapping( + value = "/api/pet/{petId}", + method = RequestMethod.GET, + produces = MediaType.APPLICATION_JSON + ) + public String getPetById( @ApiParam("Value to retrieve") + @PathVariable("petId") + Integer value) { + return value + " retrieved"; + } + + @ApiOperation("Update a pet according to the given pet id") + @RequestMapping( + value = "/api/pet/{petId}", + method = RequestMethod.POST, + produces = MediaType.APPLICATION_JSON + ) + public String updatePetById( @ApiParam("Value to store") + @PathVariable("petId") + Integer value) { + return value + " updated"; + } + + @ApiOperation("Delete a pet according to the given pet id") + @RequestMapping( + value = "/api/pet/{petId}", + method = RequestMethod.DELETE, + produces = MediaType.APPLICATION_JSON + ) + public String deletePetById( @ApiParam("Value to delete") + @PathVariable("petId") + Integer value) { + return value + " deleted"; + } + + @ApiOperation("Upload image of a pet according to the given pet id") + @RequestMapping( + value = "/api/pet/{petId}/uploadImage", + method = RequestMethod.POST, + produces = MediaType.APPLICATION_JSON + ) + public String uploadImageForPet( @ApiParam("Pet ID to upload") + @PathVariable("petId") + Integer value) { + return "Image uploaded for the pet: " + value; + } + + @ApiOperation("Add a pet to the store") + @RequestMapping( + value = "/api/pet", + method = RequestMethod.POST, + produces = MediaType.APPLICATION_JSON + ) + public String addNewPet() { + return "A new pet has been added"; + } + + @ApiOperation("Update an existing pet") + @RequestMapping( + value = "/api/pet", + method = RequestMethod.PUT, + produces = MediaType.APPLICATION_JSON + ) + public String updatePet() { + return "An existing pet has been updated"; + } + + @ApiOperation("Finds pets by the given status") + @RequestMapping( + value = "/api/pet/findByStatus", + method = RequestMethod.GET, + produces = MediaType.APPLICATION_JSON + ) + public List findByStatus() { + return Arrays.asList("pet1", "pet2", "pet3", "pet4"); + } + + @ApiOperation("Finds items in the store inventory") + @RequestMapping( + value = "/api/store/inventory", + method = RequestMethod.GET, + produces = MediaType.APPLICATION_JSON + ) + public List getStoreInventory() { + return Arrays.asList("item", "item2", "item3"); + } + + + @ApiOperation("Find a given order according to orderID ") + @RequestMapping( + value = "/api/store/order/{orderID}", + method = RequestMethod.GET, + produces = MediaType.APPLICATION_JSON + ) + public String findStoreOrderById( @ApiParam("order ID to check") + @PathVariable("orderID") + Integer value) { + return "Information for the order: " + value; + } + + @ApiOperation("Delete a given order according to orderID ") + @RequestMapping( + value = "/api/store/order/{orderID}", + method = RequestMethod.DELETE, + produces = MediaType.APPLICATION_JSON + ) + public String deleteStoreOrderById( @ApiParam("order ID to check") + @PathVariable("orderID") + Integer value) { + return "Deleted the order: " + value; + } + + @ApiOperation("Place an order") + @RequestMapping( + value = "/api/store/order", + method = RequestMethod.POST, + produces = MediaType.APPLICATION_JSON + ) + public String placeOrder() { + return "Order Placed"; + } + + @ApiOperation("Get information about a user") + @RequestMapping( + value = "/api/user/{username}", + method = RequestMethod.GET, + produces = MediaType.APPLICATION_JSON + ) + public String getUserByName( @ApiParam("username to retrieve") + @PathVariable("username") + String name) { + return "Retrieved information for the user " + name; + } + + @ApiOperation("Update information about a user") + @RequestMapping( + value = "/api/user/{username}", + method = RequestMethod.PUT, + produces = MediaType.APPLICATION_JSON + ) + public String updateUserInformation( + @ApiParam("username to update") + @PathVariable("username") + String name) { + return "Updated information for the user " + name; + } + + @ApiOperation("Delete information about a user") + @RequestMapping( + value = "/api/user/{username}", + method = RequestMethod.DELETE, + produces = MediaType.APPLICATION_JSON + ) + public String deleteUserInformation( + @ApiParam("username to delete") + @PathVariable("username") + String name) { + return "Deleted information for the user " + name; + } + + @ApiOperation("Create a new user") + @RequestMapping( + value = "/api/user", + method = RequestMethod.POST, + produces = MediaType.APPLICATION_JSON + ) + public String createUser() { + return "Created a new user"; + } + + @ApiOperation("Create users given with a given list") + @RequestMapping( + value = "/api/user/createWithList", + method = RequestMethod.POST, + produces = MediaType.APPLICATION_JSON + ) + public String createUserWithList( + @RequestBody List + userlist) { + + StringBuilder reportBuilder = new StringBuilder("Creating users\n"); + for(EndpointFocusAndPrefixRestDTO dto : userlist) { + + reportBuilder.append("-------\n"); + reportBuilder.append("ID: ").append(dto.id).append("\n"); + reportBuilder.append("Username: ").append(dto.userName).append("\n"); + reportBuilder.append("Firstname: ").append(dto.firstName).append("\n"); + reportBuilder.append("Lastname: ").append(dto.lastName).append("\n"); + reportBuilder.append("-------\n"); + } + String report = reportBuilder.toString(); + + report = report + "Created all users\n"; + + return report; + } + + @ApiOperation("Logs in a user") + @RequestMapping( + value = "/api/user/login", + method = RequestMethod.GET, + produces = MediaType.APPLICATION_JSON + ) + public String userLogin() { + + return "Logged in a user"; + } + + @ApiOperation("Logs out a user") + @RequestMapping( + value = "/api/user/logout", + method = RequestMethod.GET, + produces = MediaType.APPLICATION_JSON + ) + public String userLogout() { - @RequestMapping(value = "/*") - public void method(HttpServletResponse httpServletResponse) { - httpServletResponse.setHeader("Location", petStoreURL); - httpServletResponse.setStatus(302); + return "Logged out a user"; } } diff --git a/e2e-tests/spring-rest-openapi-v2/src/main/java/com/foo/rest/examples/spring/endpointFocusAndPrefix/EndpointFocusAndPrefixRestDTO.java b/e2e-tests/spring-rest-openapi-v2/src/main/java/com/foo/rest/examples/spring/endpointFocusAndPrefix/EndpointFocusAndPrefixRestDTO.java new file mode 100644 index 0000000000..a4145ccac8 --- /dev/null +++ b/e2e-tests/spring-rest-openapi-v2/src/main/java/com/foo/rest/examples/spring/endpointFocusAndPrefix/EndpointFocusAndPrefixRestDTO.java @@ -0,0 +1,23 @@ +package com.foo.rest.examples.spring.endpointFocusAndPrefix; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +@ApiModel +public class EndpointFocusAndPrefixRestDTO { + + @ApiModelProperty(value ="id", required = true) + public int id; + + @ApiModelProperty(value = "userName", required = true) + public String userName; + + @ApiModelProperty(value = "firstName", required = true) + public String firstName; + + @ApiModelProperty(value = "lastName", required = true) + public String lastName; + + public EndpointFocusAndPrefixRestDTO() { } + +} diff --git a/e2e-tests/spring-rest-openapi-v2/src/test/java/org/evomaster/e2etests/spring/examples/endpointFocusAndPrefix/EndpointFocusAndPrefixTest.java b/e2e-tests/spring-rest-openapi-v2/src/test/java/org/evomaster/e2etests/spring/examples/endpointFocusAndPrefix/EndpointFocusAndPrefixTest.java index 7ab9f5b29f..9d04074780 100644 --- a/e2e-tests/spring-rest-openapi-v2/src/test/java/org/evomaster/e2etests/spring/examples/endpointFocusAndPrefix/EndpointFocusAndPrefixTest.java +++ b/e2e-tests/spring-rest-openapi-v2/src/test/java/org/evomaster/e2etests/spring/examples/endpointFocusAndPrefix/EndpointFocusAndPrefixTest.java @@ -11,19 +11,15 @@ import java.lang.reflect.InvocationTargetException; import java.util.List; -import java.util.concurrent.atomic.AtomicReference; +import java.util.Arrays; import static org.junit.jupiter.api.Assertions.assertThrows; public class EndpointFocusAndPrefixTest extends SpringTestBase { - private final String swaggerURLSample = "https://petstore.swagger.io/v2/swagger.json"; - - @BeforeAll - /* An empty method since the test is going to use petstore API on - So not initiating controller for this test + /* */ public static void initClass() throws Exception { @@ -31,171 +27,69 @@ public static void initClass() throws Exception { } - /* - Black box test with existing endpointFocus - */ - @Test - public void testRunBlackBoxWithFocus() { - - String outputFolder = "EndpointFocus"; - String endpointFocus = "/pet"; - - List args = getArgsWithCompilation( - 10, - outputFolder, - ClassName.get("org.foo.EndpointFocus"), - true); - // program arguments for EvoMaster - args.add("--blackBox"); - args.add("true"); - args.add("--bbSwaggerUrl"); - args.add(swaggerURLSample); - // endpointFocus - args.add("--endpointFocus"); - args.add(endpointFocus); - - Solution solution = initAndRun(args); - - String pathToSearch = "/v2" + endpointFocus; - - assertAllSolutionsHavePathFocus(solution, pathToSearch); - - compile(outputFolder); - - } - - /* - If an endpointFocus which does not exist in the swagger is provided, - the program has to throw an IllegalArgumentException and stop - */ @Test - public void testRunBlackBoxWithPrefix() { + public void testRunBlackWithoutFocusOrPrefix() { - String outputFolder = "EndpointPrefix"; - String endpointPrefix = "/pet"; + String outputFolder = "EndPointFocusAndPrefix"; List args = getArgsWithCompilation( - 10, + 40, outputFolder, - ClassName.get("org.foo.EndpointPrefix"), + ClassName.get("org.foo.NoEndpointFocusNoEndpointPrefix"), true); // program arguments for EvoMaster args.add("--blackBox"); args.add("true"); + args.add("--bbTargetUrl"); + args.add(baseUrlOfSut); args.add("--bbSwaggerUrl"); - args.add(swaggerURLSample); - // endpointFocus - args.add("--endpointPrefix"); - args.add(endpointPrefix); - + args.add(baseUrlOfSut + "/v2/api-docs"); + // no endpointFocus or endpointPrefix is provided Solution solution = initAndRun(args); + List pathsToCheck = Arrays.asList(); - String pathToSearch = "/v2" + endpointPrefix; - - assertAllSolutionsHavePathPrefix(solution, pathToSearch); + // if neither focus nor prefix is provided, then all paths should include empty path as a prefix + assertAllSolutionsHavePathFocusOrPrefixList(solution, pathsToCheck, false); compile(outputFolder); - } @Test - void testNonExistingFocus() { + public void testRunBlackWithFocusNoPrefixTest1() { - String a = null; + String outputFolder = "EndPointFocusAndPrefix"; - String outputFolder = "EndpointFocus"; - String endpointFocusNonExisting = "/nopet"; + String endpointFocus = "/api/pet"; List args = getArgsWithCompilation( - 10, + 40, outputFolder, - ClassName.get("org.foo.EndpointFocus"), + ClassName.get("org.foo.NoEndpointFocusNoEndpointPrefix"), true); - // program arguments for EvoMaster args.add("--blackBox"); args.add("true"); + args.add("--bbTargetUrl"); + args.add(baseUrlOfSut); args.add("--bbSwaggerUrl"); - args.add(swaggerURLSample); - // endpointFocus + args.add(baseUrlOfSut + "/v2/api-docs"); args.add("--endpointFocus"); - args.add(endpointFocusNonExisting); - - assertThrows(InvocationTargetException.class, - ()-> { - // This run should throw exception - initAndRun(args); - }); - - - } - - @Test - void testNonExistingPrefix() { - - String a = null; + args.add(endpointFocus); - String outputFolder = "EndpointFocus"; - String endpointPrefixNonExisting = "/nopet"; - List args = getArgsWithCompilation( - 10, - outputFolder, - ClassName.get("org.foo.EndpointFocus"), - true); - - // program arguments for EvoMaster - args.add("--blackBox"); - args.add("true"); - args.add("--bbSwaggerUrl"); - args.add(swaggerURLSample); - // endpointFocus - args.add("--endpointPrefix"); - args.add(endpointPrefixNonExisting); + // no endpointFocus or endpointPrefix is provided + Solution solution = initAndRun(args); - assertThrows(IllegalArgumentException.class, - ()-> { - // This run should throw exception - initAndRun(args); - }); + // include swagger into possible solutions as /v2/api-docs + List pathsToCheck = Arrays.asList(endpointFocus, "/v2/api-docs"); + // if neither focus nor prefix is provided, then all paths should include empty path as a prefix + assertAllSolutionsHavePathFocusOrPrefixList(solution, pathsToCheck, true); + compile(outputFolder); } - @Test - void testBothFocusAndPrefixProvided() { - String a = null; - - String outputFolder = "EndpointFocus"; - String endpointPrefix = "/pet"; - String endpointFocus = "/user"; - - List args = getArgsWithCompilation( - 10, - outputFolder, - ClassName.get("org.foo.EndpointFocus"), - true); - - // program arguments for EvoMaster - args.add("--blackBox"); - args.add("true"); - args.add("--bbSwaggerUrl"); - args.add(swaggerURLSample); - // endpointFocus - args.add("--endpointPrefix"); - args.add(endpointPrefix); - args.add("--endpointFocus"); - args.add(endpointFocus); - - assertThrows(IllegalArgumentException.class, - ()-> { - // This run should throw exception - initAndRun(args); - }); - - - } }