You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2020/01/20 21:49:46 UTC
[juneau-petstore] 37/48: mock rest test improved
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 d2a8860369896002c6f8cf3881931950e42a158c
Author: rasa <fi...@gmail.com>
AuthorDate: Fri Dec 20 07:31:31 2019 +0200
mock rest test improved
---
.../org/apache/juneau/petstore/test/MockTest.java | 325 +++++++++++++--------
1 file changed, 205 insertions(+), 120 deletions(-)
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 51df5ad..121b874 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
@@ -1,7 +1,10 @@
package org.apache.juneau.petstore.test;
-
+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.rest.PetStoreResource;
import org.apache.juneau.rest.mock2.MockRest;
import org.junit.Before;
@@ -25,115 +28,185 @@ public class MockTest {
public void setup() {
petStoreRest = MockRest.create(petStoreResource).simpleJson().build();
+
}
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Pets
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-
- // Posting pet
-
- @Test
- public void testPostPet() throws Exception {
-
- petStoreRest
- .post("/pet", "{name:'Sunshine',tags:['nice'], price:100.0,species:'BIRD'}")
- .execute()
- .assertStatus(200)
- .assertBody("1");
+ private String createTestPet() throws AssertionError, ServletException, IOException {
+ String petId = petStoreRest
+ .post("/pet", "{name:'Sunshine',tags:['nice'], price:100.0,species:'BIRD'}")
+ .execute()
+ .assertStatus(200)
+ .getBodyAsString();
+
+ return petId;
}
-
- // Getting all pets
-
- @Test
- public void testGettingPets() throws Exception {
-
+
+ private void deleteTestPet(String petId) throws AssertionError, ServletException, IOException {
petStoreRest
- .get("/pet")
+ .delete("/pet/" + petId)
.execute()
- .assertStatus(200)
- .assertBody("[{id:1,species:'BIRD',name:'Sunshine',tags:['nice'],price:100.0,status:'AVAILABLE'}]");
+ .assertStatus(200);
}
- // Find pet by Id
+ // Delete pet by Id
@Test
- public void testfindPet() throws Exception {
-
+ public void testDeletePet() throws Exception {
+ String petId = createTestPet();
petStoreRest
- .get("/pet/1")
+ .delete("/pet/" + petId)
.execute()
- .assertStatus(200)
- .assertBody("{id:1,species:'BIRD',name:'Sunshine',tags:['nice'],price:100.0,status:'AVAILABLE'}");
-
+ .assertStatus(200);
+
}
- // Find pet by status
+ // Getting all pets
@Test
- public void testfindPetByStatus() throws Exception {
-
+ public void testGettingPets() throws Exception {
+ String petId = createTestPet();
petStoreRest
- .request("GET", "/pet/findByStatus", "{'AVAILABLE'}")
+ .get("/pet")
.execute()
.assertStatus(200)
- .assertBody("[{id:1,species:'BIRD',name:'Sunshine',tags:[],price:100.0,status:'AVAILABLE'}]");
-
+ .assertBody(
+ "[{id:" + petId + ",species:'BIRD',name:'Sunshine',tags:['nice'],price:100.0,status:'AVAILABLE'}]");
+ deleteTestPet(petId);
}
- // Find pet by tags
+ // Posting pet
@Test
- public void testFindPetByTags() throws Exception {
+ public void testPostPet() throws Exception {
- petStoreRest
- .request("GET", "/pet/findByTags", "{'nice'}")
+ petStoreRest
+ .post("/pet", "{name:'Sunshine',tags:['nice'], price:100.0,species:'BIRD'}")
.execute()
.assertStatus(200)
- .assertBody("[{id:1,species:'BIRD',name:'Sunshine',tags:['nice'],price:100.0,status:'AVAILABLE'}]");
-
- }
- // Updating pet
-
- @Test
- public void testUpdatePet() throws Exception {
-
- petStoreRest
- .put("/pet/1", "{species:'BIRD',name:'Daisy',tags:[],price:120.0,status:'AVAILABLE'}")
- .execute()
- .assertStatus(200).assertBody("OK");
+ .assertBody("2");
+ deleteTestPet("2");
}
- // Delete pet by Id
-
- @Test
- public void testDeletePet() throws Exception {
-
- petStoreRest
- .delete("/pet/1")
- .execute()
- .assertStatus(200)
- .assertBody("OK");
- }
+
+ // 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);
+
+ }
+
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// 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;
+ }
+
// Create user
- @Test
- public void testCreateUser() throws Exception {
+ @Test
+ public void testCreateUser() throws Exception {
+
+ petStoreRest
+ .post("/user", "{username:'catlover',firstName: 'Tom',lastName: 'Simon', userStatus: 'ACTIVE'}")
+ .execute()
+ .assertStatus(200);
+
+ deleteTestUser("catlover");
+ }
+
+ // Delete user
- petStoreRest
- .post("/user", "{username:'catlover',firstName: 'Tom',lastName: 'Simon', userStatus: 'ACTIVE'}")
- .execute()
- .assertStatus(200)
- .assertBody("OK");
- }
+ @Test
+ public void testDeleteUser() throws Exception {
+ String name="catlover1";
+ createTestUser(name);
+
+ petStoreRest
+ .delete("/user/" + name)
+ .execute()
+ .assertStatus(200);
+
+ }
+
// Create list of users
@@ -141,119 +214,131 @@ public class MockTest {
public void testCreateUsers() throws Exception {
petStoreRest
- .post("/user/createWithArray",
- "[{username:'billy',firstName: 'Billy',lastName: 'Bob', userStatus: 'ACTIVE'},"
- + "{username:'peter',firstName: 'Peter',lastName: 'Adams', userStatus: 'ACTIVE'}]")
- .execute()
- .assertStatus(200)
- .assertBody("OK");
+ .post("/user/createWithArray",
+ "[{username:'billy',firstName: 'Billy',lastName: 'Bob', userStatus: 'ACTIVE'},"
+ + "{username:'peter',firstName: 'Peter',lastName: 'Adams', userStatus: 'ACTIVE'}]")
+ .execute()
+ .assertStatus(200);
+ deleteTestUser("billy");
+ deleteTestUser("peter");
}
// Getting all users
@Test
public void testGettingUsers() throws Exception {
-
+ createTestUser("doglover");
petStoreRest
.get("/user")
.execute()
.assertStatus(200)
- .assertBody(
- "[{username:'catlover',firstName: 'Tom',lastName: 'Simon', userStatus: 'ACTIVE'},"
- + "{username:'billy',firstName: 'Billy',lastName: 'Bob', userStatus: 'ACTIVE'},"
- + "{username:'peter',firstName: 'Peter',lastName: 'Adams', userStatus: 'ACTIVE'}]");
-
+ .assertBody("[{username:'doglover',firstName: 'Tom',lastName: 'Simon', userStatus: 'ACTIVE'}]");
+ deleteTestUser("doglover");
}
// Get user by user name
@Test
public void testFindUserByName() throws Exception {
-
+ createTestUser("garfield");
petStoreRest
- .get("/user/catlover")
+ .get("/user/garfield")
.execute()
.assertStatus(200)
- .assertBody("{id:1, username:'catlover',firstName: 'Tom',lastName: 'Simon', userStatus: 'ACTIVE'}");
-
+ .assertBody("{username:'garfield',firstName: 'Tom',lastName: 'Simon', userStatus: 'ACTIVE'}");
+ deleteTestUser("garfield");
}
// Updating user
@Test
public void testUpdateUser() throws Exception {
-
- petStoreRest
- .put("/user/catlover", "{id:1, username:'catlover',phone: '34562345'}")
- .execute()
- .assertStatus(200)
- .assertBody("OK");
- }
-
- // Delete user
-
- @Test
- public void testDeleteUser() throws Exception {
-
+ createTestUser("snoopy");
petStoreRest
- .delete("/user/billy")
+ .put("/user/snoopy", "{username:'snoopy',phone: '34562345'}")
.execute()
- .assertStatus(200)
- .assertBody("OK");
+ .assertStatus(200);
+ deleteTestUser("snoopy");
}
-
+
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Orders
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-
+ private void deleteTestOrder(String orderId) throws AssertionError, ServletException, IOException {
+ petStoreRest
+ .delete("/store/order/" + orderId)
+ .execute()
+ .assertStatus(200);
+
+ }
+
+ private String createTestOrder() throws AssertionError, ServletException, IOException {
+ String petId = createTestPet();
+ String orderId = petStoreRest
+ .post("/store/order", "{petId:"+petId+" + ,username: 'catlover'}")
+ .execute()
+ .assertStatus(200)
+ .getBodyAsString();
+
+ return orderId;
+ }
+
// Posting order
@Test
public void testPostOrder() throws Exception {
-
- petStoreRest
- .post("/store/order", "{petId:'1',username: 'catlover'}")
+
+ petStoreRest
+ .post("/store/order", "{petId:'1',username: 'snoopy'}")
.execute()
.assertStatus(200)
- .assertBody("1");
+ .assertBody("4");
+ deleteTestOrder("4");
+
+
}
+
// Getting all orders
@Test
public void testGettingOrders() throws Exception {
-
+
+ String orderId = createTestOrder();
petStoreRest
.get("/store/order")
.execute()
.assertStatus(200)
- .assertBody("[{id:1,petId:1,status:'PLACED'}]");
-
+ .assertBody("[{id:"+orderId+",petId:0,status:'PLACED'}]");
+
+ deleteTestOrder(orderId);
+
}
// Find order by Id
@Test
public void testfindOrder() throws Exception {
-
+ String orderId = createTestOrder();
petStoreRest
- .get("/store/order/1")
+ .get("/store/order/"+orderId)
.execute()
.assertStatus(200)
- .assertBody("{id:1,petId:1,status:'PLACED'}");
-
+ .assertBody("{id:"+orderId+",petId:0,status:'PLACED'}");
+
+ deleteTestOrder(orderId);
}
// Delete order by Id
@Test
public void testDeleteOrder() throws Exception {
-
+ String orderId = createTestOrder();
petStoreRest
- .delete("/store/order/1")
+ .delete("/store/order/"+orderId)
.execute()
- .assertStatus(200)
- .assertBody("OK");
- }
+ .assertStatus(200);
+
+ }
}
\ No newline at end of file