Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 5E607200B40 for ; Thu, 16 Jun 2016 10:12:26 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 5D2C6160A61; Thu, 16 Jun 2016 08:12:26 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 83D53160A52 for ; Thu, 16 Jun 2016 10:12:24 +0200 (CEST) Received: (qmail 64125 invoked by uid 500); 16 Jun 2016 08:12:05 -0000 Mailing-List: contact commits-help@cloudstack.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cloudstack.apache.org Delivered-To: mailing list commits@cloudstack.apache.org Received: (qmail 61522 invoked by uid 99); 16 Jun 2016 08:12:03 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 16 Jun 2016 08:12:03 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 8D6CCE0FCB; Thu, 16 Jun 2016 08:12:03 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sateesh@apache.org To: commits@cloudstack.apache.org Date: Thu, 16 Jun 2016 08:12:49 -0000 Message-Id: <46776ce6689440018c00ebdc5dcf5e25@git.apache.org> In-Reply-To: <3abf179c1ff24ceab07691879f2366aa@git.apache.org> References: <3abf179c1ff24ceab07691879f2366aa@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [48/50] [abbrv] git commit: updated refs/heads/deploy-from-snapshot to 44ba14d archived-at: Thu, 16 Jun 2016 08:12:26 -0000 http://git-wip-us.apache.org/repos/asf/cloudstack/blob/09f71533/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NiciraNvpApiTest.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NiciraNvpApiTest.java b/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NiciraNvpApiTest.java index 1435cc5..79546a6 100644 --- a/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NiciraNvpApiTest.java +++ b/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NiciraNvpApiTest.java @@ -19,321 +19,180 @@ package com.cloud.network.nicira; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasProperty; +import static org.hamcrest.Matchers.hasSize; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Matchers.any; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; import java.util.List; -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.httpclient.NameValuePair; -import org.apache.commons.httpclient.methods.DeleteMethod; -import org.apache.commons.httpclient.methods.GetMethod; -import org.apache.commons.httpclient.methods.PostMethod; -import org.apache.commons.httpclient.methods.PutMethod; -import org.apache.commons.httpclient.params.HttpClientParams; -import org.junit.Before; +import org.apache.http.HttpHost; +import org.apache.http.HttpRequest; +import org.apache.http.ProtocolVersion; +import org.apache.http.StatusLine; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.message.BasicStatusLine; +import org.hamcrest.Matchers; import org.junit.Test; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; -import com.google.gson.Gson; -import com.google.gson.JsonParseException; -import com.cloud.utils.rest.RESTServiceConnector; -import com.cloud.utils.rest.RESTValidationStrategy; +import com.cloud.utils.rest.HttpClientHelper; +import com.cloud.utils.rest.HttpUriRequestMethodMatcher; +import com.cloud.utils.rest.HttpUriRequestPathMatcher; +import com.cloud.utils.rest.HttpUriRequestQueryMatcher; public class NiciraNvpApiTest { + private static final StatusLine HTTP_200_REPSONSE = new BasicStatusLine(new ProtocolVersion("HTTPS", 1, 1), HttpStatus.SC_OK, "OK"); + private static final StatusLine HTTP_201_REPSONSE = new BasicStatusLine(new ProtocolVersion("HTTPS", 1, 1), HttpStatus.SC_CREATED, "Created"); + protected static final String UUID = "aaaa"; protected static final String UUID2 = "bbbb"; - protected static final String UUID_SEC_PROFILE_URI = NiciraNvpApi.SEC_PROFILE_URI_PREFIX + "/aaaa"; + protected static final String UUID_SEC_PROFILE_URI = NiciraConstants.SEC_PROFILE_URI_PREFIX + "/aaaa"; protected static final String SCHEMA = "myTestSchema"; protected static final String SCHEMA2 = "myTestSchema2"; protected static final String HREF = "myTestHref"; protected static final String HREF2 = "myTestHref2"; protected static final String SEC_PROFILE_JSON_RESPONSE = - "{\"uuid\" : \"aaaa\"," - + "\"display_name\" : \"myTestName\"," - + "\"href\" : \"myTestHref\"," - + "\"schema\" : \"myTestSchema\"}"; + "{\"uuid\" : \"aaaa\"," + + "\"display_name\" : \"myTestName\"," + + "\"href\" : \"myTestHref\"," + + "\"schema\" : \"myTestSchema\"}"; protected static final String SEC_PROFILE_LIST_JSON_RESPONSE = "{\"results\" : [{\"uuid\" : \"aaaa\"," - + "\"display_name\" : \"myTestName\"," - + "\"href\" : \"myTestHref\"," - + "\"schema\" : \"myTestSchema\"}," - + "{ \"uuid\" : \"bbbb\"," - + "\"display_name\" : \"myTestName2\"," - + "\"href\" : \"myTestHref2\"," - + "\"schema\" : \"myTestSchema2\"}]," - + "\"result_count\": 2}"; - - NiciraNvpApi api; - HttpClient client = mock(HttpClient.class); - HttpMethod method; - String type; - String uri; - - @Before - public void setUp() { - final HttpClientParams hmp = mock(HttpClientParams.class); - when(client.getParams()).thenReturn(hmp); - api = new NiciraNvpApi(); - - api.restConnector = new RESTServiceConnector(new RESTValidationStrategy()) { - @Override - public HttpClient createHttpClient() { - return client; - } - - @Override - public HttpMethod createMethod(final String newType, final String newUri) { - type = newType; - uri = newUri; - return method; - } - }; - - api.setAdminCredentials("admin", "adminpass"); - api.setControllerAddress("localhost"); + + "\"display_name\" : \"myTestName\"," + + "\"href\" : \"myTestHref\"," + + "\"schema\" : \"myTestSchema\"}," + + "{ \"uuid\" : \"bbbb\"," + + "\"display_name\" : \"myTestName2\"," + + "\"href\" : \"myTestHref2\"," + + "\"schema\" : \"myTestSchema2\"}]," + + "\"result_count\": 2}"; + + private static NiciraNvpApi buildApi(final CloseableHttpClient httpClient) { + return NiciraNvpApi.create() + .host("localhost") + .username("admin") + .password("adminpassword") + .httpClient(httpClient) + .build(); } @Test - public void testFindSecurityProfile() throws NiciraNvpApiException, IOException { - // Prepare - method = mock(GetMethod.class); - when(method.getStatusCode()).thenReturn(HttpStatus.SC_OK); - when(method.getResponseBodyAsString()).thenReturn(SEC_PROFILE_LIST_JSON_RESPONSE); - final NameValuePair[] queryString = new NameValuePair[]{ - new NameValuePair("fields","*")}; - - // Execute - final NiciraNvpList actualProfiles = api.findSecurityProfile(); - - // Assert - verify(method, times(1)).releaseConnection(); - verify(method, times(1)).setQueryString(queryString); - assertEquals("Wrong Uuid in the newly created SecurityProfile", - UUID, actualProfiles.getResults().get(0).getUuid()); - assertEquals("Wrong Uuid in the newly created SecurityProfile", - HREF, actualProfiles.getResults().get(0).getHref()); - assertEquals("Wrong Schema in the newly created SecurityProfile", - SCHEMA, actualProfiles.getResults().get(0).getSchema()); - assertEquals("Wrong Uuid in the newly created SecurityProfile", - UUID2, actualProfiles.getResults().get(1).getUuid()); - assertEquals("Wrong Uuid in the newly created SecurityProfile", - HREF2, actualProfiles.getResults().get(1).getHref()); - assertEquals("Wrong Schema in the newly created SecurityProfile", - SCHEMA2, actualProfiles.getResults().get(1).getSchema()); - assertEquals("Wrong Schema in the newly created SecurityProfile", - 2, actualProfiles.getResultCount()); - assertEquals("Wrong URI for SecurityProfile creation REST service", - NiciraNvpApi.SEC_PROFILE_URI_PREFIX, uri); - assertEquals("Wrong URI for SecurityProfile creation REST service", - NiciraNvpApi.GET_METHOD_TYPE, type); + @SuppressWarnings("unchecked") + public void testFindSecurityProfile() throws Exception { + final CloseableHttpResponse response = mock(CloseableHttpResponse.class); + when(response.getStatusLine()).thenReturn(HTTP_200_REPSONSE); + when(response.getEntity()).thenReturn(new StringEntity(SEC_PROFILE_LIST_JSON_RESPONSE)); + final CloseableHttpClient httpClient = spy(HttpClientHelper.createHttpClient(2)); + doReturn(response).when(httpClient).execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class)); + final NiciraNvpApi api = buildApi(httpClient); + + final List actualProfiles = api.findSecurityProfile(); + + assertThat("Wrong number of results", actualProfiles, hasSize(2)); + assertThat("Wrong Uuid in the newly created SecurityProfile", actualProfiles, Matchers. contains( + hasProperty("uuid", equalTo(UUID)), + hasProperty("uuid", equalTo(UUID2)))); + assertThat("Wrong HREF in the newly created SecurityProfile", actualProfiles, Matchers. contains( + hasProperty("href", equalTo(HREF)), + hasProperty("href", equalTo(HREF2)))); + assertThat("Wrong Schema in the newly created SecurityProfile", actualProfiles, Matchers. contains( + hasProperty("schema", equalTo(SCHEMA)), + hasProperty("schema", equalTo(SCHEMA2)))); + verify(response, times(1)).close(); + verify(httpClient).execute(any(HttpHost.class), HttpUriRequestMethodMatcher.aMethod("GET"), any(HttpClientContext.class)); + verify(httpClient).execute(any(HttpHost.class), HttpUriRequestQueryMatcher.aQuery("fields=*"), any(HttpClientContext.class)); + verify(httpClient).execute(any(HttpHost.class), HttpUriRequestPathMatcher.aPath(NiciraConstants.SEC_PROFILE_URI_PREFIX), any(HttpClientContext.class)); } @Test - public void testFindSecurityProfileByUuid() throws NiciraNvpApiException, IOException { - // Prepare - method = mock(GetMethod.class); - when(method.getStatusCode()).thenReturn(HttpStatus.SC_OK); - when(method.getResponseBodyAsString()).thenReturn(SEC_PROFILE_LIST_JSON_RESPONSE); - final NameValuePair[] queryString = new NameValuePair[]{ - new NameValuePair("uuid", UUID), - new NameValuePair("fields","*") - }; - final List queryStringNvps = new ArrayList<>(); - doAnswer(new Answer() { - - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - final NameValuePair[] arguments = (NameValuePair[]) invocation.getArguments()[0]; - queryStringNvps.addAll(Arrays.asList(arguments)); - return null; - }}).when(method).setQueryString(any(NameValuePair[].class)); - - // Execute - final NiciraNvpList actualProfiles = api.findSecurityProfile(UUID); - - // Assert - verify(method, times(1)).releaseConnection(); - assertTrue(queryStringNvps.containsAll(Arrays.asList(queryString))); - assertEquals(queryString.length, queryStringNvps.size()); - assertEquals("Wrong Uuid in the newly created SecurityProfile", - UUID, actualProfiles.getResults().get(0).getUuid()); - assertEquals("Wrong Uuid in the newly created SecurityProfile", - HREF, actualProfiles.getResults().get(0).getHref()); - assertEquals("Wrong Schema in the newly created SecurityProfile", - SCHEMA, actualProfiles.getResults().get(0).getSchema()); - assertEquals("Wrong Uuid in the newly created SecurityProfile", - UUID2, actualProfiles.getResults().get(1).getUuid()); - assertEquals("Wrong Uuid in the newly created SecurityProfile", - HREF2, actualProfiles.getResults().get(1).getHref()); - assertEquals("Wrong Schema in the newly created SecurityProfile", - SCHEMA2, actualProfiles.getResults().get(1).getSchema()); - assertEquals("Wrong Schema in the newly created SecurityProfile", - 2, actualProfiles.getResultCount()); - assertEquals("Wrong URI for SecurityProfile creation REST service", - NiciraNvpApi.SEC_PROFILE_URI_PREFIX, uri); - assertEquals("Wrong HTTP method for SecurityProfile creation REST service", - NiciraNvpApi.GET_METHOD_TYPE, type); + @SuppressWarnings("unchecked") + public void testFindSecurityProfileByUuid() throws Exception { + final CloseableHttpResponse response = mock(CloseableHttpResponse.class); + when(response.getStatusLine()).thenReturn(HTTP_200_REPSONSE); + when(response.getEntity()).thenReturn(new StringEntity(SEC_PROFILE_LIST_JSON_RESPONSE)); + final CloseableHttpClient httpClient = spy(HttpClientHelper.createHttpClient(2)); + doReturn(response).when(httpClient).execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class)); + final NiciraNvpApi api = buildApi(httpClient); + + final List actualProfiles = api.findSecurityProfile(UUID); + + assertThat("Wrong number of results", actualProfiles, hasSize(2)); + assertThat("Wrong Uuid in the newly created SecurityProfile", actualProfiles, Matchers. contains( + hasProperty("uuid", equalTo(UUID)), + hasProperty("uuid", equalTo(UUID2)))); + assertThat("Wrong HREF in the newly created SecurityProfile", actualProfiles, Matchers. contains( + hasProperty("href", equalTo(HREF)), + hasProperty("href", equalTo(HREF2)))); + assertThat("Wrong Schema in the newly created SecurityProfile", actualProfiles, Matchers. contains( + hasProperty("schema", equalTo(SCHEMA)), + hasProperty("schema", equalTo(SCHEMA2)))); + verify(response, times(1)).close(); + verify(httpClient).execute(any(HttpHost.class), HttpUriRequestMethodMatcher.aMethod("GET"), any(HttpClientContext.class)); + verify(httpClient).execute(any(HttpHost.class), HttpUriRequestQueryMatcher.aQueryThatContains("uuid=" + UUID), any(HttpClientContext.class)); + verify(httpClient).execute(any(HttpHost.class), HttpUriRequestQueryMatcher.aQueryThatContains("fields=*"), any(HttpClientContext.class)); + verify(httpClient).execute(any(HttpHost.class), HttpUriRequestPathMatcher.aPath(NiciraConstants.SEC_PROFILE_URI_PREFIX), any(HttpClientContext.class)); } @Test - public void testCreateSecurityProfile() throws NiciraNvpApiException, IOException { - // Prepare - final SecurityProfile inputSecProfile = new SecurityProfile(); - method = mock(PostMethod.class); - when(method.getStatusCode()).thenReturn(HttpStatus.SC_CREATED); - when(method.getResponseBodyAsString()).thenReturn(SEC_PROFILE_JSON_RESPONSE); - - // Execute - final SecurityProfile actualSecProfile = api.createSecurityProfile(inputSecProfile); - - // Assert - verify(method, times(1)).releaseConnection(); - assertEquals("Wrong Uuid in the newly created SecurityProfile", - UUID, actualSecProfile.getUuid()); - assertEquals("Wrong Uuid in the newly created SecurityProfile", - HREF, actualSecProfile.getHref()); - assertEquals("Wrong Schema in the newly created SecurityProfile", - SCHEMA, actualSecProfile.getSchema()); - assertEquals("Wrong URI for SecurityProfile creation REST service", - NiciraNvpApi.SEC_PROFILE_URI_PREFIX, uri); - assertEquals("Wrong HTTP method for SecurityProfile creation REST service", - NiciraNvpApi.POST_METHOD_TYPE, type); + public void testCreateSecurityProfile() throws Exception { + final CloseableHttpResponse response = mock(CloseableHttpResponse.class); + when(response.getStatusLine()).thenReturn(HTTP_201_REPSONSE); + when(response.getEntity()).thenReturn(new StringEntity(SEC_PROFILE_JSON_RESPONSE)); + final CloseableHttpClient httpClient = spy(HttpClientHelper.createHttpClient(2)); + doReturn(response).when(httpClient).execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class)); + final NiciraNvpApi api = buildApi(httpClient); + + final SecurityProfile actualSecProfile = api.createSecurityProfile(new SecurityProfile()); + + assertThat("Wrong Uuid in the newly created SecurityProfile", actualSecProfile, hasProperty("uuid", equalTo(UUID))); + assertThat("Wrong Href in the newly created SecurityProfile", actualSecProfile, hasProperty("href", equalTo(HREF))); + assertThat("Wrong Schema in the newly created SecurityProfile", actualSecProfile, hasProperty("schema", equalTo(SCHEMA))); + verify(response, times(1)).close(); + verify(httpClient).execute(any(HttpHost.class), HttpUriRequestMethodMatcher.aMethod("POST"), any(HttpClientContext.class)); + verify(httpClient).execute(any(HttpHost.class), HttpUriRequestPathMatcher.aPath(NiciraConstants.SEC_PROFILE_URI_PREFIX), any(HttpClientContext.class)); } @Test - public void testUpdateSecurityProfile() throws NiciraNvpApiException, IOException { - // Prepare - final SecurityProfile inputSecProfile = new SecurityProfile(); - method = mock(PutMethod.class); - when(method.getStatusCode()).thenReturn(HttpStatus.SC_OK); - - // Execute - api.updateSecurityProfile(inputSecProfile, UUID); - - // Assert - verify(method, times(1)).releaseConnection(); - assertEquals("Wrong URI for SecurityProfile creation REST service", - UUID_SEC_PROFILE_URI, uri); - assertEquals("Wrong HTTP method for SecurityProfile creation REST service", - NiciraNvpApi.PUT_METHOD_TYPE, type); + public void testUpdateSecurityProfile() throws Exception { + final CloseableHttpResponse response = mock(CloseableHttpResponse.class); + when(response.getStatusLine()).thenReturn(HTTP_201_REPSONSE); + when(response.getEntity()).thenReturn(new StringEntity(SEC_PROFILE_JSON_RESPONSE)); + final CloseableHttpClient httpClient = spy(HttpClientHelper.createHttpClient(2)); + doReturn(response).when(httpClient).execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class)); + final NiciraNvpApi api = buildApi(httpClient); + + api.updateSecurityProfile(new SecurityProfile(), UUID); + + verify(response, times(1)).close(); + verify(httpClient).execute(any(HttpHost.class), HttpUriRequestMethodMatcher.aMethod("PUT"), any(HttpClientContext.class)); + verify(httpClient).execute(any(HttpHost.class), HttpUriRequestPathMatcher.aPath(NiciraConstants.SEC_PROFILE_URI_PREFIX + "/" + UUID), any(HttpClientContext.class)); } @Test - public void testDeleteSecurityProfile() throws NiciraNvpApiException, IOException { - // Prepare - method = mock(DeleteMethod.class); - when(method.getStatusCode()).thenReturn(HttpStatus.SC_NO_CONTENT); + public void testDeleteSecurityProfile() throws Exception { + final CloseableHttpResponse response = mock(CloseableHttpResponse.class); + when(response.getStatusLine()).thenReturn(HTTP_201_REPSONSE); + when(response.getEntity()).thenReturn(new StringEntity(SEC_PROFILE_JSON_RESPONSE)); + final CloseableHttpClient httpClient = spy(HttpClientHelper.createHttpClient(2)); + doReturn(response).when(httpClient).execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class)); + final NiciraNvpApi api = buildApi(httpClient); - // Execute api.deleteSecurityProfile(UUID); - // Assert - verify(method, times(1)).releaseConnection(); - assertEquals("Wrong URI for SecurityProfile deletion REST service", UUID_SEC_PROFILE_URI, uri); - assertEquals("Wrong HTTP method for SecurityProfile deletion REST service", NiciraNvpApi.DELETE_METHOD_TYPE, type); - } - - @Test(expected = JsonParseException.class) - public void testRoutingConfigAdapterNoType() throws NiciraNvpApiException, IOException { - // Prepare - final NiciraNvpApi api = new NiciraNvpApi(); - final Gson gson = api.restConnector.getGson(); - - // Execute - gson.fromJson("{}", RoutingConfig.class); - - // Assert: JsonParseException should be thrown - } - - @Test(expected = JsonParseException.class) - public void testRoutingConfigAdapterWrongType() throws NiciraNvpApiException, IOException { - // Prepare - final NiciraNvpApi api = new NiciraNvpApi(); - final Gson gson = api.restConnector.getGson(); - - // Execute - gson.fromJson("{type : \"WrongType\"}", RoutingConfig.class); - - // Assert: JsonParseException should be thrown - } - - @Test() - public void testRoutingConfigAdapter() throws NiciraNvpApiException, IOException { - // Prepare - final NiciraNvpApi api = new NiciraNvpApi(); - final Gson gson = api.restConnector.getGson(); - - // Execute - final SingleDefaultRouteImplicitRoutingConfig singleDefaultRouteImplicitRoutingConfig = - (SingleDefaultRouteImplicitRoutingConfig) gson.fromJson("{type : \"SingleDefaultRouteImplicitRoutingConfig\"}", RoutingConfig.class); - - // Assert: JsonParseException should be thrown - assertEquals("", SingleDefaultRouteImplicitRoutingConfig.class, singleDefaultRouteImplicitRoutingConfig.getClass()); - } - - @Test(expected = JsonParseException.class) - public void testNatRuleAdapterNoType() throws NiciraNvpApiException, IOException { - // Prepare - final NiciraNvpApi api = new NiciraNvpApi(); - final Gson gson = api.restConnector.getGson(); - - // Execute - gson.fromJson("{}", NatRule.class); - - // Assert: JsonParseException should be thrown - } - - @Test(expected = JsonParseException.class) - public void testNatRuleAdapterWrongType() throws NiciraNvpApiException, IOException { - // Prepare - final NiciraNvpApi api = new NiciraNvpApi(); - final Gson gson = api.restConnector.getGson(); - - // Execute - gson.fromJson("{type : \"WrongType\"}", NatRule.class); - - // Assert: JsonParseException should be thrown - } - - @Test() - public void testRoutingConfigAdapterWithSourceNatRule() throws NiciraNvpApiException, IOException { - // Prepare - final NiciraNvpApi api = new NiciraNvpApi(); - final Gson gson = api.restConnector.getGson(); - - // Execute - final SourceNatRule sourceNatRule = - (SourceNatRule) gson.fromJson("{type : \"SourceNatRule\"}", NatRule.class); - - // Assert: JsonParseException should be thrown - assertEquals("", SourceNatRule.class, sourceNatRule.getClass()); - } - - @Test() - public void testRoutingConfigAdapterWithDestinationNatRule() throws NiciraNvpApiException, IOException { - // Prepare - final NiciraNvpApi api = new NiciraNvpApi(); - final Gson gson = api.restConnector.getGson(); - - // Execute - final DestinationNatRule destinationNatRule = - (DestinationNatRule) gson.fromJson("{type : \"DestinationNatRule\"}", NatRule.class); - - // Assert: JsonParseException should be thrown - assertEquals("", DestinationNatRule.class, destinationNatRule.getClass()); + verify(response, times(1)).close(); + verify(httpClient).execute(any(HttpHost.class), HttpUriRequestMethodMatcher.aMethod("DELETE"), any(HttpClientContext.class)); + verify(httpClient).execute(any(HttpHost.class), HttpUriRequestPathMatcher.aPath(NiciraConstants.SEC_PROFILE_URI_PREFIX + "/" + UUID), any(HttpClientContext.class)); } } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/09f71533/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/resource/NiciraNvpRequestWrapperTest.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/resource/NiciraNvpRequestWrapperTest.java b/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/resource/NiciraNvpRequestWrapperTest.java index 25157a1..beb4059 100644 --- a/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/resource/NiciraNvpRequestWrapperTest.java +++ b/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/resource/NiciraNvpRequestWrapperTest.java @@ -45,7 +45,6 @@ import com.cloud.network.nicira.LogicalRouterPort; import com.cloud.network.nicira.LogicalSwitch; import com.cloud.network.nicira.NiciraNvpApi; import com.cloud.network.nicira.NiciraNvpApiException; -import com.cloud.network.nicira.NiciraNvpList; import com.cloud.network.nicira.VifAttachment; public class NiciraNvpRequestWrapperTest { @@ -144,10 +143,6 @@ public class NiciraNvpRequestWrapperTest { final List listPorts = new ArrayList(); listPorts.add(port1); - final NiciraNvpList ports = new NiciraNvpList(); - ports.setResults(listPorts); - ports.setResultCount(1); - final String logicalRouterUuid = "d2e05a9e-7120-4487-a5fc-414ab36d9345"; final String l3GatewayServiceUuid = "d2e05a9e-7120-4487-a5fc-414ab36d9345"; final List publicCidrs = new ArrayList(); @@ -158,7 +153,7 @@ public class NiciraNvpRequestWrapperTest { when(niciraNvpResource.getNiciraNvpApi()).thenReturn(niciraNvpApi); try { - when(niciraNvpApi.findLogicalRouterPortByGatewayServiceUuid(command.getLogicalRouterUuid(), command.getL3GatewayServiceUuid())).thenReturn(ports); + when(niciraNvpApi.findLogicalRouterPortByGatewayServiceUuid(command.getLogicalRouterUuid(), command.getL3GatewayServiceUuid())).thenReturn(listPorts); doNothing().when(niciraNvpApi).updateLogicalRouterPort(command.getLogicalRouterUuid(), port1); } catch (final NiciraNvpApiException e) { fail(e.getMessage()); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/09f71533/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/resource/NiciraNvpResourceTest.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/resource/NiciraNvpResourceTest.java b/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/resource/NiciraNvpResourceTest.java index 67d1a96..c0dedd2 100644 --- a/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/resource/NiciraNvpResourceTest.java +++ b/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/resource/NiciraNvpResourceTest.java @@ -83,7 +83,6 @@ import com.cloud.network.nicira.LogicalSwitchPort; import com.cloud.network.nicira.NatRule; import com.cloud.network.nicira.NiciraNvpApi; import com.cloud.network.nicira.NiciraNvpApiException; -import com.cloud.network.nicira.NiciraNvpList; import com.cloud.network.nicira.SourceNatRule; import com.cloud.network.utils.CommandRetryUtility; @@ -95,10 +94,10 @@ public class NiciraNvpResourceTest { private CommandRetryUtility retryUtility; @Before - public void setUp() throws ConfigurationException { + public void setUp() { resource = new NiciraNvpResource() { @Override - protected NiciraNvpApi createNiciraNvpApi() { + protected NiciraNvpApi createNiciraNvpApi(final String host, final String username, final String password) { return nvpApi; } }; @@ -121,20 +120,6 @@ public class NiciraNvpResourceTest { } @Test - public void resourceConfigure() throws ConfigurationException { - resource.configure("NiciraNvpResource", parameters); - - verify(nvpApi).setAdminCredentials("adminuser", "adminpass"); - verify(nvpApi).setControllerAddress("127.0.0.1"); - assertTrue("Incorrect resource name", "nvptestdevice".equals(resource.getName())); - - /* Pretty lame test, but here to assure this plugin fails - * if the type name ever changes from L2Networking - */ - assertTrue("Incorrect resource type", resource.getType() == Host.Type.L2Networking); - } - - @Test public void testInitialization() throws ConfigurationException { resource.configure("NiciraNvpResource", parameters); @@ -309,10 +294,7 @@ public class NiciraNvpResourceTest { public void testFindLogicalSwitchPort() throws ConfigurationException, NiciraNvpApiException { resource.configure("NiciraNvpResource", parameters); - @SuppressWarnings("unchecked") - final - NiciraNvpList lspl = mock(NiciraNvpList.class); - when(lspl.getResultCount()).thenReturn(1); + final List lspl = Arrays.asList(new LogicalSwitchPort()); when(nvpApi.findLogicalSwitchPortsByUuid("aaaa", "bbbb")).thenReturn(lspl); final FindLogicalSwitchPortAnswer flspa = (FindLogicalSwitchPortAnswer) resource.executeRequest(new FindLogicalSwitchPortCommand("aaaa", "bbbb")); @@ -324,9 +306,7 @@ public class NiciraNvpResourceTest { resource.configure("NiciraNvpResource", parameters); @SuppressWarnings("unchecked") - final - NiciraNvpList lspl = mock(NiciraNvpList.class); - when(lspl.getResultCount()).thenReturn(0); + final List lspl = Collections.EMPTY_LIST; when(nvpApi.findLogicalSwitchPortsByUuid("aaaa", "bbbb")).thenReturn(lspl); final FindLogicalSwitchPortAnswer flspa = (FindLogicalSwitchPortAnswer) resource.executeRequest(new FindLogicalSwitchPortCommand("aaaa", "bbbb")); @@ -427,8 +407,7 @@ public class NiciraNvpResourceTest { final ConfigurePublicIpsOnLogicalRouterCommand cmd = mock(ConfigurePublicIpsOnLogicalRouterCommand.class); @SuppressWarnings("unchecked") - final - NiciraNvpList list = mock(NiciraNvpList.class); + final List list = Collections.EMPTY_LIST; when(cmd.getLogicalRouterUuid()).thenReturn("aaaaa"); when(cmd.getL3GatewayServiceUuid()).thenReturn("bbbbb"); @@ -472,8 +451,7 @@ public class NiciraNvpResourceTest { // Mock the api find call @SuppressWarnings("unchecked") - final - NiciraNvpList storedRules = mock(NiciraNvpList.class); + final List storedRules = Collections.EMPTY_LIST; when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules); // Mock the api create calls @@ -522,11 +500,7 @@ public class NiciraNvpResourceTest { when(nvpApi.createLogicalRouterNatRule(eq("aaaaa"), (NatRule) any())).thenReturn(rulepair[0]).thenReturn(rulepair[1]); // Mock the api find call - @SuppressWarnings("unchecked") - final - NiciraNvpList storedRules = mock(NiciraNvpList.class); - when(storedRules.getResultCount()).thenReturn(2); - when(storedRules.getResults()).thenReturn(Arrays.asList(rulepair)); + final List storedRules = Arrays.asList(rulepair); when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules); final ConfigureStaticNatRulesOnLogicalRouterAnswer a = (ConfigureStaticNatRulesOnLogicalRouterAnswer) resource.executeRequest(cmd); @@ -571,11 +545,7 @@ public class NiciraNvpResourceTest { when(nvpApi.createLogicalRouterNatRule(eq("aaaaa"), (NatRule) any())).thenReturn(rulepair[0]).thenReturn(rulepair[1]); // Mock the api find call - @SuppressWarnings("unchecked") - final - NiciraNvpList storedRules = mock(NiciraNvpList.class); - when(storedRules.getResultCount()).thenReturn(2); - when(storedRules.getResults()).thenReturn(Arrays.asList(rulepair)); + final List storedRules = Arrays.asList(rulepair); when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules); final ConfigureStaticNatRulesOnLogicalRouterAnswer a = (ConfigureStaticNatRulesOnLogicalRouterAnswer) resource.executeRequest(cmd); @@ -616,9 +586,7 @@ public class NiciraNvpResourceTest { // Mock the api find call @SuppressWarnings("unchecked") - final - NiciraNvpList storedRules = mock(NiciraNvpList.class); - when(storedRules.getResultCount()).thenReturn(0); + final List storedRules = Collections.EMPTY_LIST; when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules); final ConfigureStaticNatRulesOnLogicalRouterAnswer a = (ConfigureStaticNatRulesOnLogicalRouterAnswer) resource.executeRequest(cmd); @@ -644,8 +612,7 @@ public class NiciraNvpResourceTest { // Mock the api find call @SuppressWarnings("unchecked") - final - NiciraNvpList storedRules = mock(NiciraNvpList.class); + final List storedRules = Collections.EMPTY_LIST; when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules); // Mock the api create calls @@ -694,11 +661,7 @@ public class NiciraNvpResourceTest { when(nvpApi.createLogicalRouterNatRule(eq("aaaaa"), (NatRule) any())).thenReturn(rulepair[0]).thenReturn(rulepair[1]); // Mock the api find call - @SuppressWarnings("unchecked") - final - NiciraNvpList storedRules = mock(NiciraNvpList.class); - when(storedRules.getResultCount()).thenReturn(2); - when(storedRules.getResults()).thenReturn(Arrays.asList(rulepair)); + final List storedRules = Arrays.asList(rulepair); when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules); final ConfigurePortForwardingRulesOnLogicalRouterAnswer a = (ConfigurePortForwardingRulesOnLogicalRouterAnswer) resource.executeRequest(cmd); @@ -743,11 +706,7 @@ public class NiciraNvpResourceTest { when(nvpApi.createLogicalRouterNatRule(eq("aaaaa"), (NatRule) any())).thenReturn(rulepair[0]).thenReturn(rulepair[1]); // Mock the api find call - @SuppressWarnings("unchecked") - final - NiciraNvpList storedRules = mock(NiciraNvpList.class); - when(storedRules.getResultCount()).thenReturn(2); - when(storedRules.getResults()).thenReturn(Arrays.asList(rulepair)); + final List storedRules = Arrays.asList(rulepair); when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules); final ConfigurePortForwardingRulesOnLogicalRouterAnswer a = (ConfigurePortForwardingRulesOnLogicalRouterAnswer) resource.executeRequest(cmd); @@ -788,9 +747,7 @@ public class NiciraNvpResourceTest { // Mock the api find call @SuppressWarnings("unchecked") - final - NiciraNvpList storedRules = mock(NiciraNvpList.class); - when(storedRules.getResultCount()).thenReturn(0); + final List storedRules = Collections.EMPTY_LIST; when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules); final ConfigurePortForwardingRulesOnLogicalRouterAnswer a = (ConfigurePortForwardingRulesOnLogicalRouterAnswer) resource.executeRequest(cmd); @@ -816,8 +773,7 @@ public class NiciraNvpResourceTest { // Mock the api find call @SuppressWarnings("unchecked") - final - NiciraNvpList storedRules = mock(NiciraNvpList.class); + final List storedRules = Collections.EMPTY_LIST; when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules); // Mock the api create calls http://git-wip-us.apache.org/repos/asf/cloudstack/blob/09f71533/utils/src/test/java/com/cloud/utils/rest/HttpUriRequestPathMatcher.java ---------------------------------------------------------------------- diff --git a/utils/src/test/java/com/cloud/utils/rest/HttpUriRequestPathMatcher.java b/utils/src/test/java/com/cloud/utils/rest/HttpUriRequestPathMatcher.java new file mode 100644 index 0000000..948e9f6 --- /dev/null +++ b/utils/src/test/java/com/cloud/utils/rest/HttpUriRequestPathMatcher.java @@ -0,0 +1,43 @@ +// +// 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.cloud.utils.rest; + +import static org.hamcrest.Matchers.equalTo; +import static org.mockito.Matchers.argThat; + +import org.apache.http.client.methods.HttpUriRequest; +import org.hamcrest.FeatureMatcher; +import org.hamcrest.Matcher; + +public class HttpUriRequestPathMatcher extends FeatureMatcher { + + public static HttpUriRequest aPath(final String path) { + return argThat(new HttpUriRequestPathMatcher(equalTo(path), "path", "path")); + } + + public HttpUriRequestPathMatcher(final Matcher subMatcher, final String featureDescription, final String featureName) { + super(subMatcher, featureDescription, featureName); + } + + @Override + protected String featureValueOf(final HttpUriRequest actual) { + return actual.getURI().getPath(); + } +}