cloudian: refactor wiremock static method usage

Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
This commit is contained in:
Rohit Yadav 2017-09-27 17:16:38 +05:30
parent 078112df25
commit 02907dac6d
1 changed files with 74 additions and 52 deletions

View File

@ -1,13 +1,36 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloudian.client;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.containing;
import static com.github.tomakehurst.wiremock.client.WireMock.delete;
import static com.github.tomakehurst.wiremock.client.WireMock.deleteRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.put;
import static com.github.tomakehurst.wiremock.client.WireMock.putRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import java.util.List;
@ -18,7 +41,6 @@ import org.junit.Test;
import com.cloud.utils.exception.CloudRuntimeException;
import com.github.tomakehurst.wiremock.client.BasicCredentials;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
public class CloudianClientTest {
@ -61,8 +83,8 @@ public class CloudianClientTest {
@Test(expected = CloudRuntimeException.class)
public void testRequestTimeout() {
wireMockRule.stubFor(WireMock.get(urlEqualTo("/group/list"))
.willReturn(WireMock.aResponse()
wireMockRule.stubFor(get(urlEqualTo("/group/list"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withStatus(200)
.withFixedDelay(2 * timeout * 1000)
@ -72,12 +94,12 @@ public class CloudianClientTest {
@Test
public void testBasicAuth() {
wireMockRule.stubFor(WireMock.get(urlEqualTo("/group/list"))
.willReturn(WireMock.aResponse()
wireMockRule.stubFor(get(urlEqualTo("/group/list"))
.willReturn(aResponse()
.withStatus(200)
.withBody("[]")));
client.listGroups();
WireMock.verify(getRequestedFor(urlEqualTo("/group/list"))
verify(getRequestedFor(urlEqualTo("/group/list"))
.withBasicAuth(new BasicCredentials(adminUsername, adminPassword)));
}
@ -87,23 +109,23 @@ public class CloudianClientTest {
@Test
public void addUserAccount() {
wireMockRule.stubFor(WireMock.put(urlEqualTo("/user"))
.willReturn(WireMock.aResponse()
wireMockRule.stubFor(put(urlEqualTo("/user"))
.willReturn(aResponse()
.withStatus(200)
.withBody("")));
final CloudianUser user = getTestUser();
boolean result = client.addUser(user);
Assert.assertTrue(result);
WireMock.verify(putRequestedFor(urlEqualTo("/user"))
verify(putRequestedFor(urlEqualTo("/user"))
.withRequestBody(containing("userId\":\"" + user.getUserId()))
.withHeader("Content-Type", equalTo("application/json")));
}
@Test
public void addUserAccountFail() {
wireMockRule.stubFor(WireMock.put(urlEqualTo("/user"))
.willReturn(WireMock.aResponse()
wireMockRule.stubFor(put(urlEqualTo("/user"))
.willReturn(aResponse()
.withStatus(400)
.withBody("")));
@ -116,8 +138,8 @@ public class CloudianClientTest {
public void listUserAccount() {
final String userId = "someUser";
final String groupId = "someGroup";
wireMockRule.stubFor(WireMock.get(urlPathMatching("/user?.*"))
.willReturn(WireMock.aResponse()
wireMockRule.stubFor(get(urlPathMatching("/user?.*"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody("{\"userId\":\"someUser\",\"userType\":\"User\",\"fullName\":\"John Doe (jdoe)\",\"emailAddr\":\"j@doe.com\",\"address1\":null,\"address2\":null,\"city\":null,\"state\":null,\"zip\":null,\"country\":null,\"phone\":null,\"groupId\":\"someGroup\",\"website\":null,\"active\":\"true\",\"canonicalUserId\":\"b3940886468689d375ebf8747b151c37\",\"ldapEnabled\":false}")));
@ -130,8 +152,8 @@ public class CloudianClientTest {
@Test
public void listUserAccountFail() {
wireMockRule.stubFor(WireMock.get(urlPathMatching("/user?.*"))
.willReturn(WireMock.aResponse()
wireMockRule.stubFor(get(urlPathMatching("/user?.*"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody("")));
@ -142,8 +164,8 @@ public class CloudianClientTest {
@Test
public void listUserAccounts() {
final String groupId = "someGroup";
wireMockRule.stubFor(WireMock.get(urlPathMatching("/user/list?.*"))
.willReturn(WireMock.aResponse()
wireMockRule.stubFor(get(urlPathMatching("/user/list?.*"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody("[{\"userId\":\"someUser\",\"userType\":\"User\",\"fullName\":\"John Doe (jdoe)\",\"emailAddr\":\"j@doe.com\",\"address1\":null,\"address2\":null,\"city\":null,\"state\":null,\"zip\":null,\"country\":null,\"phone\":null,\"groupId\":\"someGroup\",\"website\":null,\"active\":\"true\",\"canonicalUserId\":\"b3940886468689d375ebf8747b151c37\",\"ldapEnabled\":false}]")));
@ -155,8 +177,8 @@ public class CloudianClientTest {
@Test
public void listUserAccountsFail() {
wireMockRule.stubFor(WireMock.get(urlPathMatching("/user/list?.*"))
.willReturn(WireMock.aResponse()
wireMockRule.stubFor(get(urlPathMatching("/user/list?.*"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody("")));
@ -166,23 +188,23 @@ public class CloudianClientTest {
@Test
public void updateUserAccount() {
wireMockRule.stubFor(WireMock.post(urlEqualTo("/user"))
.willReturn(WireMock.aResponse()
wireMockRule.stubFor(post(urlEqualTo("/user"))
.willReturn(aResponse()
.withStatus(200)
.withBody("")));
final CloudianUser user = getTestUser();
boolean result = client.updateUser(user);
Assert.assertTrue(result);
WireMock.verify(postRequestedFor(urlEqualTo("/user"))
verify(postRequestedFor(urlEqualTo("/user"))
.withRequestBody(containing("userId\":\"" + user.getUserId()))
.withHeader("Content-Type", equalTo("application/json")));
}
@Test
public void updateUserAccountFail() {
wireMockRule.stubFor(WireMock.post(urlEqualTo("/user"))
.willReturn(WireMock.aResponse()
wireMockRule.stubFor(post(urlEqualTo("/user"))
.willReturn(aResponse()
.withStatus(400)
.withBody("")));
@ -192,21 +214,21 @@ public class CloudianClientTest {
@Test
public void removeUserAccount() {
wireMockRule.stubFor(WireMock.delete(urlPathMatching("/user.*"))
.willReturn(WireMock.aResponse()
wireMockRule.stubFor(delete(urlPathMatching("/user.*"))
.willReturn(aResponse()
.withStatus(200)
.withBody("")));
final CloudianUser user = getTestUser();
boolean result = client.removeUser(user.getUserId(), user.getGroupId());
Assert.assertTrue(result);
WireMock.verify(deleteRequestedFor(urlPathMatching("/user.*"))
verify(deleteRequestedFor(urlPathMatching("/user.*"))
.withQueryParam("userId", equalTo(user.getUserId())));
}
@Test
public void removeUserAccountFail() {
wireMockRule.stubFor(WireMock.delete(urlPathMatching("/user.*"))
.willReturn(WireMock.aResponse()
wireMockRule.stubFor(delete(urlPathMatching("/user.*"))
.willReturn(aResponse()
.withStatus(400)
.withBody("")));
final CloudianUser user = getTestUser();
@ -220,23 +242,23 @@ public class CloudianClientTest {
@Test
public void addGroup() {
wireMockRule.stubFor(WireMock.put(urlEqualTo("/group"))
.willReturn(WireMock.aResponse()
wireMockRule.stubFor(put(urlEqualTo("/group"))
.willReturn(aResponse()
.withStatus(200)
.withBody("")));
final CloudianGroup group = getTestGroup();
boolean result = client.addGroup(group);
Assert.assertTrue(result);
WireMock.verify(putRequestedFor(urlEqualTo("/group"))
verify(putRequestedFor(urlEqualTo("/group"))
.withRequestBody(containing("groupId\":\"someGroupId"))
.withHeader("Content-Type", equalTo("application/json")));
}
@Test
public void addGroupFail() throws Exception {
wireMockRule.stubFor(WireMock.put(urlEqualTo("/group"))
.willReturn(WireMock.aResponse()
wireMockRule.stubFor(put(urlEqualTo("/group"))
.willReturn(aResponse()
.withStatus(400)
.withBody("")));
@ -248,8 +270,8 @@ public class CloudianClientTest {
@Test
public void listGroup() {
final String groupId = "someGroup";
wireMockRule.stubFor(WireMock.get(urlPathMatching("/group.*"))
.willReturn(WireMock.aResponse()
wireMockRule.stubFor(get(urlPathMatching("/group.*"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody("{\"groupId\":\"someGroup\",\"groupName\":\"/someDomain\",\"ldapGroup\":null,\"active\":\"true\",\"ldapEnabled\":false,\"ldapServerURL\":null,\"ldapUserDNTemplate\":null,\"ldapSearch\":null,\"ldapSearchUserBase\":null,\"ldapMatchAttribute\":null}")));
@ -260,8 +282,8 @@ public class CloudianClientTest {
@Test
public void listGroupFail() {
wireMockRule.stubFor(WireMock.get(urlPathMatching("/group.*"))
.willReturn(WireMock.aResponse()
wireMockRule.stubFor(get(urlPathMatching("/group.*"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody("")));
@ -272,8 +294,8 @@ public class CloudianClientTest {
@Test
public void listGroups() {
final String groupId = "someGroup";
wireMockRule.stubFor(WireMock.get(urlEqualTo("/group/list"))
.willReturn(WireMock.aResponse()
wireMockRule.stubFor(get(urlEqualTo("/group/list"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody("[{\"groupId\":\"someGroup\",\"groupName\":\"/someDomain\",\"ldapGroup\":null,\"active\":\"true\",\"ldapEnabled\":false,\"ldapServerURL\":null,\"ldapUserDNTemplate\":null,\"ldapSearch\":null,\"ldapSearchUserBase\":null,\"ldapMatchAttribute\":null}]")));
@ -285,8 +307,8 @@ public class CloudianClientTest {
@Test
public void listGroupsFail() {
wireMockRule.stubFor(WireMock.get(urlEqualTo("/group/list"))
.willReturn(WireMock.aResponse()
wireMockRule.stubFor(get(urlEqualTo("/group/list"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody("")));
@ -296,23 +318,23 @@ public class CloudianClientTest {
@Test
public void updateGroup() {
wireMockRule.stubFor(WireMock.post(urlEqualTo("/group"))
.willReturn(WireMock.aResponse()
wireMockRule.stubFor(post(urlEqualTo("/group"))
.willReturn(aResponse()
.withStatus(200)
.withBody("")));
final CloudianGroup group = getTestGroup();
boolean result = client.updateGroup(group);
Assert.assertTrue(result);
WireMock.verify(postRequestedFor(urlEqualTo("/group"))
verify(postRequestedFor(urlEqualTo("/group"))
.withRequestBody(containing("groupId\":\"" + group.getGroupId()))
.withHeader("Content-Type", equalTo("application/json")));
}
@Test
public void updateGroupFail() {
wireMockRule.stubFor(WireMock.post(urlEqualTo("/group"))
.willReturn(WireMock.aResponse()
wireMockRule.stubFor(post(urlEqualTo("/group"))
.willReturn(aResponse()
.withStatus(400)
.withBody("")));
@ -322,21 +344,21 @@ public class CloudianClientTest {
@Test
public void removeGroup() {
wireMockRule.stubFor(WireMock.delete(urlPathMatching("/group.*"))
.willReturn(WireMock.aResponse()
wireMockRule.stubFor(delete(urlPathMatching("/group.*"))
.willReturn(aResponse()
.withStatus(200)
.withBody("")));
final CloudianGroup group = getTestGroup();
boolean result = client.removeGroup(group.getGroupId());
Assert.assertTrue(result);
WireMock.verify(deleteRequestedFor(urlPathMatching("/group.*"))
verify(deleteRequestedFor(urlPathMatching("/group.*"))
.withQueryParam("groupId", equalTo(group.getGroupId())));
}
@Test
public void removeGroupFail() {
wireMockRule.stubFor(WireMock.delete(urlPathMatching("/group.*"))
.willReturn(WireMock.aResponse()
wireMockRule.stubFor(delete(urlPathMatching("/group.*"))
.willReturn(aResponse()
.withStatus(400)
.withBody("")));
final CloudianGroup group = getTestGroup();