diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/predicates/UserPredicates.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/predicates/UserPredicates.java index 518804c753..52dc210313 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/predicates/UserPredicates.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/predicates/UserPredicates.java @@ -60,14 +60,7 @@ public class UserPredicates { /** * - * @return true, if the account has user privileges - */ - public static Predicate isUserAccount() { - return accountTypeEquals(Account.Type.USER); - } - /** - * - * @return true, if the user's apiKey is the following + * @return true, if the user's account type is the following */ public static Predicate accountTypeEquals(Account.Type type) { return new AccountTypeEquals(type); @@ -90,12 +83,27 @@ public class UserPredicates { return "accountTypeEquals(" + type + ")"; } } + + /** + * + * @return true, if the account has user privileges + */ + public static Predicate isUserAccount() { + return accountTypeEquals(Account.Type.USER); + } + + /** + * @return true, is the user is a domain admin + */ + public static Predicate isDomainAdminAccount() { + return accountTypeEquals(Type.DOMAIN_ADMIN); + } + /** * - * @return true, if the user's apiKey is the following + * @return true, if the user is a global admin */ public static Predicate isAdminAccount() { return accountTypeEquals(Account.Type.ADMIN); } - } diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/predicates/ZonePredicates.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/predicates/ZonePredicates.java index 99334d325c..79b3aa6a2a 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/predicates/ZonePredicates.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/predicates/ZonePredicates.java @@ -39,13 +39,13 @@ public class ZonePredicates { return new Predicate() { @Override - public boolean apply(Zone input) { - return NetworkType.ADVANCED.equals(checkNotNull(input, "zone").getNetworkType()); + public boolean apply(Zone zone) { + return NetworkType.ADVANCED.equals(checkNotNull(zone, "zone").getNetworkType()); } @Override public String toString() { - return "supportsAvancedNetworks()"; + return "supportsAdvancedNetworks()"; } }; } diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/JobCompleteTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/JobCompleteTest.java new file mode 100644 index 0000000000..4c5c74d34d --- /dev/null +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/JobCompleteTest.java @@ -0,0 +1,52 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.cloudstack.predicates; + +import org.jclouds.cloudstack.CloudStackClient; +import org.jclouds.cloudstack.domain.AsyncJob; +import org.jclouds.cloudstack.features.AsyncJobClient; +import org.testng.annotations.Test; + +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.testng.Assert.assertTrue; + +/** + * @author Andrei Savu + */ +@Test(groups = "unit") +public class JobCompleteTest { + + @Test + public void testJobComplete() { + CloudStackClient client = createMock(CloudStackClient.class); + AsyncJobClient asyncJobClient = createMock(AsyncJobClient.class); + + expect(client.getAsyncJobClient()).andReturn(asyncJobClient); + + AsyncJob job = AsyncJob.builder().id(100L).status(1).build(); + expect(asyncJobClient.getAsyncJob(job.getId())).andReturn(job); + + replay(client, asyncJobClient); + assertTrue(new JobComplete(client).apply(job.getId())); + verify(client, asyncJobClient); + } +} diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/OSCategoryInTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/OSCategoryInTest.java new file mode 100644 index 0000000000..5a1dae378d --- /dev/null +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/OSCategoryInTest.java @@ -0,0 +1,90 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.jclouds.cloudstack.predicates; + +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Maps; +import org.jclouds.cloudstack.CloudStackClient; +import org.jclouds.cloudstack.domain.OSType; +import org.jclouds.cloudstack.domain.Template; +import org.jclouds.cloudstack.features.GuestOSClient; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.Map; +import java.util.Set; + +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + +/** + * @author Andrei Savu + */ +@Test(groups = "unit") +public class OSCategoryInTest { + + private CloudStackClient client; + private GuestOSClient guestOSClient; + private Set acceptableCategories = ImmutableSet.of("Ubuntu"); + + @BeforeMethod + public void setUp() { + client = createMock(CloudStackClient.class); + guestOSClient = createMock(GuestOSClient.class); + + expect(client.getGuestOSClient()).andReturn(guestOSClient).times(2); + + Map osCategories = Maps.newHashMap(); + osCategories.put(1L, "Ubuntu"); + osCategories.put(2L, "CentOS"); + osCategories.put(3L, "RHEL"); + + expect(guestOSClient.listOSCategories()).andReturn(osCategories); + + Set osTypes = ImmutableSet.of( + OSType.builder().id(10L).OSCategoryId(1).description("Ubuntu 10.04 LTS").build(), + OSType.builder().id(20L).OSCategoryId(2).description("CentOS 5.4").build(), + OSType.builder().id(30L).OSCategoryId(3).description("RHEL 6").build() + ); + + expect(guestOSClient.listOSTypes()).andReturn(osTypes); + replay(client, guestOSClient); + } + + @Test + public void testTemplateInAcceptableCategory() { + assertTrue(new OSCategoryIn(client).apply(acceptableCategories).apply( + Template.builder().OSTypeId(10L).build() + )); + verify(client, guestOSClient); + } + + @Test + public void testTemplateNotInAcceptableCategory() { + assertFalse(new OSCategoryIn(client).apply(acceptableCategories).apply( + Template.builder().OSTypeId(30L).build() + )); + verify(client, guestOSClient); + } +} diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/TemplatePredicatesTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/TemplatePredicatesTest.java new file mode 100644 index 0000000000..a98aedb1fa --- /dev/null +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/TemplatePredicatesTest.java @@ -0,0 +1,36 @@ +package org.jclouds.cloudstack.predicates; + +import org.jclouds.cloudstack.domain.Template; +import org.testng.annotations.Test; + +import static org.jclouds.cloudstack.predicates.TemplatePredicates.isPasswordEnabled; +import static org.jclouds.cloudstack.predicates.TemplatePredicates.isReady; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + +/** + * @author Andrei Savu + */ +@Test(groups = "unit") +public class TemplatePredicatesTest { + + @Test + public void testTemplateIsReady() { + assertTrue(isReady().apply( + Template.builder().ready(true).build() + )); + assertFalse(isReady().apply( + Template.builder().ready(false).build() + )); + } + + @Test + public void testTemplateIsPasswordEnabled() { + assertTrue(isPasswordEnabled().apply( + Template.builder().passwordEnabled(true).build() + )); + assertFalse(isPasswordEnabled().apply( + Template.builder().passwordEnabled(false).build() + )); + } +} diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/UserPredicatesTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/UserPredicatesTest.java new file mode 100644 index 0000000000..4fe137ad56 --- /dev/null +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/UserPredicatesTest.java @@ -0,0 +1,51 @@ +package org.jclouds.cloudstack.predicates; + +import org.jclouds.cloudstack.domain.Account; +import org.jclouds.cloudstack.domain.User; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import static org.jclouds.cloudstack.predicates.UserPredicates.apiKeyEquals; +import static org.jclouds.cloudstack.predicates.UserPredicates.isAdminAccount; +import static org.jclouds.cloudstack.predicates.UserPredicates.isDomainAdminAccount; +import static org.jclouds.cloudstack.predicates.UserPredicates.isUserAccount; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + +/** + * @author Andrei Savu + */ +@Test(groups = "unit") +public class UserPredicatesTest { + + @Test + public void testMatchApiKey() { + assertTrue(apiKeyEquals("random-text").apply( + User.builder().apiKey("random-text").build() + )); + assertFalse(apiKeyEquals("something-different").apply( + User.builder().apiKey("random-text").build() + )); + } + + @DataProvider(name = "accountType") + public Object[][] getAccountTypes() { + return new Object[][] { + /* Type ID, isUser, isDomainAdmin, isAdmin */ + {Account.Type.USER, true, false, false}, + {Account.Type.DOMAIN_ADMIN, false, true, false}, + {Account.Type.ADMIN, false, false, true}, + {Account.Type.UNRECOGNIZED, false, false, false} + }; + } + + @Test(dataProvider = "accountType") + public void testAccountType(Account.Type type, boolean isUser, boolean isDomainAdmin, boolean isAdmin) { + User testUser = User.builder().accountType(type).build(); + assertEquals(isUserAccount().apply(testUser), isUser); + assertEquals(isDomainAdminAccount().apply(testUser), isDomainAdmin); + assertEquals(isAdminAccount().apply(testUser), isAdmin); + } + +} diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/VirtualMachineDestroyedTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/VirtualMachineDestroyedTest.java new file mode 100644 index 0000000000..aaec87c4b5 --- /dev/null +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/VirtualMachineDestroyedTest.java @@ -0,0 +1,74 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.jclouds.cloudstack.predicates; + +import org.jclouds.cloudstack.CloudStackClient; +import org.jclouds.cloudstack.domain.VirtualMachine; +import org.jclouds.cloudstack.features.VirtualMachineClient; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + +/** + * @author Andrei Savu + */ +@Test(groups = "unit", testName = "VirtualMachineDestroyedTest") +public class VirtualMachineDestroyedTest { + + CloudStackClient client; + VirtualMachineClient virtualMachineClient; + + @BeforeMethod + public void setUp() { + client = createMock(CloudStackClient.class); + virtualMachineClient = createMock(VirtualMachineClient.class); + expect(client.getVirtualMachineClient()).andReturn(virtualMachineClient); + } + + @Test + public void testIsDestroyed() { + VirtualMachine virtualMachine = VirtualMachine.builder(). + id(229).state(VirtualMachine.State.DESTROYED).build(); + + expect(virtualMachineClient.getVirtualMachine(virtualMachine.getId())).andReturn(virtualMachine); + + replay(client, virtualMachineClient); + assertTrue(new VirtualMachineDestroyed(client).apply(virtualMachine)); + verify(client, virtualMachineClient); + } + + @Test + public void testStillRunning() { + VirtualMachine virtualMachine = VirtualMachine.builder(). + id(229).state(VirtualMachine.State.RUNNING).build(); + + expect(virtualMachineClient.getVirtualMachine(virtualMachine.getId())).andReturn(virtualMachine); + + replay(client, virtualMachineClient); + assertFalse(new VirtualMachineDestroyed(client).apply(virtualMachine)); + verify(client, virtualMachineClient); + } +} diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/VirtualMachineExpungedTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/VirtualMachineExpungedTest.java index 380c022488..245af44f4e 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/VirtualMachineExpungedTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/VirtualMachineExpungedTest.java @@ -22,12 +22,14 @@ package org.jclouds.cloudstack.predicates; import org.jclouds.cloudstack.CloudStackClient; import org.jclouds.cloudstack.domain.VirtualMachine; import org.jclouds.cloudstack.features.VirtualMachineClient; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.verify; +import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; /** @@ -36,21 +38,33 @@ import static org.testng.Assert.assertTrue; @Test(groups = "unit", testName = "VirtualMachineExpungedTest") public class VirtualMachineExpungedTest { + CloudStackClient client; + VirtualMachineClient virtualMachineClient; + + @BeforeMethod + public void setUp() { + client = createMock(CloudStackClient.class); + virtualMachineClient = createMock(VirtualMachineClient.class); + expect(client.getVirtualMachineClient()).andReturn(virtualMachineClient); + } + @Test public void testWaitForVirtualMachineToBeExpunged() { - CloudStackClient client = createMock(CloudStackClient.class); - VirtualMachineClient virtualMachineClient = createMock(VirtualMachineClient.class); - - long virtualMachineId = 229; - VirtualMachine virtualMachine = VirtualMachine.builder().id(virtualMachineId).build(); - - expect(client.getVirtualMachineClient()).andReturn(virtualMachineClient); - expect(virtualMachineClient.getVirtualMachine(virtualMachineId)).andReturn(null); + VirtualMachine virtualMachine = VirtualMachine.builder().id(229L).build(); + expect(virtualMachineClient.getVirtualMachine(virtualMachine.getId())).andReturn(null); replay(client, virtualMachineClient); - assertTrue(new VirtualMachineExpunged(client).apply(virtualMachine)); + verify(client, virtualMachineClient); + } + @Test + public void testNoRemovedYet() { + VirtualMachine virtualMachine = VirtualMachine.builder().id(229L).build(); + expect(virtualMachineClient.getVirtualMachine(virtualMachine.getId())).andReturn(virtualMachine); + + replay(client, virtualMachineClient); + assertFalse(new VirtualMachineExpunged(client).apply(virtualMachine)); verify(client, virtualMachineClient); } } diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/VirtualMachineRunningTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/VirtualMachineRunningTest.java new file mode 100644 index 0000000000..65eb210924 --- /dev/null +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/VirtualMachineRunningTest.java @@ -0,0 +1,88 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.jclouds.cloudstack.predicates; + +import org.jclouds.cloudstack.CloudStackClient; +import org.jclouds.cloudstack.domain.VirtualMachine; +import org.jclouds.cloudstack.features.VirtualMachineClient; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.jclouds.cloudstack.domain.VirtualMachine.State; +import static org.testng.Assert.assertEquals; + +/** + * @author Andrei Savu + */ +@Test(groups = "unit", testName = "VirtualMachineRunningTest") +public class VirtualMachineRunningTest { + + CloudStackClient client; + VirtualMachineClient virtualMachineClient; + + @BeforeMethod + public void setUp() { + client = createMock(CloudStackClient.class); + virtualMachineClient = createMock(VirtualMachineClient.class); + + expect(client.getVirtualMachineClient()).andReturn(virtualMachineClient); + } + + @DataProvider(name = "virtualMachineStates") + public Object[][] virtualMachineStates() { + return new Object[][]{ + {State.RUNNING, true}, + {State.STARTING, false}, + {State.STOPPING, false}, + {State.STOPPED, false}, + {State.SHUTDOWNED, false}, + {State.DESTROYED, false}, + {State.EXPUNGING, false}, + {State.MIGRATING, false} + }; + } + + @Test(dataProvider = "virtualMachineStates") + public void testWaitForVirtualMachineToBeRunning(State state, boolean expected) { + assertPredicateResult(state, expected); + } + + @Test(expectedExceptions = IllegalStateException.class) + public void testThrowExceptionOnErrorState() { + assertPredicateResult(State.ERROR, true); + } + + private void assertPredicateResult(State state, boolean expected) { + long virtualMachineId = 229; + VirtualMachine virtualMachine = VirtualMachine.builder(). + id(virtualMachineId).state(state).build(); + + expect(virtualMachineClient.getVirtualMachine(virtualMachineId)).andReturn(virtualMachine); + replay(client, virtualMachineClient); + + assertEquals(new VirtualMachineRunning(client).apply(virtualMachine), expected); + verify(client, virtualMachineClient); + } +} diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/ZonePredicatesTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/ZonePredicatesTest.java new file mode 100644 index 0000000000..048652179b --- /dev/null +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/predicates/ZonePredicatesTest.java @@ -0,0 +1,38 @@ +package org.jclouds.cloudstack.predicates; + +import org.jclouds.cloudstack.domain.NetworkType; +import org.jclouds.cloudstack.domain.Zone; +import org.testng.annotations.Test; + +import static org.jclouds.cloudstack.predicates.ZonePredicates.supportsAdvancedNetworks; +import static org.jclouds.cloudstack.predicates.ZonePredicates.supportsSecurityGroups; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + +/** + * @author Andrei Savu + */ +@Test(groups = "unit") +public class ZonePredicatesTest { + + @Test + public void testSupportsAdvancedNetworks() { + assertTrue(supportsAdvancedNetworks().apply( + Zone.builder().networkType(NetworkType.ADVANCED).build() + )); + assertFalse(supportsAdvancedNetworks().apply( + Zone.builder().networkType(NetworkType.BASIC).build() + )); + } + + @Test + public void testSupportsSecurityGroups() { + assertTrue(supportsSecurityGroups().apply( + Zone.builder().securityGroupsEnabled(true).build() + )); + assertFalse(supportsSecurityGroups().apply( + Zone.builder().securityGroupsEnabled(false).build() + )); + } + +}