PageRenderTime 385ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/BaseVCloudDirectorExpectTest.java

http://github.com/jclouds/jclouds
Java | 266 lines | 197 code | 40 blank | 29 comment | 4 complexity | 56d577cd3f301bfd0c6b3a1892375315 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. */
  19. package org.jclouds.vcloud.director.v1_5.internal;
  20. import static com.google.common.base.Preconditions.checkNotNull;
  21. import static org.testng.Assert.assertNotNull;
  22. import java.net.URI;
  23. import java.util.Properties;
  24. import org.jclouds.Constants;
  25. import org.jclouds.date.DateService;
  26. import org.jclouds.http.HttpRequest;
  27. import org.jclouds.http.HttpResponse;
  28. import org.jclouds.rest.internal.BaseRestApiExpectTest;
  29. import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
  30. import org.testng.annotations.BeforeGroups;
  31. import com.google.common.base.Throwables;
  32. import com.google.common.collect.ImmutableMultimap;
  33. import com.google.common.collect.LinkedListMultimap;
  34. import com.google.common.collect.Multimap;
  35. import com.google.common.net.HttpHeaders;
  36. import com.google.inject.Guice;
  37. import com.jamesmurty.utils.XMLBuilder;
  38. /**
  39. * Base class for writing vCloud Director REST api expect tests.
  40. *
  41. * @author Adrian Cole
  42. */
  43. public abstract class BaseVCloudDirectorExpectTest<T> extends BaseRestApiExpectTest<T> {
  44. public BaseVCloudDirectorExpectTest() {
  45. provider = "vcloud-director";
  46. identity = String.format("%s@%s", user, org);
  47. credential = password;
  48. }
  49. public static final String user = "adrian@jclouds.org";
  50. public static final String org = "JClouds";
  51. public static final String password = "password";
  52. public static final String token = "mIaR3/6Lna8DWImd7/JPR5rK8FcUHabt+G/UCJV5pJQ=";
  53. public static final String endpoint = "https://vcloudbeta.bluelock.com/api";
  54. protected static DateService dateService;
  55. @BeforeGroups("unit")
  56. protected static void setupDateService() {
  57. dateService = Guice.createInjector().getInstance(DateService.class);
  58. assertNotNull(dateService);
  59. }
  60. @Override
  61. public Properties setupProperties() {
  62. Properties props = new Properties();
  63. props.put(Constants.PROPERTY_MAX_RETRIES, 1);
  64. props.put(Constants.PROPERTY_ENDPOINT, endpoint);
  65. return props;
  66. }
  67. @Override
  68. public HttpRequestComparisonType compareHttpRequestAsType(HttpRequest input) {
  69. if (input.getPayload() == null || input.getPayload().getContentMetadata().getContentLength() == 0) {
  70. return HttpRequestComparisonType.DEFAULT;
  71. }
  72. return HttpRequestComparisonType.XML;
  73. }
  74. protected HttpRequest loginRequest = HttpRequest.builder().method("POST")
  75. .endpoint(endpoint + "/sessions")
  76. .addHeader("Accept", "*/*")
  77. .addHeader("Authorization", "Basic YWRyaWFuQGpjbG91ZHMub3JnQEpDbG91ZHM6cGFzc3dvcmQ=")
  78. .build();
  79. protected HttpResponse sessionResponse = HttpResponse.builder()
  80. .statusCode(200)
  81. .headers(ImmutableMultimap.<String, String> builder()
  82. .put("x-vcloud-authorization", token)
  83. .put("Set-Cookie", String.format("vcloud-token=%s; Secure; Path=/", token))
  84. .build())
  85. .payload(payloadFromResourceWithContentType("/session.xml", VCloudDirectorMediaType.SESSION + ";version=1.5"))
  86. .build();
  87. @Deprecated
  88. protected HttpRequest getStandardRequest(String method, String path) {
  89. return getStandardRequest(method, path, VCloudDirectorMediaType.ANY);
  90. }
  91. @Deprecated
  92. protected HttpRequest getStandardRequest(String method, URI uri) {
  93. return getStandardRequest(method, uri, VCloudDirectorMediaType.ANY);
  94. }
  95. @Deprecated
  96. protected HttpRequest getStandardRequest(String method, String path, String mediaType) {
  97. return getStandardRequest(method, URI.create(endpoint + path), VCloudDirectorMediaType.ANY);
  98. }
  99. @Deprecated
  100. protected HttpRequest getStandardPayloadRequest(String method, String command, String relativeFilePath,
  101. String postMediaType) {
  102. return getStandardPayloadRequest(method, URI.create(endpoint + command), relativeFilePath, postMediaType);
  103. }
  104. @Deprecated
  105. protected HttpRequest getStandardPayloadRequest(String method, URI uri, String relativeFilePath,
  106. String postMediaType) {
  107. return getStandardRequestWithPayload(method, uri, VCloudDirectorMediaType.ANY, relativeFilePath, postMediaType);
  108. }
  109. @Deprecated
  110. protected HttpRequest getStandardRequest(String method, URI uri, String mediaType) {
  111. return HttpRequest.builder()
  112. .method(method)
  113. .endpoint(uri)
  114. .headers(ImmutableMultimap.<String, String> builder()
  115. .put("Accept", mediaType)
  116. .put("x-vcloud-authorization", token)
  117. .put(HttpHeaders.COOKIE, "vcloud-token=" + token)
  118. .build())
  119. .build();
  120. }
  121. @Deprecated
  122. protected HttpRequest getStandardRequestWithPayload(String method, String path, String relativeFilePath, String mediaType) {
  123. return getStandardRequestWithPayload(method, path, VCloudDirectorMediaType.ANY, relativeFilePath, mediaType);
  124. }
  125. @Deprecated
  126. protected HttpRequest getStandardRequestWithPayload(String method, URI uri, String relativeFilePath, String mediaType) {
  127. return getStandardRequestWithPayload(method, uri, VCloudDirectorMediaType.ANY, relativeFilePath, mediaType);
  128. }
  129. @Deprecated
  130. protected HttpRequest getStandardRequestWithPayload(String method, String path, String acceptType, String relativeFilePath, String mediaType) {
  131. URI uri = URI.create(endpoint + path);
  132. return getStandardRequestWithPayload(method, uri, acceptType, relativeFilePath, mediaType);
  133. }
  134. @Deprecated
  135. protected HttpRequest getStandardRequestWithPayload(String method, URI uri, String acceptType, String relativeFilePath, String mediaType) {
  136. return HttpRequest.builder()
  137. .method(method)
  138. .endpoint(uri)
  139. .headers(ImmutableMultimap.<String, String> builder()
  140. .put("Accept", acceptType)
  141. .put("x-vcloud-authorization", token)
  142. .put(HttpHeaders.COOKIE, "vcloud-token=" + token)
  143. .build())
  144. .payload(payloadFromResourceWithContentType(relativeFilePath, mediaType))
  145. .build();
  146. }
  147. @Deprecated
  148. protected HttpResponse getStandardPayloadResponse(String relativeFilePath, String mediaType) {
  149. return getStandardPayloadResponse(200, relativeFilePath, mediaType);
  150. }
  151. @Deprecated
  152. protected HttpResponse getStandardPayloadResponse(int statusCode, String relativeFilePath, String mediaType) {
  153. return HttpResponse.builder()
  154. .statusCode(statusCode)
  155. .payload(payloadFromResourceWithContentType(relativeFilePath, mediaType + ";version=1.5"))
  156. .build();
  157. }
  158. /**
  159. * Implicitly adds x-vcloud-authorization header with token.
  160. * Provides convenience methods for priming a HttpRequest.Builder for vCloud testing
  161. *
  162. * @author danikov
  163. */
  164. @Deprecated
  165. public class VcloudHttpRequestPrimer {
  166. private Multimap<String, String> headers = LinkedListMultimap.create();
  167. private HttpRequest.Builder<?> builder = HttpRequest.builder();
  168. public VcloudHttpRequestPrimer() {
  169. }
  170. public VcloudHttpRequestPrimer apiCommand(String method, String command) {
  171. builder.method(method).endpoint(endpoint + command);
  172. return this;
  173. }
  174. public VcloudHttpRequestPrimer xmlFilePayload(String relativeFilePath, String mediaType) {
  175. builder.payload(payloadFromResourceWithContentType(relativeFilePath, mediaType));
  176. return this;
  177. }
  178. public VcloudHttpRequestPrimer headers(Multimap<String, String> headers) {
  179. this.headers.putAll(ImmutableMultimap.copyOf(checkNotNull(headers, "headers")));
  180. return this;
  181. }
  182. public VcloudHttpRequestPrimer acceptAnyMedia() {
  183. return acceptMedia(VCloudDirectorMediaType.ANY);
  184. }
  185. public VcloudHttpRequestPrimer acceptMedia(String media) {
  186. return header("Accept", media);
  187. }
  188. public VcloudHttpRequestPrimer header(String name, String value) {
  189. headers.put(checkNotNull(name, "header.name"), checkNotNull(value, "header.value"));
  190. return this;
  191. }
  192. public HttpRequest.Builder<?> httpRequestBuilder() {
  193. header("x-vcloud-authorization", token);
  194. header(HttpHeaders.COOKIE, "vcloud-token=" + token);
  195. builder.headers(headers);
  196. return builder;
  197. }
  198. }
  199. @Deprecated
  200. protected class VcloudHttpResponsePrimer {
  201. private HttpResponse.Builder<?> builder = HttpResponse.builder().statusCode(200);
  202. public VcloudHttpResponsePrimer() {
  203. }
  204. public VcloudHttpResponsePrimer xmlFilePayload(String relativeFilePath, String mediaType) {
  205. builder.payload(payloadFromResourceWithContentType(relativeFilePath, mediaType));
  206. return this;
  207. }
  208. public HttpResponse.Builder<?> httpResponseBuilder() {
  209. return builder;
  210. }
  211. }
  212. protected static XMLBuilder createXMLBuilder(String root){
  213. try {
  214. return XMLBuilder.create(root);
  215. } catch (Exception e) {
  216. throw Throwables.propagate(e);
  217. }
  218. }
  219. protected static String asString(XMLBuilder in){
  220. try {
  221. return in.asString();
  222. } catch (Exception e) {
  223. throw Throwables.propagate(e);
  224. }
  225. }
  226. }