From 890f377908b339b238fcde56bb5b93ee0e40fb27 Mon Sep 17 00:00:00 2001 From: Jason King Date: Wed, 21 Dec 2011 17:29:00 +0000 Subject: [PATCH] Issue 695: Added Predicate to test for a task being complete or sucessful --- .../predicates/TaskPredicates.java | 54 ++++++++++++++ .../predicates/TaskPredicatesTest.java | 73 +++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/predicates/TaskPredicates.java create mode 100644 sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/predicates/TaskPredicatesTest.java diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/predicates/TaskPredicates.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/predicates/TaskPredicates.java new file mode 100644 index 0000000000..a00ad490f2 --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/predicates/TaskPredicates.java @@ -0,0 +1,54 @@ +/** + * 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.tmrk.enterprisecloud.predicates; + +import com.google.common.base.Predicate; +import org.jclouds.tmrk.enterprisecloud.domain.Task; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * Predicates relating to a task + * @author Jason King + */ +public class TaskPredicates { + + public static Predicate completeOrSuccess() { + return new Predicate() { + @Override + public boolean apply(Task task) { + checkNotNull(task,"task cannot be null"); + switch(task.getStatus()) { + case QUEUED: + case RUNNING: + return false; + case COMPLETE: + case SUCCESS: + return true; + default: + throw new RuntimeException("Task Failed:"+task.getHref()+", Status:"+task.getStatus()); + } + } + @Override + public String toString() { + return "completeOrSuccess"; + } + }; + } +} diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/predicates/TaskPredicatesTest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/predicates/TaskPredicatesTest.java new file mode 100644 index 0000000000..8766391f65 --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/predicates/TaskPredicatesTest.java @@ -0,0 +1,73 @@ +/** + * 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.tmrk.enterprisecloud.predicates; + +import org.jclouds.tmrk.enterprisecloud.domain.NamedResource; +import org.jclouds.tmrk.enterprisecloud.domain.Task; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.net.URI; +import java.util.Date; + +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; +import static org.jclouds.tmrk.enterprisecloud.predicates.TaskPredicates.completeOrSuccess; + +/** + * @author Jason King + */ +@Test(groups = "unit", testName = "TaskPredicatesTest") +public class TaskPredicatesTest { + + private Task task; + + @BeforeMethod + public void setUp() { + task = Task.builder().href(URI.create("")).name("test") + .operation("no-op") + .impactedItem(NamedResource.builder().href(URI.create("")).build()) + .startTime(new Date()) + .initiatedBy(NamedResource.builder().href(URI.create("")).build()) + .status(Task.Status.UNRECOGNIZED) + .build(); + } + + public void testCompleteOrSuccess() { + assertTrue(completeOrSuccess().apply(task.toBuilder().status(Task.Status.COMPLETE).build())); + assertTrue(completeOrSuccess().apply(task.toBuilder().status(Task.Status.SUCCESS).build())); + assertFalse(completeOrSuccess().apply(task.toBuilder().status(Task.Status.RUNNING).build())); + assertFalse(completeOrSuccess().apply(task.toBuilder().status(Task.Status.QUEUED).build())); + } + + @Test(expectedExceptions = NullPointerException.class) + public void testCompleteOrSuccessWhenNull() { + TaskPredicates.completeOrSuccess().apply(null); + } + + @Test(expectedExceptions = RuntimeException.class) + public void testCompleteOrSuccessWhenFailure() { + TaskPredicates.completeOrSuccess().apply(task.toBuilder().status(Task.Status.FAILED).build()); + } + + @Test(expectedExceptions = RuntimeException.class) + public void testCompleteOrSuccessWhenError() { + TaskPredicates.completeOrSuccess().apply(task.toBuilder().status(Task.Status.ERROR).build()); + } +}