Added more tests for cloudstack predicates

This commit is contained in:
andreisavu 2011-11-29 17:39:24 +02:00
parent 5762f0f851
commit 6afe98304c
9 changed files with 420 additions and 13 deletions

View File

@ -60,14 +60,7 @@ public class UserPredicates {
/** /**
* *
* @return true, if the account has user privileges * @return true, if the user's account type is the following
*/
public static Predicate<User> isUserAccount() {
return accountTypeEquals(Account.Type.USER);
}
/**
*
* @return true, if the user's apiKey is the following
*/ */
public static Predicate<User> accountTypeEquals(Account.Type type) { public static Predicate<User> accountTypeEquals(Account.Type type) {
return new AccountTypeEquals(type); return new AccountTypeEquals(type);
@ -90,12 +83,27 @@ public class UserPredicates {
return "accountTypeEquals(" + type + ")"; return "accountTypeEquals(" + type + ")";
} }
} }
/**
*
* @return true, if the account has user privileges
*/
public static Predicate<User> isUserAccount() {
return accountTypeEquals(Account.Type.USER);
}
/**
* @return true, is the user is a domain admin
*/
public static Predicate<User> 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<User> isAdminAccount() { public static Predicate<User> isAdminAccount() {
return accountTypeEquals(Account.Type.ADMIN); return accountTypeEquals(Account.Type.ADMIN);
} }
} }

View File

@ -39,13 +39,13 @@ public class ZonePredicates {
return new Predicate<Zone>() { return new Predicate<Zone>() {
@Override @Override
public boolean apply(Zone input) { public boolean apply(Zone zone) {
return NetworkType.ADVANCED.equals(checkNotNull(input, "zone").getNetworkType()); return NetworkType.ADVANCED.equals(checkNotNull(zone, "zone").getNetworkType());
} }
@Override @Override
public String toString() { public String toString() {
return "supportsAvancedNetworks()"; return "supportsAdvancedNetworks()";
} }
}; };
} }

View File

@ -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);
}
}

View File

@ -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<String> acceptableCategories = ImmutableSet.<String>of("Ubuntu");
@BeforeMethod
public void setUp() {
client = createMock(CloudStackClient.class);
guestOSClient = createMock(GuestOSClient.class);
expect(client.getGuestOSClient()).andReturn(guestOSClient).times(2);
Map<Long, String> osCategories = Maps.newHashMap();
osCategories.put(1L, "Ubuntu");
osCategories.put(2L, "CentOS");
osCategories.put(3L, "RHEL");
expect(guestOSClient.listOSCategories()).andReturn(osCategories);
Set<OSType> 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);
}
}

View File

@ -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()
));
}
}

View File

@ -0,0 +1,38 @@
package org.jclouds.cloudstack.predicates;
import org.jclouds.cloudstack.domain.Account;
import org.jclouds.cloudstack.domain.User;
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.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()
));
}
@Test
public void testIsUserAccount() {
User adminUser = User.builder().accountType(Account.Type.ADMIN).build();
assertFalse(isUserAccount().apply(adminUser));
assertFalse(isDomainAdminAccount().apply(adminUser));
assertTrue(isAdminAccount().apply(adminUser));
}
}

View File

@ -0,0 +1,57 @@
/**
* 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.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", testName = "VirtualMachineDestroyedTest")
public class VirtualMachineDestroyedTest {
@Test
public void testWaitForVirtualMachineToBeDestroyed() {
CloudStackClient client = createMock(CloudStackClient.class);
VirtualMachineClient virtualMachineClient = createMock(VirtualMachineClient.class);
long virtualMachineId = 229;
VirtualMachine virtualMachine = VirtualMachine.builder().
id(virtualMachineId).state(VirtualMachine.State.DESTROYED).build();
expect(client.getVirtualMachineClient()).andReturn(virtualMachineClient);
expect(virtualMachineClient.getVirtualMachine(virtualMachineId)).andReturn(virtualMachine);
replay(client, virtualMachineClient);
assertTrue(new VirtualMachineDestroyed(client).apply(virtualMachine));
verify(client, virtualMachineClient);
}
}

View File

@ -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);
}
}

View File

@ -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()
));
}
}