PageRenderTime 72ms CodeModel.GetById 49ms app.highlight 20ms RepoModel.GetById 0ms app.codeStats 0ms

/apis/elasticstack/src/test/java/org/jclouds/elasticstack/handlers/ElasticStackErrorHandlerTest.java

https://github.com/gnodet/jclouds
Java | 140 lines | 93 code | 25 blank | 22 comment | 1 complexity | 19f393e9a6a4eb01f8534f6295f845b5 MD5 | raw file
  1/**
  2 * Licensed to jclouds, Inc. (jclouds) under one or more
  3 * contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  jclouds licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *   http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing,
 13 * software distributed under the License is distributed on an
 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15 * KIND, either express or implied.  See the License for the
 16 * specific language governing permissions and limitations
 17 * under the License.
 18 */
 19package org.jclouds.elasticstack.handlers;
 20
 21import static org.easymock.EasyMock.expect;
 22import static org.easymock.EasyMock.reportMatcher;
 23import static org.easymock.classextension.EasyMock.createMock;
 24import static org.easymock.classextension.EasyMock.replay;
 25import static org.easymock.classextension.EasyMock.verify;
 26
 27import java.net.URI;
 28
 29import org.easymock.IArgumentMatcher;
 30import org.jclouds.http.HttpCommand;
 31import org.jclouds.http.HttpRequest;
 32import org.jclouds.http.HttpResponse;
 33import org.jclouds.io.Payloads;
 34import org.jclouds.rest.AuthorizationException;
 35import org.jclouds.rest.ResourceNotFoundException;
 36import org.jclouds.util.Strings2;
 37import org.testng.annotations.Test;
 38
 39import com.google.inject.Guice;
 40
 41/**
 42 * 
 43 * @author Adrian Cole
 44 */
 45@Test(groups = { "unit" })
 46public class ElasticStackErrorHandlerTest {
 47
 48   @Test
 49   public void test400MakesIllegalArgumentException() {
 50      assertCodeMakes("GET", URI.create("https://elasticstack.com/foo"), 400, "", "Bad Request",
 51            IllegalArgumentException.class);
 52   }
 53
 54   @Test
 55   public void test400MakesResourceNotFoundExceptionOnInfo() {
 56      assertCodeMakes("GET", URI.create("https://elasticstack.com/foo/info"), 400, "", "",
 57            ResourceNotFoundException.class);
 58   }
 59
 60   @Test
 61   public void test400MakesResourceNotFoundExceptionOnMessageNotFound() {
 62      assertCodeMakes(
 63            "GET",
 64            URI.create("https://elasticstack.com/foo"),
 65            400,
 66            "",
 67            "errors:system Drive 8f9b42b1-26de-49ad-a3fd-d4fa06524339 could not be found. Please re-validate your entry.",
 68            ResourceNotFoundException.class);
 69   }
 70
 71   @Test
 72   public void test401MakesAuthorizationException() {
 73      assertCodeMakes("GET", URI.create("https://elasticstack.com/foo"), 401, "", "Unauthorized",
 74            AuthorizationException.class);
 75   }
 76
 77   @Test
 78   public void test404MakesResourceNotFoundException() {
 79      assertCodeMakes("GET", URI.create("https://elasticstack.com/foo"), 404, "", "Not Found",
 80            ResourceNotFoundException.class);
 81   }
 82
 83   @Test
 84   public void test405MakesIllegalArgumentException() {
 85      assertCodeMakes("GET", URI.create("https://elasticstack.com/foo"), 405, "", "Method Not Allowed",
 86            IllegalArgumentException.class);
 87   }
 88
 89   @Test
 90   public void test409MakesIllegalStateException() {
 91      assertCodeMakes("GET", URI.create("https://elasticstack.com/foo"), 409, "", "Conflict",
 92            IllegalStateException.class);
 93   }
 94
 95   private void assertCodeMakes(String method, URI uri, int statusCode, String message, String content,
 96         Class<? extends Exception> expected) {
 97      assertCodeMakes(method, uri, statusCode, message, "text/xml", content, expected);
 98   }
 99
100   private void assertCodeMakes(String method, URI uri, int statusCode, String message, String contentType,
101         String content, Class<? extends Exception> expected) {
102
103      ElasticStackErrorHandler function = Guice.createInjector().getInstance(ElasticStackErrorHandler.class);
104
105      HttpCommand command = createMock(HttpCommand.class);
106      HttpRequest request = new HttpRequest(method, uri);
107      HttpResponse response = new HttpResponse(statusCode, message, Payloads.newInputStreamPayload(Strings2
108            .toInputStream(content)));
109      response.getPayload().getContentMetadata().setContentType(contentType);
110
111      expect(command.getCurrentRequest()).andReturn(request).atLeastOnce();
112      command.setException(classEq(expected));
113
114      replay(command);
115
116      function.handleError(command, response);
117
118      verify(command);
119   }
120
121   public static Exception classEq(final Class<? extends Exception> in) {
122      reportMatcher(new IArgumentMatcher() {
123
124         @Override
125         public void appendTo(StringBuffer buffer) {
126            buffer.append("classEq(");
127            buffer.append(in);
128            buffer.append(")");
129         }
130
131         @Override
132         public boolean matches(Object arg) {
133            return arg.getClass() == in;
134         }
135
136      });
137      return null;
138   }
139
140}