/apis/vcloud/src/test/java/org/jclouds/vcloud/suppliers/OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefaultTest.java

https://github.com/regularfry/jclouds · Java · 124 lines · 73 code · 25 blank · 26 comment · 0 complexity · f89ded10d7bae31d87490b6afb971f7f 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.suppliers;
  20. import static org.easymock.classextension.EasyMock.createMock;
  21. import static org.testng.Assert.assertEquals;
  22. import java.util.NoSuchElementException;
  23. import org.jclouds.config.ValueOfConfigurationKeyOrNull;
  24. import org.jclouds.vcloud.domain.ReferenceType;
  25. import org.jclouds.vcloud.domain.internal.ReferenceTypeImpl;
  26. import org.testng.annotations.Test;
  27. import com.google.common.base.Predicates;
  28. import com.google.common.collect.ImmutableList;
  29. import com.google.common.collect.ImmutableSet;
  30. import com.google.inject.AbstractModule;
  31. import com.google.inject.Guice;
  32. import com.google.inject.name.Names;
  33. /**
  34. * Tests behavior of
  35. * {@code OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault}
  36. *
  37. * @author Adrian Cole
  38. */
  39. // NOTE:without testName, this will not call @Before* and fail w/NPE during
  40. // surefire
  41. @Test(groups = "unit", testName = "OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefaultTest")
  42. public class OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefaultTest {
  43. ValueOfConfigurationKeyOrNull valueOfConfigurationKeyOrNull = new ValueOfConfigurationKeyOrNull(
  44. Guice.createInjector());
  45. @Test(expectedExceptions = IllegalArgumentException.class)
  46. public void testIllegalArgumentWhenResourcesEmpty() {
  47. new OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault(valueOfConfigurationKeyOrNull, "foo",
  48. Predicates.<ReferenceType> alwaysTrue()).apply(ImmutableSet.<ReferenceType> of());
  49. }
  50. @Test
  51. public void testReturnsOnlyResource() {
  52. ReferenceType reference = createMock(ReferenceType.class);
  53. assertEquals(new OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault(valueOfConfigurationKeyOrNull,
  54. "foo", Predicates.<ReferenceType> alwaysTrue()).apply(ImmutableSet.<ReferenceType> of(reference)),
  55. reference);
  56. }
  57. @Test
  58. public void testReturnsFirstResourceWhenConfigurationUnspecified() {
  59. ReferenceType reference1 = createMock(ReferenceType.class);
  60. ReferenceType reference2 = createMock(ReferenceType.class);
  61. assertEquals(new OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault(valueOfConfigurationKeyOrNull,
  62. "foo", Predicates.<ReferenceType> alwaysTrue()).apply(ImmutableList.<ReferenceType> of(reference1,
  63. reference2)), reference1);
  64. }
  65. @Test
  66. public void testReturnsResourceMatchingDefaultPredicateWhenConfigurationUnspecified() {
  67. ReferenceType reference1 = createMock(ReferenceType.class);
  68. ReferenceType reference2 = createMock(ReferenceType.class);
  69. assertEquals(new OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault(valueOfConfigurationKeyOrNull,
  70. "foo", Predicates.equalTo(reference2)).apply(ImmutableList.<ReferenceType> of(reference1, reference2)),
  71. reference2);
  72. }
  73. @Test
  74. public void testReturnsResourceWithNameMatchingConfigurationKey() {
  75. ReferenceType reference1 = new ReferenceTypeImpl("travis tritt", null, null);
  76. ReferenceType reference2 = new ReferenceTypeImpl("hail mary", null, null);
  77. assertEquals(new OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault(
  78. new ValueOfConfigurationKeyOrNull(Guice.createInjector(new AbstractModule() {
  79. @Override
  80. protected void configure() {
  81. bindConstant().annotatedWith(Names.named("foo")).to(".*mary.*");
  82. }
  83. })), "foo", Predicates.<ReferenceType> alwaysTrue()).apply(ImmutableList.<ReferenceType> of(reference1,
  84. reference2)), reference2);
  85. }
  86. @Test(expectedExceptions = NoSuchElementException.class)
  87. public void testThrowsNoSuchElementWhenNoneMatchConfigurationKey() {
  88. ReferenceType reference1 = new ReferenceTypeImpl("travis tritt", null, null);
  89. ReferenceType reference2 = new ReferenceTypeImpl("hail mary", null, null);
  90. new OnlyReferenceTypeFirstWithNameMatchingConfigurationKeyOrDefault(new ValueOfConfigurationKeyOrNull(
  91. Guice.createInjector(new AbstractModule() {
  92. @Override
  93. protected void configure() {
  94. bindConstant().annotatedWith(Names.named("foo")).to(".*happy.*");
  95. }
  96. })), "foo", Predicates.<ReferenceType> alwaysTrue()).apply(ImmutableList.<ReferenceType> of(reference1,
  97. reference2));
  98. }
  99. }