Skip to content

Commit a305753

Browse files
committed
updated for unit test search test
1 parent 951f505 commit a305753

File tree

3 files changed

+71
-12
lines changed

3 files changed

+71
-12
lines changed

src/main/java/org/couchbase/quickstart/configs/ConfigDb.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public String getScope() {
4848
}
4949

5050
public static final ConfigDb getInstance(){
51-
return new ConfigDb("http://localhost",
51+
return new ConfigDb("localhost",
5252
"Administrator",
5353
"password",
5454
"user_profile",

src/test/java/org/couchbase/quickstart/userProfile/userProfileTest.java

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.couchbase.client.java.json.JsonObject;
44
import org.couchbase.quickstart.helpers.DatabaseHelper;
55
import org.couchbase.quickstart.models.Profile;
6+
import org.couchbase.quickstart.models.ProfileList;
67
import org.couchbase.quickstart.models.ProfileResult;
78

89
import org.springframework.beans.factory.annotation.Autowired;
@@ -27,7 +28,7 @@ public class userProfileTest {
2728

2829
@Autowired
2930
private WebTestClient webTestClient;
30-
/*
31+
3132
@Test
3233
public void testUserProfileNotFound() {
3334
//bootstrap database
@@ -41,22 +42,71 @@ public void testUserProfileNotFound() {
4142
.expectStatus().is4xxClientError()
4243
.expectHeader().contentType(MediaType.APPLICATION_JSON);
4344
}
44-
*/
45+
4546
@Test
46-
public void testCreateThenDeleteUserProfile() {
47+
public void testCreateSearchThenDeleteUserProfile(){
4748
//bootstrap database
4849
DatabaseHelper dbHelper = new DatabaseHelper();
4950
dbHelper.createDb();
5051

5152
//test data
5253
Profile testProfile = getTestProfile();
54+
String json = getCreatedUserJson(testProfile);
5355

54-
//create json to post to integration test
55-
String json = JsonObject.create()
56-
.put("firstName", testProfile.getFirstName())
57-
.put("lastName", testProfile.getLastName())
58-
.put("password",testProfile.getPassword())
59-
.put("email",testProfile.getEmail()).toString();
56+
//run the post test
57+
EntityExchangeResult<ProfileResult> profileResult = this.webTestClient.post()
58+
.uri("/api/v1/userprofiles/")
59+
.bodyValue(json)
60+
.accept(MediaType.APPLICATION_JSON)
61+
.header("Content-Type", "application/json; charset=utf-8")
62+
.exchange()
63+
.expectStatus().isCreated()
64+
.expectBody(ProfileResult.class)
65+
.returnResult();
66+
67+
//get user and then try to search for the user
68+
ProfileResult newUser = profileResult.getResponseBody();
69+
70+
EntityExchangeResult<ProfileList> profileListResult = this.webTestClient.get()
71+
.uri("/api/v1/userprofiles?limit=5&skip=0&searchFirstName=Bob")
72+
.accept(MediaType.APPLICATION_JSON)
73+
.exchange()
74+
.expectStatus().isOk()
75+
.expectHeader().contentType(MediaType.APPLICATION_JSON)
76+
.expectBody(ProfileList.class)
77+
.returnResult();
78+
79+
ProfileList users = profileListResult.getResponseBody();
80+
assertNotNull(users);
81+
82+
assertTrue("Found one user", users.getProfiles().stream().count() == 1);
83+
84+
//compare the one user found
85+
Profile foundUser = users.getProfiles().get(0);
86+
assertEquals(foundUser.getFirstName(), testProfile.getFirstName());
87+
assertEquals(foundUser.getLastName(), testProfile.getLastName());
88+
assertEquals(foundUser.getEmail(), testProfile.getEmail());
89+
assertNotEquals(foundUser.getPassword(), testProfile.getPassword());
90+
assertNotNull(foundUser.getPid());
91+
92+
//delete the user
93+
this.webTestClient.delete()
94+
.uri(String.format("/api/v1/userprofiles/%s", newUser.getPid()))
95+
.accept(MediaType.APPLICATION_JSON)
96+
.header("Content-Type", "application/json; charset=utf-8")
97+
.exchange()
98+
.expectStatus().isOk();
99+
}
100+
101+
@Test
102+
public void testCreateThenDeleteUserProfile() {
103+
//bootstrap database
104+
DatabaseHelper dbHelper = new DatabaseHelper();
105+
dbHelper.createDb();
106+
107+
//test data
108+
Profile testProfile = getTestProfile();
109+
String json = getCreatedUserJson(testProfile);
60110

61111
//run the post test
62112
EntityExchangeResult<ProfileResult> profileResult = this.webTestClient.post()
@@ -86,7 +136,16 @@ public void testCreateThenDeleteUserProfile() {
86136
.expectStatus().isOk();
87137
}
88138

89-
public Profile getTestProfile() {
139+
private String getCreatedUserJson(Profile profile) {
140+
//create json to post to integration test
141+
return JsonObject.create()
142+
.put("firstName", profile.getFirstName())
143+
.put("lastName", profile.getLastName())
144+
.put("password", profile.getPassword())
145+
.put("email", profile.getEmail()).toString();
146+
}
147+
148+
private Profile getTestProfile() {
90149
return new Profile(
91150
UUID.randomUUID(),
92151
"James",

target/classes/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
spring.couchbase.bootstrap-hosts=couchbase://localhost
1+
spring.couchbase.bootstrap-hosts=http://localhost
22
spring.couchbase.bucket.name=user_profile
33
spring.couchbase.bucket.user=Administrator
44
spring.couchbase.bucket.password=password

0 commit comments

Comments
 (0)