You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by na...@apache.org on 2014/05/13 10:20:27 UTC

git commit: JCLOUDS-544: Add missing ERROR value to the Event.Status enum

Repository: jclouds-labs
Updated Branches:
  refs/heads/master 96a5d13b3 -> e255b82ce


JCLOUDS-544: Add missing ERROR value to the Event.Status enum


Project: http://git-wip-us.apache.org/repos/asf/jclouds-labs/repo
Commit: http://git-wip-us.apache.org/repos/asf/jclouds-labs/commit/e255b82c
Tree: http://git-wip-us.apache.org/repos/asf/jclouds-labs/tree/e255b82c
Diff: http://git-wip-us.apache.org/repos/asf/jclouds-labs/diff/e255b82c

Branch: refs/heads/master
Commit: e255b82ce80f1abb0cf5440498b70692982e2612
Parents: 96a5d13
Author: Ignasi Barrera <na...@apache.org>
Authored: Wed May 7 15:07:00 2014 +0200
Committer: Ignasi Barrera <na...@apache.org>
Committed: Tue May 13 10:12:47 2014 +0200

----------------------------------------------------------------------
 digitalocean/pom.xml                            |  2 +-
 ...DigitalOceanComputeServiceContextModule.java | 61 ++++++++-------
 .../org/jclouds/digitalocean/domain/Event.java  |  2 +-
 .../compute/config/EventDonePredicateTest.java  | 78 ++++++++++++++++++++
 4 files changed, 114 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/e255b82c/digitalocean/pom.xml
----------------------------------------------------------------------
diff --git a/digitalocean/pom.xml b/digitalocean/pom.xml
index e1d06e2..92f77b3 100644
--- a/digitalocean/pom.xml
+++ b/digitalocean/pom.xml
@@ -39,7 +39,7 @@
     <test.digitalocean.identity>FIXME</test.digitalocean.identity>
     <test.digitalocean.credential>FIXME</test.digitalocean.credential>
     <!-- CentOS 6.5 x64 -->
-    <test.digitalocean.template>imageId=1646467</test.digitalocean.template>
+    <test.digitalocean.template>imageId=3448641</test.digitalocean.template>
     <jclouds.osgi.export>org.jclouds.digitalocean*;version="${jclouds.version}"</jclouds.osgi.export>
     <jclouds.osgi.import>
       org.jclouds.compute.internal;version="${jclouds.version}",

http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/e255b82c/digitalocean/src/main/java/org/jclouds/digitalocean/compute/config/DigitalOceanComputeServiceContextModule.java
----------------------------------------------------------------------
diff --git a/digitalocean/src/main/java/org/jclouds/digitalocean/compute/config/DigitalOceanComputeServiceContextModule.java b/digitalocean/src/main/java/org/jclouds/digitalocean/compute/config/DigitalOceanComputeServiceContextModule.java
index 67837f5..8c9d6df 100644
--- a/digitalocean/src/main/java/org/jclouds/digitalocean/compute/config/DigitalOceanComputeServiceContextModule.java
+++ b/digitalocean/src/main/java/org/jclouds/digitalocean/compute/config/DigitalOceanComputeServiceContextModule.java
@@ -16,6 +16,7 @@
  */
 package org.jclouds.digitalocean.compute.config;
 
+import static com.google.common.base.Preconditions.checkNotNull;
 import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_IMAGE_AVAILABLE;
 import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_RUNNING;
 import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_SUSPENDED;
@@ -52,6 +53,7 @@ import org.jclouds.digitalocean.domain.Size;
 import org.jclouds.domain.Location;
 import org.jclouds.util.Predicates2;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.base.Predicate;
@@ -106,13 +108,8 @@ public class DigitalOceanComputeServiceContextModule extends
    @Named(TIMEOUT_NODE_RUNNING)
    protected Predicate<Integer> provideDropletRunningPredicate(final DigitalOceanApi api, Timeouts timeouts,
          PollPeriod pollPeriod) {
-      return Predicates2.retry(new Predicate<Integer>() {
-         @Override
-         public boolean apply(Integer input) {
-            Event event = api.getEventApi().get(input);
-            return Event.Status.DONE == event.getStatus();
-         }
-      }, timeouts.nodeRunning, pollPeriod.pollInitialPeriod, pollPeriod.pollMaxPeriod);
+      return Predicates2.retry(new EventDonePredicate(api), timeouts.nodeRunning, pollPeriod.pollInitialPeriod,
+            pollPeriod.pollMaxPeriod);
    }
 
    @Provides
@@ -120,13 +117,8 @@ public class DigitalOceanComputeServiceContextModule extends
    @Named(TIMEOUT_NODE_SUSPENDED)
    protected Predicate<Integer> provideDropletSuspendedPredicate(final DigitalOceanApi api, Timeouts timeouts,
          PollPeriod pollPeriod) {
-      return Predicates2.retry(new Predicate<Integer>() {
-         @Override
-         public boolean apply(Integer input) {
-            Event event = api.getEventApi().get(input);
-            return Event.Status.DONE == event.getStatus();
-         }
-      }, timeouts.nodeSuspended, pollPeriod.pollInitialPeriod, pollPeriod.pollMaxPeriod);
+      return Predicates2.retry(new EventDonePredicate(api), timeouts.nodeSuspended, pollPeriod.pollInitialPeriod,
+            pollPeriod.pollMaxPeriod);
    }
 
    @Provides
@@ -134,13 +126,8 @@ public class DigitalOceanComputeServiceContextModule extends
    @Named(TIMEOUT_NODE_TERMINATED)
    protected Predicate<Integer> provideDropletTerminatedPredicate(final DigitalOceanApi api, Timeouts timeouts,
          PollPeriod pollPeriod) {
-      return Predicates2.retry(new Predicate<Integer>() {
-         @Override
-         public boolean apply(Integer input) {
-            Event event = api.getEventApi().get(input);
-            return Event.Status.DONE == event.getStatus();
-         }
-      }, timeouts.nodeTerminated, pollPeriod.pollInitialPeriod, pollPeriod.pollMaxPeriod);
+      return Predicates2.retry(new EventDonePredicate(api), timeouts.nodeTerminated, pollPeriod.pollInitialPeriod,
+            pollPeriod.pollMaxPeriod);
    }
 
    @Provides
@@ -148,12 +135,32 @@ public class DigitalOceanComputeServiceContextModule extends
    @Named(TIMEOUT_IMAGE_AVAILABLE)
    protected Predicate<Integer> provideImageAvailablePredicate(final DigitalOceanApi api, Timeouts timeouts,
          PollPeriod pollPeriod) {
-      return Predicates2.retry(new Predicate<Integer>() {
-         @Override
-         public boolean apply(Integer input) {
-            Event event = api.getEventApi().get(input);
-            return Event.Status.DONE == event.getStatus();
+      return Predicates2.retry(new EventDonePredicate(api), timeouts.imageAvailable, pollPeriod.pollInitialPeriod,
+            pollPeriod.pollMaxPeriod);
+   }
+
+   @VisibleForTesting
+   static class EventDonePredicate implements Predicate<Integer> {
+
+      private final DigitalOceanApi api;
+
+      public EventDonePredicate(DigitalOceanApi api) {
+         this.api = checkNotNull(api, "api must not be null");
+      }
+
+      @Override
+      public boolean apply(Integer input) {
+         Event event = api.getEventApi().get(input);
+         switch (event.getStatus()) {
+            case DONE:
+               return true;
+            case PENDING:
+               return false;
+            case ERROR:
+            default:
+               throw new IllegalStateException("Resource is in invalid status: " + event.getStatus().name());
          }
-      }, timeouts.imageAvailable, pollPeriod.pollInitialPeriod, pollPeriod.pollMaxPeriod);
+      }
+
    }
 }

http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/e255b82c/digitalocean/src/main/java/org/jclouds/digitalocean/domain/Event.java
----------------------------------------------------------------------
diff --git a/digitalocean/src/main/java/org/jclouds/digitalocean/domain/Event.java b/digitalocean/src/main/java/org/jclouds/digitalocean/domain/Event.java
index 6b39da1..1aea9f8 100644
--- a/digitalocean/src/main/java/org/jclouds/digitalocean/domain/Event.java
+++ b/digitalocean/src/main/java/org/jclouds/digitalocean/domain/Event.java
@@ -37,7 +37,7 @@ import com.google.inject.name.Named;
 public class Event {
 
    public enum Status {
-      DONE, PENDING;
+      DONE, PENDING, ERROR;
 
       public static Status fromValue(String value) {
          // DigitalOcean return a 'null' status when the operation is still in

http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/e255b82c/digitalocean/src/test/java/org/jclouds/digitalocean/compute/config/EventDonePredicateTest.java
----------------------------------------------------------------------
diff --git a/digitalocean/src/test/java/org/jclouds/digitalocean/compute/config/EventDonePredicateTest.java b/digitalocean/src/test/java/org/jclouds/digitalocean/compute/config/EventDonePredicateTest.java
new file mode 100644
index 0000000..4c47488
--- /dev/null
+++ b/digitalocean/src/test/java/org/jclouds/digitalocean/compute/config/EventDonePredicateTest.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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.digitalocean.compute.config;
+
+import static org.easymock.EasyMock.anyInt;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+
+import org.easymock.EasyMock;
+import org.jclouds.digitalocean.DigitalOceanApi;
+import org.jclouds.digitalocean.compute.config.DigitalOceanComputeServiceContextModule.EventDonePredicate;
+import org.jclouds.digitalocean.domain.Event;
+import org.jclouds.digitalocean.domain.Event.Status;
+import org.jclouds.digitalocean.features.EventApi;
+import org.testng.annotations.Test;
+
+/**
+ * Unit tests for the {@link EventDonePredicate} class.
+ * 
+ * @author Ignasi Barrera
+ */
+@Test(groups = "unit", testName = "EventDonePredicateTest")
+public class EventDonePredicateTest {
+
+   public void testEventProgress() {
+      EventApi eventApi = EasyMock.createMock(EventApi.class);
+      DigitalOceanApi api = EasyMock.createMock(DigitalOceanApi.class);
+
+      expect(eventApi.get(1)).andReturn(event(Status.DONE));
+      expect(eventApi.get(2)).andReturn(event(Status.PENDING));
+      expect(api.getEventApi()).andReturn(eventApi).times(2);
+      replay(eventApi, api);
+
+      EventDonePredicate predicate = new EventDonePredicate(api);
+      assertTrue(predicate.apply(1));
+      assertFalse(predicate.apply(2));
+   }
+
+   public void testEventFailed() {
+      EventApi eventApi = EasyMock.createMock(EventApi.class);
+      DigitalOceanApi api = EasyMock.createMock(DigitalOceanApi.class);
+
+      expect(eventApi.get(anyInt())).andReturn(event(Status.ERROR));
+      expect(api.getEventApi()).andReturn(eventApi);
+      replay(eventApi, api);
+
+      EventDonePredicate predicate = new EventDonePredicate(api);
+
+      try {
+         predicate.apply(1);
+         fail("Method should have thrown an IllegalStateException");
+      } catch (IllegalStateException ex) {
+         assertEquals(ex.getMessage(), "Resource is in invalid status: ERROR");
+      }
+   }
+
+   private static Event event(Status status) {
+      return new Event(0, status, 0, "0", 0);
+   }
+}