This is an automated email from the ASF dual-hosted git repository. jamesbognar pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/juneau-petstore.git
commit 9648c8b214f2235f91ff57f7ab56b4624e979276 Author: rasa <[email protected]> AuthorDate: Fri Dec 20 19:48:11 2019 +0200 mock rest tests improvement --- .../juneau/petstore/rest/PetStoreResource.java | 37 +++ .../juneau/petstore/service/PetStoreService.java | 18 ++ .../org/apache/juneau/petstore/test/MockTest.java | 321 +++++++++++---------- 3 files changed, 224 insertions(+), 152 deletions(-) diff --git a/juneau-petstore-server/src/main/java/org/apache/juneau/petstore/rest/PetStoreResource.java b/juneau-petstore-server/src/main/java/org/apache/juneau/petstore/rest/PetStoreResource.java index 9808eae..b0a28ea 100644 --- a/juneau-petstore-server/src/main/java/org/apache/juneau/petstore/rest/PetStoreResource.java +++ b/juneau-petstore-server/src/main/java/org/apache/juneau/petstore/rest/PetStoreResource.java @@ -271,6 +271,18 @@ public class PetStoreResource extends BasicRest implements PetStore { store.removePet(petId); return OK; } + + @RestMethod( + name=DELETE, + path="/pets", + summary="Delete all pets", + description="This can be done only by the logged in user." + + ) + public Ok deleteAllPets() { + store.deleteAllPets(); + return OK; + } //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- // Orders @@ -369,6 +381,18 @@ public class PetStoreResource extends BasicRest implements PetStore { store.removeOrder(orderId); return OK; } + + @RestMethod( + name=DELETE, + path="/orders", + summary="Delete all orders", + description="This can be done only by the logged in user." + + ) + public Ok deleteAllOrders() { + store.deleteAllOrders(); + return OK; + } @Override @RestMethod( @@ -477,6 +501,17 @@ public class PetStoreResource extends BasicRest implements PetStore { store.removeUser(username); return OK; } + @RestMethod( + name=DELETE, + path="/users", + summary="Delete all users", + description="This can be done only by the admin." + + ) + public Ok deleteAllUsers() { + store.deleteAllUsers(); + return OK; + } @Override @RestMethod( @@ -487,6 +522,8 @@ public class PetStoreResource extends BasicRest implements PetStore { tags="user" ) ) + + public Ok login( String username, String password, diff --git a/juneau-petstore-server/src/main/java/org/apache/juneau/petstore/service/PetStoreService.java b/juneau-petstore-server/src/main/java/org/apache/juneau/petstore/service/PetStoreService.java index bb69c2c..540b75b 100644 --- a/juneau-petstore-server/src/main/java/org/apache/juneau/petstore/service/PetStoreService.java +++ b/juneau-petstore-server/src/main/java/org/apache/juneau/petstore/service/PetStoreService.java @@ -331,4 +331,22 @@ public class PetStoreService { private InputStream getStream(String fileName) { return getClass().getResourceAsStream(fileName); } + + + public void deleteAllPets() { + petRepository.deleteAll(); + + } + + + public void deleteAllUsers() { + userRepository.deleteAll(); + + } + + + public void deleteAllOrders() { + orderRepository.deleteAll(); + + } } \ No newline at end of file diff --git a/juneau-petstore-server/src/test/java/org/apache/juneau/petstore/test/MockTest.java b/juneau-petstore-server/src/test/java/org/apache/juneau/petstore/test/MockTest.java index 121b874..3131ad8 100644 --- a/juneau-petstore-server/src/test/java/org/apache/juneau/petstore/test/MockTest.java +++ b/juneau-petstore-server/src/test/java/org/apache/juneau/petstore/test/MockTest.java @@ -4,7 +4,7 @@ import java.io.IOException; import javax.servlet.ServletException; import org.apache.juneau.petstore.App; import org.apache.juneau.petstore.dto.PetStatus; -import org.apache.juneau.petstore.dto.Species; +import org.apache.juneau.petstore.repository.UserRepository; import org.apache.juneau.petstore.rest.PetStoreResource; import org.apache.juneau.rest.mock2.MockRest; import org.junit.Before; @@ -23,6 +23,7 @@ public class MockTest { @Autowired PetStoreResource petStoreResource; MockRest petStoreRest; + UserRepository userRepository; @Before public void setup() { @@ -35,6 +36,7 @@ public class MockTest { // Pets // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- private String createTestPet() throws AssertionError, ServletException, IOException { + String petId = petStoreRest .post("/pet", "{name:'Sunshine',tags:['nice'], price:100.0,species:'BIRD'}") .execute() @@ -43,10 +45,11 @@ public class MockTest { return petId; } - - private void deleteTestPet(String petId) throws AssertionError, ServletException, IOException { + + private void deleteTestPets() throws AssertionError, ServletException, IOException { + petStoreRest - .delete("/pet/" + petId) + .delete("/pets") .execute() .assertStatus(200); @@ -56,157 +59,164 @@ public class MockTest { @Test public void testDeletePet() throws Exception { + String petId = createTestPet(); petStoreRest .delete("/pet/" + petId) .execute() .assertStatus(200); - + } // Getting all pets @Test public void testGettingPets() throws Exception { + String petId = createTestPet(); + petStoreRest .get("/pet") .execute() .assertStatus(200) .assertBody( "[{id:" + petId + ",species:'BIRD',name:'Sunshine',tags:['nice'],price:100.0,status:'AVAILABLE'}]"); - deleteTestPet(petId); + deleteTestPets(); } - // Posting pet + // Posting pet @Test public void testPostPet() throws Exception { - petStoreRest + petStoreRest .post("/pet", "{name:'Sunshine',tags:['nice'], price:100.0,species:'BIRD'}") .execute() - .assertStatus(200) - .assertBody("2"); + .assertStatus(200); - deleteTestPet("2"); + deleteTestPets(); } - // Find pet by Id - - @Test public void testfindPet() throws Exception { - String petId = createTestPet(); - petStoreRest - .get("/pet/" + petId) - .execute() - .assertStatus(200) - .assertBody("{id:"+petId+",species:'BIRD',name:'Sunshine',tags:['nice'],price:100.0,status:'AVAILABLE'}" - ); - deleteTestPet(petId); - - } - - // Find pet by status - - @Test public void testfindPetByStatus() throws Exception { - String petId = createTestPet(); - PetStatus [] status= {PetStatus.AVAILABLE}; - - petStoreRest - .request("GET", "/pet/findByStatus", status) - .execute() - .assertStatus(200) - .assertBody( - "[{id:"+petId+",species:'BIRD',name:'Sunshine',tags:[],price:100.0,status:'AVAILABLE'}]" - ); - deleteTestPet(petId); - } - - // Find pet by tags - - @Test public void testFindPetByTags() throws Exception { - String petId = createTestPet(); - String[] tags= {"nice"}; - - petStoreRest - .request("GET", "/pet/findByTags", tags) - .execute() - .assertStatus(200) - .assertBody( - "[{id:"+petId+",species:'BIRD',name:'Sunshine',tags:['nice'],price:100.0,status:'AVAILABLE'}]" - ); - - } - - - // Updating pet - - @Test public void testUpdatePet() throws Exception { - - String petId = createTestPet(); - petStoreRest - - .put("/pet/" + petId , "{id: "+petId+",name:'Daisy1',price:1000.0,species:'BIRD'tags:['nice'], status:'AVAILABLE' }") - .execute() - .assertStatus(200); - - deleteTestPet(petId); - - } - + @Test + public void testfindPet() throws Exception { + + String petId = createTestPet(); + + petStoreRest + .get("/pet/" + petId) + .execute() + .assertStatus(200) + .assertBody( + "{id:" + petId + ",species:'BIRD',name:'Sunshine',tags:['nice'],price:100.0,status:'AVAILABLE'}"); + + deleteTestPets(); + + } + + // Find pet by status + + @Test + public void testfindPetByStatus() throws Exception { + + String petId = createTestPet(); + PetStatus[] status = { PetStatus.AVAILABLE }; + + petStoreRest + .request("GET", "/pet/findByStatus", status) + .execute() + .assertStatus(200) + .assertBody( + "[{id:" + petId + ",species:'BIRD',name:'Sunshine',tags:[],price:100.0,status:'AVAILABLE'}]"); + + deleteTestPets(); + } + + // Find pet by tags + + @Test + public void testFindPetByTags() throws Exception { + + String petId = createTestPet(); + String[] tags = { "nice" }; + + petStoreRest + .request("GET", "/pet/findByTags", tags) + .execute() + .assertStatus(200) + .assertBody( + "[{id:" + petId + ",species:'BIRD',name:'Sunshine',tags:['nice'],price:100.0,status:'AVAILABLE'}]"); + + deleteTestPets(); + + } + + // Updating pet + + @Test + public void testUpdatePet() throws Exception { + + String petId = createTestPet(); + + petStoreRest + .put("/pet/" + petId, + "{id: " + petId + + ",name:'Daisy1',price:1000.0,species:'BIRD'tags:['nice'], status:'AVAILABLE' }") + .execute() + .assertStatus(200); + + deleteTestPets(); + + } + // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- // Users // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - - private void deleteTestUser(String username) throws AssertionError, ServletException, IOException { - - petStoreRest - .delete("/user/"+ username) - .execute() - .assertStatus(200); - - } - - private String createTestUser(String username) throws AssertionError, ServletException, IOException { - - - petStoreRest - .post("/user", "{username:"+username+",firstName: 'Tom',lastName: 'Simon', userStatus: 'ACTIVE'}") - .execute() - .assertStatus(200); - - return username; - } - + + private void deleteTestUsers() throws AssertionError, ServletException, IOException { + + petStoreRest + .delete("/users/") + .execute() + .assertStatus(200); + + } + + private String createTestUser(String username) throws AssertionError, ServletException, IOException { + + petStoreRest + .post("/user", "{username:" + username + ",firstName: 'Tom',lastName: 'Simon', userStatus: 'ACTIVE'}") + .execute() + .assertStatus(200); + + return username; + } + // Create user - @Test - public void testCreateUser() throws Exception { - - petStoreRest - .post("/user", "{username:'catlover',firstName: 'Tom',lastName: 'Simon', userStatus: 'ACTIVE'}") - .execute() - .assertStatus(200); - - deleteTestUser("catlover"); - } - + @Test + public void testCreateUser() throws Exception { + + petStoreRest + .post("/user", "{username:'catlover',firstName: 'Tom',lastName: 'Simon', userStatus: 'ACTIVE'}") + .execute() + .assertStatus(200); + + deleteTestUsers(); + } + // Delete user - @Test - public void testDeleteUser() throws Exception { - String name="catlover1"; - createTestUser(name); - - petStoreRest - .delete("/user/" + name) - .execute() - .assertStatus(200); - - } - + @Test + public void testDeleteUser() throws Exception { + + petStoreRest + .delete("/user/" + "catlover1") + .execute() + .assertStatus(200); + + } // Create list of users @@ -219,126 +229,133 @@ public class MockTest { + "{username:'peter',firstName: 'Peter',lastName: 'Adams', userStatus: 'ACTIVE'}]") .execute() .assertStatus(200); - deleteTestUser("billy"); - deleteTestUser("peter"); + + deleteTestUsers(); } // Getting all users @Test public void testGettingUsers() throws Exception { + createTestUser("doglover"); petStoreRest .get("/user") .execute() .assertStatus(200) - .assertBody("[{username:'doglover',firstName: 'Tom',lastName: 'Simon', userStatus: 'ACTIVE'}]"); - deleteTestUser("doglover"); + .assertBody("[{username:'doglover',firstName:'Tom',lastName:'Simon',userStatus:'ACTIVE'}]"); + + deleteTestUsers(); } // Get user by user name @Test public void testFindUserByName() throws Exception { + createTestUser("garfield"); petStoreRest .get("/user/garfield") .execute() .assertStatus(200) - .assertBody("{username:'garfield',firstName: 'Tom',lastName: 'Simon', userStatus: 'ACTIVE'}"); - deleteTestUser("garfield"); + .assertBody("{username:'garfield',firstName:'Tom',lastName:'Simon',userStatus:'ACTIVE'}"); + + deleteTestUsers(); } // Updating user @Test public void testUpdateUser() throws Exception { + createTestUser("snoopy"); petStoreRest .put("/user/snoopy", "{username:'snoopy',phone: '34562345'}") .execute() .assertStatus(200); - deleteTestUser("snoopy"); - } - + deleteTestUsers(); + } //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- // Orders // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - private void deleteTestOrder(String orderId) throws AssertionError, ServletException, IOException { + private void deleteTestOrders() throws AssertionError, ServletException, IOException { + petStoreRest - .delete("/store/order/" + orderId) + .delete("/orders") .execute() .assertStatus(200); - + } - + private String createTestOrder() throws AssertionError, ServletException, IOException { + String petId = createTestPet(); String orderId = petStoreRest - .post("/store/order", "{petId:"+petId+" + ,username: 'catlover'}") + .post("/store/order", "{petId:" + petId + " + ,username: 'catlover'}") .execute() .assertStatus(200) .getBodyAsString(); return orderId; } - + // Posting order @Test public void testPostOrder() throws Exception { - - petStoreRest + + petStoreRest .post("/store/order", "{petId:'1',username: 'snoopy'}") .execute() - .assertStatus(200) - .assertBody("4"); - deleteTestOrder("4"); - - + .assertStatus(200); + + deleteTestOrders(); + } - + // Getting all orders @Test public void testGettingOrders() throws Exception { - + String orderId = createTestOrder(); petStoreRest .get("/store/order") .execute() .assertStatus(200) - .assertBody("[{id:"+orderId+",petId:0,status:'PLACED'}]"); - - deleteTestOrder(orderId); - + .assertBody("[{id:" + orderId + ",petId:0,status:'PLACED'}]"); + + deleteTestOrders(); + } // Find order by Id @Test public void testfindOrder() throws Exception { + String orderId = createTestOrder(); petStoreRest - .get("/store/order/"+orderId) + .get("/store/order/" + orderId) .execute() .assertStatus(200) - .assertBody("{id:"+orderId+",petId:0,status:'PLACED'}"); - - deleteTestOrder(orderId); + .assertBody("{id:" + orderId + ",petId:0,status:'PLACED'}"); + + deleteTestOrders(); } // Delete order by Id @Test public void testDeleteOrder() throws Exception { + String orderId = createTestOrder(); petStoreRest - .delete("/store/order/"+orderId) + .delete("/store/order/" + orderId) .execute() .assertStatus(200); - - } + + } } \ No newline at end of file
