PageRenderTime 8ms CodeModel.GetById 47ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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

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