You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by ra...@apache.org on 2014/03/02 12:29:47 UTC

git commit: starting to add some tests and fix what arises

Repository: curator
Updated Branches:
  refs/heads/CURATOR-88 1de24127e -> 0313b6fce


starting to add some tests and fix what arises


Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/0313b6fc
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/0313b6fc
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/0313b6fc

Branch: refs/heads/CURATOR-88
Commit: 0313b6fce1b3ecf1fa44b8474c4d9cd3472fe252
Parents: 1de2412
Author: randgalt <ra...@apache.org>
Authored: Sun Mar 2 16:59:38 2014 +0530
Committer: randgalt <ra...@apache.org>
Committed: Sun Mar 2 16:59:38 2014 +0530

----------------------------------------------------------------------
 curator-x-discovery-server/pom.xml              |   3 +-
 curator-x-discovery/pom.xml                     |   1 -
 curator-x-rest/pom.xml                          |  27 ++++
 .../curator/x/rest/api/ClientResource.java      |   8 +-
 .../org/apache/curator/x/rest/api/Session.java  |   2 +-
 .../x/rest/dropwizard/CuratorApplication.java   |  24 ++--
 .../apache/curator/x/rest/entities/Status.java  |  62 ++++++++++
 .../src/site/confluence/entities.confluence     |   2 +-
 .../apache/curator/x/rest/api/TestClient.java   | 112 +++++++++++++++++
 .../x/rest/support/BaseClassForTests.java       | 123 +++++++++++++++++++
 .../x/rest/support/InterProcessLockBridge.java  |  24 ++++
 11 files changed, 370 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/0313b6fc/curator-x-discovery-server/pom.xml
----------------------------------------------------------------------
diff --git a/curator-x-discovery-server/pom.xml b/curator-x-discovery-server/pom.xml
index a39c04d..be7993c 100644
--- a/curator-x-discovery-server/pom.xml
+++ b/curator-x-discovery-server/pom.xml
@@ -27,7 +27,6 @@
         <version>2.4.1-SNAPSHOT</version>
     </parent>
 
-    <groupId>org.apache.curator</groupId>
     <artifactId>curator-x-discovery-server</artifactId>
     <version>2.4.1-SNAPSHOT</version>
     <packaging>bundle</packaging>
@@ -37,7 +36,7 @@
     <inceptionYear>2011</inceptionYear>
 
     <properties>
-        <jersey.version>1.11</jersey.version>
+        <jersey.version>1.18</jersey.version>
         <osgi.import.package>
             *
         </osgi.import.package>

http://git-wip-us.apache.org/repos/asf/curator/blob/0313b6fc/curator-x-discovery/pom.xml
----------------------------------------------------------------------
diff --git a/curator-x-discovery/pom.xml b/curator-x-discovery/pom.xml
index 9ff2280..b9936c3 100644
--- a/curator-x-discovery/pom.xml
+++ b/curator-x-discovery/pom.xml
@@ -27,7 +27,6 @@
         <version>2.4.1-SNAPSHOT</version>
     </parent>
 
-    <groupId>org.apache.curator</groupId>
     <artifactId>curator-x-discovery</artifactId>
     <version>2.4.1-SNAPSHOT</version>
     <packaging>bundle</packaging>

http://git-wip-us.apache.org/repos/asf/curator/blob/0313b6fc/curator-x-rest/pom.xml
----------------------------------------------------------------------
diff --git a/curator-x-rest/pom.xml b/curator-x-rest/pom.xml
index 07b2b6e..affe441 100644
--- a/curator-x-rest/pom.xml
+++ b/curator-x-rest/pom.xml
@@ -30,6 +30,7 @@
 
     <properties>
         <dropwizard-version>0.7.0-rc1</dropwizard-version>
+        <jersey-version>1.18</jersey-version> <!-- must match Dropwizard jersey version -->
     </properties>
 
     <dependencies>
@@ -60,5 +61,31 @@
             <version>${dropwizard-version}</version>
             <scope>provided</scope>
         </dependency>
+
+        <dependency>
+            <groupId>org.apache.curator</groupId>
+            <artifactId>curator-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>com.sun.jersey</groupId>
+            <artifactId>jersey-core</artifactId>
+            <version>${jersey-version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.sun.jersey</groupId>
+            <artifactId>jersey-client</artifactId>
+            <version>${jersey-version}</version>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>com.sun.jersey</groupId>
+            <artifactId>jersey-json</artifactId>
+            <version>${jersey-version}</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>

http://git-wip-us.apache.org/repos/asf/curator/blob/0313b6fc/curator-x-rest/src/main/java/org/apache/curator/x/rest/api/ClientResource.java
----------------------------------------------------------------------
diff --git a/curator-x-rest/src/main/java/org/apache/curator/x/rest/api/ClientResource.java b/curator-x-rest/src/main/java/org/apache/curator/x/rest/api/ClientResource.java
index 0b19e35..b490427 100644
--- a/curator-x-rest/src/main/java/org/apache/curator/x/rest/api/ClientResource.java
+++ b/curator-x-rest/src/main/java/org/apache/curator/x/rest/api/ClientResource.java
@@ -30,6 +30,7 @@ import org.apache.curator.x.rest.entities.GetChildrenSpec;
 import org.apache.curator.x.rest.entities.GetDataSpec;
 import org.apache.curator.x.rest.entities.PathAndId;
 import org.apache.curator.x.rest.entities.SetDataSpec;
+import org.apache.curator.x.rest.entities.Status;
 import org.apache.zookeeper.CreateMode;
 import org.apache.zookeeper.data.Stat;
 import org.codehaus.jackson.node.ObjectNode;
@@ -39,7 +40,6 @@ import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
-import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 import javax.ws.rs.WebApplicationException;
@@ -74,16 +74,14 @@ public class ClientResource
     @Path("/status")
     public Response getStatusWithTouch(List<String> ids) throws IOException
     {
-        ObjectNode node = context.getMapper().createObjectNode();
-        node.put("state", context.getConnectionState().name().toLowerCase());
-        node.putPOJO("messages", context.getSession().drainMessages());
+        Status status = new Status(context.getConnectionState().name().toLowerCase(), context.getSession().drainMessages());
 
         for ( String id : ids )
         {
             context.getSession().updateThingLastUse(id);
         }
 
-        return Response.ok(context.getWriter().writeValueAsString(node)).build();
+        return Response.ok(status).build();
     }
 
     @POST

http://git-wip-us.apache.org/repos/asf/curator/blob/0313b6fc/curator-x-rest/src/main/java/org/apache/curator/x/rest/api/Session.java
----------------------------------------------------------------------
diff --git a/curator-x-rest/src/main/java/org/apache/curator/x/rest/api/Session.java b/curator-x-rest/src/main/java/org/apache/curator/x/rest/api/Session.java
index f8bf80d..cbc2211 100644
--- a/curator-x-rest/src/main/java/org/apache/curator/x/rest/api/Session.java
+++ b/curator-x-rest/src/main/java/org/apache/curator/x/rest/api/Session.java
@@ -93,7 +93,7 @@ public class Session implements Closeable
         messages.add(message);
     }
 
-    Collection<StatusMessage> drainMessages()
+    List<StatusMessage> drainMessages()
     {
         List<StatusMessage> localMessages = Lists.newArrayList();
         messages.drainTo(localMessages);

http://git-wip-us.apache.org/repos/asf/curator/blob/0313b6fc/curator-x-rest/src/main/java/org/apache/curator/x/rest/dropwizard/CuratorApplication.java
----------------------------------------------------------------------
diff --git a/curator-x-rest/src/main/java/org/apache/curator/x/rest/dropwizard/CuratorApplication.java b/curator-x-rest/src/main/java/org/apache/curator/x/rest/dropwizard/CuratorApplication.java
index 539097d..385d081 100644
--- a/curator-x-rest/src/main/java/org/apache/curator/x/rest/dropwizard/CuratorApplication.java
+++ b/curator-x-rest/src/main/java/org/apache/curator/x/rest/dropwizard/CuratorApplication.java
@@ -27,7 +27,22 @@ public class CuratorApplication
 {
     public static void main(String[] args) throws Exception
     {
-        Application application = new Application<CuratorConfiguration>()
+        run(build(), args);
+    }
+
+    public static void run(Application<CuratorConfiguration> application, String[] args) throws Exception
+    {
+        if ( (args == null) || (args.length == 0) )
+        {
+            args = new String[]{"server"};
+        }
+
+        application.run(args);
+    }
+
+    public static Application<CuratorConfiguration> build()
+    {
+        return new Application<CuratorConfiguration>()
         {
             @Override
             public void initialize(Bootstrap<CuratorConfiguration> bootstrap)
@@ -41,12 +56,5 @@ public class CuratorApplication
                 // NOP
             }
         };
-
-        if ( args.length == 0 )
-        {
-            args = new String[]{"server"};
-        }
-
-        application.run(args);
     }
 }

http://git-wip-us.apache.org/repos/asf/curator/blob/0313b6fc/curator-x-rest/src/main/java/org/apache/curator/x/rest/entities/Status.java
----------------------------------------------------------------------
diff --git a/curator-x-rest/src/main/java/org/apache/curator/x/rest/entities/Status.java b/curator-x-rest/src/main/java/org/apache/curator/x/rest/entities/Status.java
new file mode 100644
index 0000000..f51aa0f
--- /dev/null
+++ b/curator-x-rest/src/main/java/org/apache/curator/x/rest/entities/Status.java
@@ -0,0 +1,62 @@
+/**
+ * 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.apache.curator.x.rest.entities;
+
+import com.google.common.collect.Lists;
+import javax.xml.bind.annotation.XmlRootElement;
+import java.util.List;
+
+@XmlRootElement
+public class Status
+{
+    private String state;
+    private List<StatusMessage> messages;
+
+    public Status()
+    {
+        this("", Lists.<StatusMessage>newArrayList());
+    }
+
+    public Status(String state, List<StatusMessage> messages)
+    {
+        this.state = state;
+        this.messages = messages;
+    }
+
+    public String getState()
+    {
+        return state;
+    }
+
+    public void setState(String state)
+    {
+        this.state = state;
+    }
+
+    public List<StatusMessage> getMessages()
+    {
+        return messages;
+    }
+
+    public void setMessages(List<StatusMessage> messages)
+    {
+        this.messages = messages;
+    }
+}

http://git-wip-us.apache.org/repos/asf/curator/blob/0313b6fc/curator-x-rest/src/site/confluence/entities.confluence
----------------------------------------------------------------------
diff --git a/curator-x-rest/src/site/confluence/entities.confluence b/curator-x-rest/src/site/confluence/entities.confluence
index 0537bde..b8f686b 100644
--- a/curator-x-rest/src/site/confluence/entities.confluence
+++ b/curator-x-rest/src/site/confluence/entities.confluence
@@ -7,7 +7,7 @@ Here are the entity descriptions for the entities used in the APIs:
 ||Field||Type||Description||
 |*Status*| | |
 |state|string|This instance's Curator connection state. One of: "connected", "suspended", or "lost". If the state is other than "connected" you must assume that any open locks and/or watchers are no longer valid.|
-|messages|array of StatusMessages|Any pending messages from this instance.|
+|messages|array of StatusMessage|Any pending messages from this instance.|
 | | | |
 |*StatusMessage*| | |
 |type|string|The status message type. See the [[Managing Status|client.html]] section for details.|

http://git-wip-us.apache.org/repos/asf/curator/blob/0313b6fc/curator-x-rest/src/test/java/org/apache/curator/x/rest/api/TestClient.java
----------------------------------------------------------------------
diff --git a/curator-x-rest/src/test/java/org/apache/curator/x/rest/api/TestClient.java b/curator-x-rest/src/test/java/org/apache/curator/x/rest/api/TestClient.java
new file mode 100644
index 0000000..b839603
--- /dev/null
+++ b/curator-x-rest/src/test/java/org/apache/curator/x/rest/api/TestClient.java
@@ -0,0 +1,112 @@
+/**
+ * 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.apache.curator.x.rest.api;
+
+import ch.qos.logback.core.util.CloseUtil;
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.curator.framework.CuratorFrameworkFactory;
+import org.apache.curator.retry.RetryOneTime;
+import org.apache.curator.test.Timing;
+import org.apache.curator.x.rest.entities.ExistsSpec;
+import org.apache.curator.x.rest.entities.Status;
+import org.apache.curator.x.rest.entities.StatusMessage;
+import org.apache.curator.x.rest.support.BaseClassForTests;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.UriBuilder;
+import java.net.URI;
+
+public class TestClient extends BaseClassForTests
+{
+    @Test
+    public void testStatus() throws Exception
+    {
+        boolean connected = false;
+        for ( int i = 0; i < 10; ++i )
+        {
+            Status status = restClient.resource(getStatusUri()).get(Status.class);
+            if ( status.getState().equals("connected") )
+            {
+                connected = true;
+                break;
+            }
+            Thread.sleep(1000);
+        }
+        Assert.assertTrue(connected);
+    }
+
+    @Test
+    public void testWatcher() throws Exception
+    {
+        final String path = "/a/path/to/a/node";
+        final String watchId = "test-watcher";
+
+        ExistsSpec existsSpec = new ExistsSpec();
+        existsSpec.setPath(path);
+        existsSpec.setWatched(true);
+        existsSpec.setWatchId(watchId);
+        URI uri = UriBuilder.fromUri("http://localhost:" + PORT).path(ClientResource.class).path(ClientResource.class, "exists").build();
+        restClient.resource(uri).type(MediaType.APPLICATION_JSON).post(existsSpec);
+
+        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
+        try
+        {
+            client.start();
+            client.create().creatingParentsIfNeeded().forPath(path);
+        }
+        finally
+        {
+            CloseUtil.closeQuietly(client);
+        }
+
+        new Timing().sleepABit();
+
+        Status status = restClient.resource(getStatusUri()).get(Status.class);
+        boolean foundWatch = false;
+        boolean foundWatchId = false;
+        boolean foundMessage = false;
+        for ( StatusMessage message : status.getMessages() )
+        {
+            if ( message.getType().equals("watch") )
+            {
+                foundWatch = true;
+                if ( message.getSourceId().equals(watchId) )
+                {
+                    foundWatchId = true;
+                    if ( message.getMessage().equals("NodeCreated") )
+                    {
+                        foundMessage = true;
+                    }
+                }
+                break;
+            }
+        }
+
+        Assert.assertTrue(foundWatch);
+        Assert.assertTrue(foundWatchId);
+        Assert.assertTrue(foundMessage);
+    }
+
+    private URI getStatusUri()
+    {
+        return UriBuilder.fromUri("http://localhost:" + PORT).path(ClientResource.class).path(ClientResource.class, "getStatus").build();
+    }
+}

http://git-wip-us.apache.org/repos/asf/curator/blob/0313b6fc/curator-x-rest/src/test/java/org/apache/curator/x/rest/support/BaseClassForTests.java
----------------------------------------------------------------------
diff --git a/curator-x-rest/src/test/java/org/apache/curator/x/rest/support/BaseClassForTests.java b/curator-x-rest/src/test/java/org/apache/curator/x/rest/support/BaseClassForTests.java
new file mode 100644
index 0000000..c9d4cb7
--- /dev/null
+++ b/curator-x-rest/src/test/java/org/apache/curator/x/rest/support/BaseClassForTests.java
@@ -0,0 +1,123 @@
+/**
+ * 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.apache.curator.x.rest.support;
+
+import com.google.common.io.CharStreams;
+import com.google.common.io.Files;
+import com.sun.jersey.api.client.Client;
+import io.dropwizard.Application;
+import io.dropwizard.setup.Bootstrap;
+import io.dropwizard.setup.Environment;
+import org.apache.curator.test.TestingServer;
+import org.apache.curator.utils.DebugUtils;
+import org.apache.curator.x.rest.dropwizard.CuratorApplication;
+import org.apache.curator.x.rest.dropwizard.CuratorConfiguration;
+import org.apache.curator.x.rest.dropwizard.CuratorRestBundle;
+import org.eclipse.jetty.util.component.AbstractLifeCycle;
+import org.eclipse.jetty.util.component.LifeCycle;
+import org.eclipse.jetty.util.thread.ShutdownThread;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import java.io.File;
+import java.nio.charset.Charset;
+import java.util.concurrent.Callable;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Executors;
+
+public class BaseClassForTests
+{
+    protected TestingServer server;
+    protected Application<CuratorConfiguration> application;
+    protected Client restClient;
+
+    protected static final int PORT = 8080;
+
+    private File configFile;
+
+    @BeforeMethod
+    public void     setup() throws Exception
+    {
+        restClient = Client.create();
+
+        System.setProperty(DebugUtils.PROPERTY_DONT_LOG_CONNECTION_ISSUES, "true");
+        server = new TestingServer();
+
+        configFile = File.createTempFile("temp", ".tmp");
+        CharStreams.write("{\"connection-string\": \"" + server.getConnectString() + "\"}", Files.newWriterSupplier(configFile, Charset.defaultCharset()));
+
+        final CountDownLatch startedLatch = new CountDownLatch(1);
+        application = new Application<CuratorConfiguration>()
+        {
+            @Override
+            public void initialize(Bootstrap<CuratorConfiguration> bootstrap)
+            {
+                bootstrap.addBundle(new CuratorRestBundle());
+            }
+
+            @Override
+            public void run(CuratorConfiguration configuration, Environment environment) throws Exception
+            {
+                LifeCycle.Listener listener = new AbstractLifeCycle.AbstractLifeCycleListener()
+                {
+                    @Override
+                    public void lifeCycleStarted(LifeCycle event)
+                    {
+                        startedLatch.countDown();
+                    }
+                };
+                environment.lifecycle().addLifeCycleListener(listener);
+            }
+        };
+
+        Executors.newSingleThreadExecutor().submit
+        (
+            new Callable<Void>()
+            {
+                @Override
+                public Void call() throws Exception
+                {
+                    String[] args = new String[]{"server", configFile.getPath()};
+                    CuratorApplication.run(application, args);
+                    return null;
+                }
+            }
+        );
+
+        startedLatch.await();
+    }
+
+    @AfterMethod
+    public void     teardown() throws Exception
+    {
+        if ( configFile != null )
+        {
+            //noinspection ResultOfMethodCallIgnored
+            configFile.delete();
+        }
+
+        if ( restClient != null )
+        {
+            restClient.destroy();
+        }
+
+        server.close();
+        ShutdownThread.getInstance().run();
+    }
+}

http://git-wip-us.apache.org/repos/asf/curator/blob/0313b6fc/curator-x-rest/src/test/java/org/apache/curator/x/rest/support/InterProcessLockBridge.java
----------------------------------------------------------------------
diff --git a/curator-x-rest/src/test/java/org/apache/curator/x/rest/support/InterProcessLockBridge.java b/curator-x-rest/src/test/java/org/apache/curator/x/rest/support/InterProcessLockBridge.java
new file mode 100644
index 0000000..7fdd8b3
--- /dev/null
+++ b/curator-x-rest/src/test/java/org/apache/curator/x/rest/support/InterProcessLockBridge.java
@@ -0,0 +1,24 @@
+/**
+ * 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.apache.curator.x.rest.support;
+
+public class InterProcessLockBridge
+{
+}