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 47462200B2D for ; Thu, 16 Jun 2016 10:12:27 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 45F62160A61; Thu, 16 Jun 2016 08:12:27 +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 E9074160A51 for ; Thu, 16 Jun 2016 10:12:25 +0200 (CEST) Received: (qmail 64242 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 61507 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 84A56E00DB; 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:47 -0000 Message-Id: <1899efd3639940dd8e32b115fdb7526d@git.apache.org> In-Reply-To: <3abf179c1ff24ceab07691879f2366aa@git.apache.org> References: <3abf179c1ff24ceab07691879f2366aa@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [46/50] [abbrv] git commit: updated refs/heads/deploy-from-snapshot to 44ba14d archived-at: Thu, 16 Jun 2016 08:12:27 -0000 Add NSX specific RestClient implementation - Add -noverify JVM arg to surefire plugin, to allow Powermockito to de-encapsulate private methods - Add dependency on cloud-utils test-jar to use custom HttpRequest matchers Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/18e6b9ba Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/18e6b9ba Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/18e6b9ba Branch: refs/heads/deploy-from-snapshot Commit: 18e6b9ba78600fd01456d18c1f2b4c5333714dd9 Parents: de63b94 Author: Miguel Ferreira Authored: Sat Aug 22 17:05:30 2015 +0200 Committer: Miguel Ferreira Committed: Tue Aug 25 17:36:15 2015 +0200 ---------------------------------------------------------------------- plugins/network-elements/nicira-nvp/pom.xml | 10 + .../cloud/network/nicira/ExecutionCounter.java | 50 +++++ .../cloud/network/nicira/NiciraRestClient.java | 202 +++++++++++++++++++ .../network/nicira/ExecutionCounterTest.java | 55 +++++ .../network/nicira/NiciraRestClientTest.java | 172 ++++++++++++++++ pom.xml | 2 +- .../cloud/utils/rest/HttpStatusCodeHelper.java | 34 ++++ .../utils/rest/HttpStatusCodeHelperTest.java | 59 ++++++ 8 files changed, 583 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/18e6b9ba/plugins/network-elements/nicira-nvp/pom.xml ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/pom.xml b/plugins/network-elements/nicira-nvp/pom.xml index 2d20ae2..e74a5ae 100644 --- a/plugins/network-elements/nicira-nvp/pom.xml +++ b/plugins/network-elements/nicira-nvp/pom.xml @@ -30,6 +30,16 @@ ../../pom.xml + + + org.apache.cloudstack + cloud-utils + 4.6.0-SNAPSHOT + test-jar + test + + + src/main/java src/test/java http://git-wip-us.apache.org/repos/asf/cloudstack/blob/18e6b9ba/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/ExecutionCounter.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/ExecutionCounter.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/ExecutionCounter.java new file mode 100644 index 0000000..1314498 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/ExecutionCounter.java @@ -0,0 +1,50 @@ +// +// 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.network.nicira; + +import java.util.concurrent.atomic.AtomicInteger; + +public class ExecutionCounter { + + private final int executionLimit; + private final AtomicInteger executionCount = new AtomicInteger(0); + + public ExecutionCounter(final int executionLimit) { + this.executionLimit = executionLimit; + } + + public ExecutionCounter resetExecutionCounter() { + executionCount.set(0); + return this; + } + + public boolean hasReachedExecutionLimit() { + return executionCount.get() >= executionLimit; + } + + public ExecutionCounter incrementExecutionCounter() { + executionCount.incrementAndGet(); + return this; + } + + public int getValue() { + return executionCount.get(); + } +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/18e6b9ba/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/NiciraRestClient.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/NiciraRestClient.java b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/NiciraRestClient.java new file mode 100644 index 0000000..6ade3a5 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/nicira/NiciraRestClient.java @@ -0,0 +1,202 @@ +// +// 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.network.nicira; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.apache.http.HttpEntity; +import org.apache.http.StatusLine; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.util.EntityUtils; +import org.apache.log4j.Logger; + +import com.cloud.utils.rest.BasicRestClient; +import com.cloud.utils.rest.CloudstackRESTException; +import com.cloud.utils.rest.HttpConstants; +import com.cloud.utils.rest.HttpMethods; +import com.cloud.utils.rest.HttpStatusCodeHelper; +import com.cloud.utils.rest.HttpUriRequestBuilder; + +public class NiciraRestClient extends BasicRestClient { + + private static final Logger s_logger = Logger.getLogger(NiciraRestClient.class); + + private static final String CONTENT_TYPE = HttpConstants.CONTENT_TYPE; + private static final String TEXT_HTML_CONTENT_TYPE = HttpConstants.TEXT_HTML_CONTENT_TYPE; + + private static final int DEFAULT_BODY_RESP_MAX_LEN = 1024; + private static final int DEFAULT_EXECUTION_LIMIT = 5; + + private final ExecutionCounter counter; + private final int maxResponseErrorMesageLength; + private final int executionLimit; + + private final String username; + private final String password; + private final String loginUrl; + + private NiciraRestClient(final Builder builder) { + super(builder.client, builder.clientContext, builder.hostname); + executionLimit = builder.executionLimit; + counter = new ExecutionCounter(executionLimit); + maxResponseErrorMesageLength = builder.maxResponseErrorMesageLength; + username = builder.username; + password = builder.password; + loginUrl = builder.loginUrl; + } + + public static Builder create() { + return new Builder(); + } + + @Override + public CloseableHttpResponse execute(final HttpUriRequest request) throws CloudstackRESTException { + return execute(request, 0); + } + + private CloseableHttpResponse execute(final HttpUriRequest request, final int previousStatusCode) throws CloudstackRESTException { + if (counter.hasReachedExecutionLimit()) { + throw new CloudstackRESTException("Reached max executions limit of " + executionLimit); + } + counter.incrementExecutionCounter(); + s_logger.debug("Executing " + request.getMethod() + " request [execution count = " + counter.getValue() + "]"); + final CloseableHttpResponse response = super.execute(request); + + final StatusLine statusLine = response.getStatusLine(); + final int statusCode = statusLine.getStatusCode(); + s_logger.debug("Status of last request: " + statusLine.toString()); + if (HttpStatusCodeHelper.isUnauthorized(statusCode)) { + return handleUnauthorizedResponse(request, previousStatusCode, response, statusCode); + } else if (HttpStatusCodeHelper.isSuccess(statusCode)) { + return handleSuccessResponse(response); + } else { + throw new CloudstackRESTException("Unexpecetd status code: " + statusCode); + } + } + + private CloseableHttpResponse handleUnauthorizedResponse(final HttpUriRequest request, final int previousStatusCode, final CloseableHttpResponse response, final int statusCode) + throws CloudstackRESTException { + super.closeResponse(response); + if (HttpStatusCodeHelper.isUnauthorized(previousStatusCode)) { + s_logger.error(responseToErrorMessage(response)); + throw new CloudstackRESTException("Two consecutive failed attempts to authenticate against REST server"); + } + final HttpUriRequest authenticateRequest = createAuthenticationRequest(); + final CloseableHttpResponse loginResponse = execute(authenticateRequest, statusCode); + final int loginStatusCode = loginResponse.getStatusLine().getStatusCode(); + super.closeResponse(loginResponse); + return execute(request, loginStatusCode); + } + + private CloseableHttpResponse handleSuccessResponse(final CloseableHttpResponse response) { + counter.resetExecutionCounter(); + return response; + } + + private HttpUriRequest createAuthenticationRequest() { + final Map parameters = new HashMap<>(); + parameters.put("username", username); + parameters.put("password", password); + return HttpUriRequestBuilder.create() + .method(HttpMethods.POST) + .methodParameters(parameters) + .path(loginUrl) + .build(); + } + + private String responseToErrorMessage(final CloseableHttpResponse response) { + String errorMessage = response.getStatusLine().toString(); + if (response.containsHeader(CONTENT_TYPE) && TEXT_HTML_CONTENT_TYPE.equals(response.getFirstHeader(CONTENT_TYPE).getValue())) { + try { + final HttpEntity entity = response.getEntity(); + final String respobnseBody = EntityUtils.toString(entity); + errorMessage = respobnseBody.subSequence(0, maxResponseErrorMesageLength).toString(); + } catch (final IOException e) { + s_logger.debug("Could not read repsonse body. Response: " + response, e); + } + } + + return errorMessage; + } + + protected static class Builder extends BasicRestClient.Builder { + private CloseableHttpClient client; + private HttpClientContext clientContext; + private String hostname; + private String username; + private String password; + private String loginUrl; + private int executionLimit = DEFAULT_EXECUTION_LIMIT; + private int maxResponseErrorMesageLength = DEFAULT_BODY_RESP_MAX_LEN; + + public Builder hostname(final String hostname) { + this.hostname = hostname; + return this; + } + + public Builder username(final String username) { + this.username = username; + return this; + } + + public Builder password(final String password) { + this.password = password; + return this; + } + + public Builder loginUrl(final String loginUrl) { + this.loginUrl = loginUrl; + return this; + } + + @Override + public Builder client(final CloseableHttpClient client) { + this.client = client; + return this; + } + + @Override + public Builder clientContext(final HttpClientContext clientContext) { + this.clientContext = clientContext; + return this; + } + + public Builder executionLimit(final int executionLimit) { + this.executionLimit = executionLimit; + return this; + } + + public Builder maxResponseErrorMesageLength(final int maxResponseErrorMesageLength) { + this.maxResponseErrorMesageLength = maxResponseErrorMesageLength; + return this; + } + + @Override + public NiciraRestClient build() { + return new NiciraRestClient(this); + } + + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/18e6b9ba/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/ExecutionCounterTest.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/ExecutionCounterTest.java b/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/ExecutionCounterTest.java new file mode 100644 index 0000000..18797df --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/ExecutionCounterTest.java @@ -0,0 +1,55 @@ +// +// 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.network.nicira; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +import org.junit.Test; + +public class ExecutionCounterTest { + + @Test + public void testIncrementCounter() throws Exception { + final ExecutionCounter executionCounter = new ExecutionCounter(-1); + + executionCounter.incrementExecutionCounter().incrementExecutionCounter(); + + assertThat(executionCounter.getValue(), equalTo(2)); + } + + @Test + public void testHasNotYetReachedTheExecutuionLimit() throws Exception { + final ExecutionCounter executionCounter = new ExecutionCounter(2); + + executionCounter.incrementExecutionCounter(); + + assertThat(executionCounter.hasReachedExecutionLimit(), equalTo(false)); + } + + @Test + public void testHasAlreadyReachedTheExecutuionLimit() throws Exception { + final ExecutionCounter executionCounter = new ExecutionCounter(2); + + executionCounter.incrementExecutionCounter().incrementExecutionCounter(); + + assertThat(executionCounter.hasReachedExecutionLimit(), equalTo(true)); + } +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/18e6b9ba/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NiciraRestClientTest.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NiciraRestClientTest.java b/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NiciraRestClientTest.java new file mode 100644 index 0000000..7fcab80 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/nicira/NiciraRestClientTest.java @@ -0,0 +1,172 @@ +// +// 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.network.nicira; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.isEmptyOrNullString; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.sameInstance; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.spy; +import static org.powermock.api.mockito.PowerMockito.verifyPrivate; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.http.HttpHost; +import org.apache.http.ProtocolVersion; +import org.apache.http.StatusLine; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.message.BasicStatusLine; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import com.cloud.utils.rest.CloudstackRESTException; +import com.cloud.utils.rest.HttpMethods; +import com.cloud.utils.rest.HttpRequestMatcher; +import com.cloud.utils.rest.HttpUriRequestBuilder; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(NiciraRestClient.class) +public class NiciraRestClientTest { + + private static final int HTTPS_PORT = 443; + + private static final String HTTPS = "HTTPS"; + private static final String LOGIN_PATH = "/login"; + private static final String LOCALHOST = "localhost"; + private static final String ADMIN = "admin"; + private static final String ADMIN_PASSWORD = "adminpassword"; + + private static final HttpHost HTTP_HOST = new HttpHost(LOCALHOST, HTTPS_PORT, HTTPS); + + private static final StatusLine HTTP_200_STATUSLINE = new BasicStatusLine(new ProtocolVersion(HTTPS, 1, 1), 200, "OK"); + private static final StatusLine HTTP_401_STATUSLINE = new BasicStatusLine(new ProtocolVersion(HTTPS, 1, 1), 401, "Unauthorized"); + + private static final Map loginParameters = new HashMap(); + private static HttpUriRequest request; + private static HttpUriRequest loginRequest; + private final CloseableHttpClient httpClient = mock(CloseableHttpClient.class); + private final CloseableHttpResponse mockResponse = mock(CloseableHttpResponse.class); + private HttpClientContext httpClientContext; + private NiciraRestClient client; + + @BeforeClass + public static void setupClass() { + loginParameters.put("username", ADMIN); + loginParameters.put("password", ADMIN_PASSWORD); + request = HttpUriRequestBuilder.create() + .method(HttpMethods.GET) + .path("/path") + .build(); + loginRequest = HttpUriRequestBuilder.create() + .method(HttpMethods.POST) + .methodParameters(loginParameters) + .path(LOGIN_PATH) + .build(); + } + + @Before + public void setup() { + httpClientContext = HttpClientContext.create(); + client = spy(NiciraRestClient.create() + .client(httpClient) + .clientContext(httpClientContext) + .hostname(LOCALHOST) + .username(ADMIN) + .password(ADMIN_PASSWORD) + .loginUrl(LOGIN_PATH) + .executionLimit(5) + .build()); + } + + @Test + public void testExecuteSuccessAtFirstAttempt() throws Exception { + when(mockResponse.getStatusLine()).thenReturn(HTTP_200_STATUSLINE); + when(httpClient.execute(eq(HTTP_HOST), eq(request), eq(httpClientContext))).thenReturn(mockResponse); + + final CloseableHttpResponse response = client.execute(request); + + assertThat(response, notNullValue()); + assertThat(response, sameInstance(mockResponse)); + verifyPrivate(client).invoke("execute", request, 0); + } + + @Test + public void testExecuteUnauthorizedThenSuccess() throws Exception { + when(mockResponse.getStatusLine()) + .thenReturn(HTTP_401_STATUSLINE) + .thenReturn(HTTP_200_STATUSLINE) + .thenReturn(HTTP_200_STATUSLINE); + when(httpClient.execute(eq(HTTP_HOST), HttpRequestMatcher.eq(request), eq(httpClientContext))) + .thenReturn(mockResponse) + .thenReturn(mockResponse); + when(httpClient.execute(eq(HTTP_HOST), HttpRequestMatcher.eq(loginRequest), eq(httpClientContext))) + .thenReturn(mockResponse); + + final CloseableHttpResponse response = client.execute(request); + + assertThat(response, notNullValue()); + assertThat(response, sameInstance(mockResponse)); + verifyPrivate(client).invoke("execute", HttpRequestMatcher.eq(request), eq(0)); + verifyPrivate(client).invoke("execute", HttpRequestMatcher.eq(loginRequest), eq(401)); + verifyPrivate(client).invoke("execute", HttpRequestMatcher.eq(request), eq(200)); + } + + @Test + public void testExecuteTwoConsecutiveUnauthorizedExecutions() throws Exception { + when(mockResponse.getStatusLine()) + .thenReturn(HTTP_401_STATUSLINE) + .thenReturn(HTTP_401_STATUSLINE); + when(httpClient.execute(eq(HTTP_HOST), HttpRequestMatcher.eq(request), eq(httpClientContext))) + .thenReturn(mockResponse); + when(httpClient.execute(eq(HTTP_HOST), HttpRequestMatcher.eq(loginRequest), eq(httpClientContext))) + .thenReturn(mockResponse); + final NiciraRestClient client = spy(NiciraRestClient.create() + .client(httpClient) + .clientContext(httpClientContext) + .hostname(LOCALHOST) + .username(ADMIN) + .password(ADMIN_PASSWORD) + .loginUrl(LOGIN_PATH) + .executionLimit(2) + .build()); + + try { + client.execute(request); + fail("Expected CloudstackRESTException exception"); + } catch (final CloudstackRESTException e) { + assertThat(e.getMessage(), not(isEmptyOrNullString())); + verifyPrivate(client).invoke("execute", HttpRequestMatcher.eq(request), eq(0)); + verifyPrivate(client).invoke("execute", HttpRequestMatcher.eq(loginRequest), eq(401)); + } + } +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/18e6b9ba/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index b5e380e..219402c 100644 --- a/pom.xml +++ b/pom.xml @@ -985,7 +985,7 @@ maven-surefire-plugin 2.18.1 - -Djava.security.egd=file:/dev/./urandom + -Djava.security.egd=file:/dev/./urandom -noverify http://git-wip-us.apache.org/repos/asf/cloudstack/blob/18e6b9ba/utils/src/main/java/com/cloud/utils/rest/HttpStatusCodeHelper.java ---------------------------------------------------------------------- diff --git a/utils/src/main/java/com/cloud/utils/rest/HttpStatusCodeHelper.java b/utils/src/main/java/com/cloud/utils/rest/HttpStatusCodeHelper.java new file mode 100644 index 0000000..9492920 --- /dev/null +++ b/utils/src/main/java/com/cloud/utils/rest/HttpStatusCodeHelper.java @@ -0,0 +1,34 @@ +// +// 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 org.apache.http.HttpStatus; + +public class HttpStatusCodeHelper { + + public static boolean isSuccess(final int statusCode) { + return statusCode >= HttpStatus.SC_OK && statusCode <= HttpStatus.SC_MULTI_STATUS; + } + + public static boolean isUnauthorized(final int statusCode) { + return statusCode == HttpStatus.SC_UNAUTHORIZED; + } + +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/18e6b9ba/utils/src/test/java/com/cloud/utils/rest/HttpStatusCodeHelperTest.java ---------------------------------------------------------------------- diff --git a/utils/src/test/java/com/cloud/utils/rest/HttpStatusCodeHelperTest.java b/utils/src/test/java/com/cloud/utils/rest/HttpStatusCodeHelperTest.java new file mode 100644 index 0000000..3999b20 --- /dev/null +++ b/utils/src/test/java/com/cloud/utils/rest/HttpStatusCodeHelperTest.java @@ -0,0 +1,59 @@ +// +// 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.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +import org.apache.http.HttpStatus; +import org.junit.Test; + +public class HttpStatusCodeHelperTest { + + @Test + public void testIsSuccess() throws Exception { + assertThat(HttpStatusCodeHelper.isSuccess(HttpStatus.SC_SWITCHING_PROTOCOLS), equalTo(false)); + assertThat(HttpStatusCodeHelper.isSuccess(HttpStatus.SC_PROCESSING), equalTo(false)); + + assertThat(HttpStatusCodeHelper.isSuccess(HttpStatus.SC_OK), equalTo(true)); + assertThat(HttpStatusCodeHelper.isSuccess(HttpStatus.SC_CREATED), equalTo(true)); + assertThat(HttpStatusCodeHelper.isSuccess(HttpStatus.SC_ACCEPTED), equalTo(true)); + assertThat(HttpStatusCodeHelper.isSuccess(HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION), equalTo(true)); + assertThat(HttpStatusCodeHelper.isSuccess(HttpStatus.SC_NO_CONTENT), equalTo(true)); + assertThat(HttpStatusCodeHelper.isSuccess(HttpStatus.SC_RESET_CONTENT), equalTo(true)); + assertThat(HttpStatusCodeHelper.isSuccess(HttpStatus.SC_PARTIAL_CONTENT), equalTo(true)); + assertThat(HttpStatusCodeHelper.isSuccess(HttpStatus.SC_MULTI_STATUS), equalTo(true)); + + assertThat(HttpStatusCodeHelper.isSuccess(HttpStatus.SC_MULTIPLE_CHOICES), equalTo(false)); + assertThat(HttpStatusCodeHelper.isSuccess(HttpStatus.SC_MOVED_PERMANENTLY), equalTo(false)); + } + + @Test + public void testIsUnauthorized() throws Exception { + assertThat(HttpStatusCodeHelper.isUnauthorized(HttpStatus.SC_TEMPORARY_REDIRECT), equalTo(false)); + assertThat(HttpStatusCodeHelper.isUnauthorized(HttpStatus.SC_BAD_REQUEST), equalTo(false)); + + assertThat(HttpStatusCodeHelper.isUnauthorized(HttpStatus.SC_UNAUTHORIZED), equalTo(true)); + + assertThat(HttpStatusCodeHelper.isUnauthorized(HttpStatus.SC_PAYMENT_REQUIRED), equalTo(false)); + assertThat(HttpStatusCodeHelper.isUnauthorized(HttpStatus.SC_FORBIDDEN), equalTo(false)); + } + +}