PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/binders/BindTagFiltersToIndexedFormParamsTest.java

https://github.com/alasdairhodge/jclouds
Java | 108 lines | 69 code | 16 blank | 23 comment | 0 complexity | d454a22ffadc0fa97b56fe9cf6fa6bd6 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.aws.ec2.binders;
  20. import static org.testng.Assert.*;
  21. import java.io.File;
  22. import java.net.URI;
  23. import javax.ws.rs.HttpMethod;
  24. import org.jclouds.aws.ec2.util.TagFilters;
  25. import org.jclouds.aws.ec2.util.TagFilters.FilterName;
  26. import org.jclouds.aws.ec2.util.TagFilters.ResourceType;
  27. import org.jclouds.http.HttpRequest;
  28. import org.testng.annotations.Test;
  29. import com.google.common.collect.ImmutableMap;
  30. import com.google.common.collect.ImmutableSet;
  31. import com.google.inject.Guice;
  32. import com.google.inject.Injector;
  33. /**
  34. * Tests behavior of {@code BindTagFiltersToIndexedFormParams}
  35. *
  36. * @author grkvlt@apache.org
  37. */
  38. @Test(groups = "unit")
  39. public class BindTagFiltersToIndexedFormParamsTest {
  40. Injector injector = Guice.createInjector();
  41. BindTagFiltersToIndexedFormParams binder = injector.getInstance(BindTagFiltersToIndexedFormParams.class);
  42. public void testMultipleResourceTypes() {
  43. HttpRequest request = HttpRequest.builder().method("POST").endpoint(URI.create("http://localhost")).build();
  44. request = binder.bindToRequest(request, ImmutableMap.<FilterName, Iterable<ResourceType>>builder().put(FilterName.RESOURCE_TYPE, ImmutableSet.<ResourceType>of(ResourceType.VPN_GATEWAY, ResourceType.INTERNET_GATEWAY)).build());
  45. assertEquals(request.getPayload().getRawContent(), "Filter.1.Name=resource-type&Filter.1.Value.1=vpn-gateway&Filter.1.Value.2=internet-gateway");
  46. }
  47. public void testMultipleKeys() {
  48. HttpRequest request = HttpRequest.builder().method("POST").endpoint(URI.create("http://localhost")).build();
  49. request = binder.bindToRequest(request, ImmutableMap.<FilterName, Iterable<String>>builder().put(FilterName.KEY, ImmutableSet.<String>of("one", "two")).build());
  50. assertEquals(request.getPayload().getRawContent(), "Filter.1.Name=key&Filter.1.Value.1=one&Filter.1.Value.2=two");
  51. }
  52. public void testkeyWithValue() {
  53. HttpRequest request = HttpRequest.builder().method("POST").endpoint(URI.create("http://localhost")).build();
  54. request = binder.bindToRequest(request, ImmutableMap.<FilterName, Iterable<String>>builder().put(FilterName.KEY, ImmutableSet.<String>of("one")).put(FilterName.VALUE, ImmutableSet.<String>of("alpha")).build());
  55. assertEquals(request.getPayload().getRawContent(), "Filter.1.Name=key&Filter.1.Value.1=one&Filter.2.Name=value&Filter.2.Value.1=alpha");
  56. }
  57. public void testAnyKey() {
  58. HttpRequest request = HttpRequest.builder().method("POST").endpoint(URI.create("http://localhost")).build();
  59. request = binder.bindToRequest(request, ImmutableMap.<FilterName, Iterable<String>>builder().put(FilterName.KEY, ImmutableSet.<String>of()).build());
  60. assertEquals(request.getPayload().getRawContent(), "Filter.1.Name=key");
  61. }
  62. public void testMultipleResourceTypesBuilder() {
  63. HttpRequest request = HttpRequest.builder().method("POST").endpoint(URI.create("http://localhost")).build();
  64. request = binder.bindToRequest(request, TagFilters.filters().vpnGateway().internetGateway().build());
  65. assertEquals(request.getPayload().getRawContent(), "Filter.1.Name=resource-type&Filter.1.Value.1=vpn-gateway&Filter.1.Value.2=internet-gateway");
  66. }
  67. public void testMultipleKeysBuilder() {
  68. HttpRequest request = HttpRequest.builder().method("POST").endpoint(URI.create("http://localhost")).build();
  69. request = binder.bindToRequest(request, TagFilters.filters().key("one").key("two").build());
  70. assertEquals(request.getPayload().getRawContent(), "Filter.1.Name=key&Filter.1.Value.1=one&Filter.1.Value.2=two");
  71. }
  72. public void testKeyWithValueBuilder() {
  73. HttpRequest request = HttpRequest.builder().method("POST").endpoint(URI.create("http://localhost")).build();
  74. request = binder.bindToRequest(request, TagFilters.filters().keyValuePair("one", "alpha").build());
  75. assertEquals(request.getPayload().getRawContent(), "Filter.1.Name=key&Filter.1.Value.1=one&Filter.2.Name=value&Filter.2.Value.1=alpha");
  76. }
  77. public void testAnyKeyBuilder() {
  78. HttpRequest request = HttpRequest.builder().method("POST").endpoint(URI.create("http://localhost")).build();
  79. request = binder.bindToRequest(request, TagFilters.filters().anyKey().build());
  80. assertEquals(request.getPayload().getRawContent(), "Filter.1.Name=key");
  81. }
  82. @Test(expectedExceptions = IllegalArgumentException.class)
  83. public void testMustBeArray() {
  84. HttpRequest request = new HttpRequest(HttpMethod.POST, URI.create("http://localhost"));
  85. binder.bindToRequest(request, new File("foo"));
  86. }
  87. @Test(expectedExceptions = NullPointerException.class)
  88. public void testNullIsBad() {
  89. HttpRequest request = HttpRequest.builder().method("GET").endpoint(URI.create("http://momma")).build();
  90. binder.bindToRequest(request, null);
  91. }
  92. }