You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tajo.apache.org by ji...@apache.org on 2015/03/18 18:25:46 UTC

[07/13] tajo git commit: TAJO-1337: Implements common modules to handle RESTful API

http://git-wip-us.apache.org/repos/asf/tajo/blob/a9ae3cab/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/NettyRestHandlerContainerProviderTest.java
----------------------------------------------------------------------
diff --git a/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/NettyRestHandlerContainerProviderTest.java b/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/NettyRestHandlerContainerProviderTest.java
new file mode 100644
index 0000000..1511068
--- /dev/null
+++ b/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/NettyRestHandlerContainerProviderTest.java
@@ -0,0 +1,66 @@
+/**
+ * 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.tajo.ws.rs.netty;
+
+import io.netty.channel.ChannelHandler;
+import io.netty.channel.ChannelInboundHandler;
+import org.apache.tajo.ws.rs.netty.testapp1.TestApplication1;
+import org.glassfish.jersey.server.ApplicationHandler;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class NettyRestHandlerContainerProviderTest {
+
+  private NettyRestHandlerContainerProvider provider;
+  private ApplicationHandler applicationHandler;
+
+  @Before
+  public void setUp() throws Exception {
+    provider = new NettyRestHandlerContainerProvider();
+    applicationHandler = new ApplicationHandler(new TestApplication1());
+  }
+
+  @Test
+  public void testCreation() throws Exception {
+    ChannelHandler handler = provider.createContainer(ChannelHandler.class, applicationHandler);
+
+    assertNotNull(handler);
+
+    ChannelInboundHandler inboundHandler = provider.createContainer(ChannelInboundHandler.class, applicationHandler);
+
+    assertNotNull(inboundHandler);
+
+    NettyRestHandlerContainer container = provider.createContainer(NettyRestHandlerContainer.class, applicationHandler);
+
+    assertNotNull(container);
+  }
+
+  @Test
+  public void testNullCreation() throws Exception {
+    String stringValue = provider.createContainer(String.class, applicationHandler);
+
+    assertNull(stringValue);
+
+    Object objectValue = provider.createContainer(Object.class, applicationHandler);
+
+    assertNull(objectValue);
+  }
+}

http://git-wip-us.apache.org/repos/asf/tajo/blob/a9ae3cab/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/NettyRestServerTest.java
----------------------------------------------------------------------
diff --git a/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/NettyRestServerTest.java b/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/NettyRestServerTest.java
new file mode 100644
index 0000000..d8a57bf
--- /dev/null
+++ b/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/NettyRestServerTest.java
@@ -0,0 +1,137 @@
+/**
+ * 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.tajo.ws.rs.netty;
+
+import org.apache.tajo.ws.rs.netty.gson.GsonFeature;
+import org.apache.tajo.ws.rs.netty.testapp1.TestApplication1;
+import org.apache.tajo.ws.rs.netty.testapp1.TestResource1;
+import org.apache.tajo.ws.rs.netty.testapp2.Directory;
+import org.apache.tajo.ws.rs.netty.testapp2.FileManagementApplication;
+import org.glassfish.jersey.server.ResourceConfig;
+import org.junit.Test;
+
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.GenericType;
+import javax.ws.rs.core.MediaType;
+import java.net.InetSocketAddress;
+import java.net.ServerSocket;
+import java.net.URI;
+import java.util.Collection;
+
+import static org.junit.Assert.*;
+
+public class NettyRestServerTest {
+
+  @Test
+  public void testNettyRestServerCreation() throws Exception {
+    ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(TestApplication1.class);
+    ServerSocket serverSocket = new ServerSocket(0);
+    int availPort = serverSocket.getLocalPort();
+    serverSocket.close();
+    URI baseUri = new URI("http://localhost:"+availPort+"/rest");
+
+    NettyRestServer restServer = NettyRestServerFactory.createNettyRestServer(baseUri, resourceConfig, 3);
+
+    assertNotNull(restServer);
+    assertNotNull(restServer.getHandler());
+    assertNotNull(restServer.getChannel());
+    assertNotNull(restServer.getListenAddress());
+
+    InetSocketAddress listeningAddress = restServer.getListenAddress();
+
+    assertEquals(availPort, listeningAddress.getPort());
+  }
+
+  @Test
+  public void testTextPlainApplication() throws Exception {
+    ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(TestApplication1.class);
+    ServerSocket serverSocket = new ServerSocket(0);
+    int availPort = serverSocket.getLocalPort();
+    serverSocket.close();
+    URI baseUri = new URI("http://localhost:"+availPort+"/rest");
+
+    NettyRestServer restServer = NettyRestServerFactory.createNettyRestServer(baseUri, resourceConfig, 3);
+
+    try {
+      WebTarget webTarget = ClientBuilder.newClient().target(baseUri + "/testapp1");
+
+      assertEquals(TestResource1.outputMessage, webTarget.request(MediaType.TEXT_PLAIN).get(String.class));
+    } finally {
+      restServer.shutdown();
+    }
+  }
+
+  protected Directory createDirectory1() {
+    Directory newDirectory = new Directory();
+
+    newDirectory.setName("newdir1");
+    newDirectory.setOwner("owner1");
+    newDirectory.setGroup("group1");
+
+    return newDirectory;
+  }
+
+  @Test
+  public void testFileMgmtApplication() throws Exception {
+    ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(FileManagementApplication.class)
+        .register(GsonFeature.class);
+    ServerSocket serverSocket = new ServerSocket(0);
+    int availPort = serverSocket.getLocalPort();
+    serverSocket.close();
+    URI baseUri = new URI("http://localhost:"+availPort+"/rest");
+    URI directoriesUri = new URI(baseUri + "/directories");
+    Client restClient = ClientBuilder.newBuilder()
+        .register(GsonFeature.class).build();
+
+    NettyRestServer restServer = NettyRestServerFactory.createNettyRestServer(baseUri, resourceConfig, 3);
+
+    try {
+      Directory directory1 = createDirectory1();
+      Directory savedDirectory = restClient.target(directoriesUri)
+          .request().post(Entity.entity(directory1, MediaType.APPLICATION_JSON_TYPE), Directory.class);
+
+      assertNotNull(savedDirectory);
+      assertNotNull(savedDirectory.getName());
+
+      Directory fetchedDirectory = restClient.target(directoriesUri).path("{name}")
+          .resolveTemplate("name", directory1.getName()).request().get(Directory.class);
+
+      assertEquals(directory1.getName(), fetchedDirectory.getName());
+      assertEquals(directory1.getOwner(), fetchedDirectory.getOwner());
+      assertEquals(directory1.getGroup(), fetchedDirectory.getGroup());
+
+      GenericType<Collection<Directory>> directoryType = new GenericType<Collection<Directory>>(Collection.class);
+      Collection<Directory> directories = restClient.target(directoriesUri).request().get(directoryType);
+
+      assertEquals(1, directories.size());
+
+      restClient.target(directoriesUri).path("{name}").resolveTemplate("name", directory1.getName())
+          .request().delete();
+
+      directories = restClient.target(directoriesUri).request().get(directoryType);
+
+      assertTrue(directories.isEmpty());
+    } finally {
+      restServer.shutdown();
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/tajo/blob/a9ae3cab/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp1/TestApplication1.java
----------------------------------------------------------------------
diff --git a/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp1/TestApplication1.java b/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp1/TestApplication1.java
new file mode 100644
index 0000000..3531b3a
--- /dev/null
+++ b/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp1/TestApplication1.java
@@ -0,0 +1,38 @@
+/**
+ * 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.tajo.ws.rs.netty.testapp1;
+
+import javax.ws.rs.core.Application;
+import java.util.HashSet;
+import java.util.Set;
+
+public class TestApplication1 extends Application {
+
+  private final Set<Class<?>> classes;
+
+  public TestApplication1() {
+    classes = new HashSet<Class<?>>();
+    classes.add(TestResource1.class);
+  }
+
+  @Override
+  public Set<Class<?>> getClasses() {
+    return classes;
+  }
+}

http://git-wip-us.apache.org/repos/asf/tajo/blob/a9ae3cab/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp1/TestResource1.java
----------------------------------------------------------------------
diff --git a/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp1/TestResource1.java b/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp1/TestResource1.java
new file mode 100644
index 0000000..302e217
--- /dev/null
+++ b/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp1/TestResource1.java
@@ -0,0 +1,36 @@
+/**
+ * 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.tajo.ws.rs.netty.testapp1;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+@Path("testapp1")
+public class TestResource1 {
+
+  public static String outputMessage = "TestApplication1";
+
+  @GET
+  @Produces(MediaType.TEXT_PLAIN)
+  public String getApplicationName() {
+    return outputMessage;
+  }
+}

http://git-wip-us.apache.org/repos/asf/tajo/blob/a9ae3cab/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp2/DirectoriesDao.java
----------------------------------------------------------------------
diff --git a/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp2/DirectoriesDao.java b/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp2/DirectoriesDao.java
new file mode 100644
index 0000000..0e82e00
--- /dev/null
+++ b/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp2/DirectoriesDao.java
@@ -0,0 +1,39 @@
+/**
+ * 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.tajo.ws.rs.netty.testapp2;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class DirectoriesDao {
+  private static DirectoriesDao instance = new DirectoriesDao();
+
+  private final Map<String, Directory> directoryMap = new ConcurrentHashMap<String, Directory>();
+
+  private DirectoriesDao() {
+  }
+
+  public static DirectoriesDao getInstance() {
+    return instance;
+  }
+
+  public Map<String, Directory> getDirectoryMap() {
+    return directoryMap;
+  }
+}

http://git-wip-us.apache.org/repos/asf/tajo/blob/a9ae3cab/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp2/DirectoriesResource.java
----------------------------------------------------------------------
diff --git a/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp2/DirectoriesResource.java b/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp2/DirectoriesResource.java
new file mode 100644
index 0000000..40f1ced
--- /dev/null
+++ b/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp2/DirectoriesResource.java
@@ -0,0 +1,85 @@
+/**
+ * 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.tajo.ws.rs.netty.testapp2;
+
+import javax.ws.rs.*;
+import javax.ws.rs.core.*;
+import java.net.URI;
+import java.util.Collection;
+
+@Path("/directories")
+public class DirectoriesResource {
+
+  @Context
+  UriInfo uriInfo;
+
+  @GET
+  @Produces(MediaType.APPLICATION_JSON)
+  public Response getAllDirectories() {
+    Collection<Directory> directories = DirectoriesDao.getInstance().getDirectoryMap().values();
+    GenericEntity<Collection<Directory>> dirEntities =
+        new GenericEntity<Collection<Directory>>(directories, Collection.class);
+    return Response.ok(dirEntities).build();
+  }
+
+  @POST
+  @Consumes(MediaType.APPLICATION_JSON)
+  @Produces(MediaType.APPLICATION_JSON)
+  public Response createDirectory(Directory directory) {
+    String directoryName = directory.getName();
+
+    if (directoryName == null || directoryName.isEmpty()) {
+      return Response.status(Response.Status.BAD_REQUEST).build();
+    }
+
+    DirectoriesDao.getInstance().getDirectoryMap().put(directoryName, directory);
+
+    UriBuilder uriBuilder = uriInfo.getBaseUriBuilder();
+    URI directoryUri = uriBuilder.path(DirectoriesResource.class)
+        .path(DirectoriesResource.class, "getDirectoryByName")
+        .build(directoryName);
+
+    return Response.created(directoryUri).entity(directory).build();
+  }
+
+  @GET
+  @Path("{name}")
+  @Produces(MediaType.APPLICATION_JSON)
+  public Response getDirectoryByName(@PathParam("name") String directoryName) {
+    Directory directory = DirectoriesDao.getInstance().getDirectoryMap().get(directoryName);
+
+    if (directory == null) {
+      return Response.status(Response.Status.NOT_FOUND).build();
+    }
+
+    return Response.ok(directory).build();
+  }
+
+  @DELETE
+  @Path("{name}")
+  public Response deleteDirectory(@PathParam("name") String directoryName) {
+    if (!DirectoriesDao.getInstance().getDirectoryMap().containsKey(directoryName)) {
+      return Response.status(Response.Status.NOT_FOUND).build();
+    }
+
+    DirectoriesDao.getInstance().getDirectoryMap().remove(directoryName);
+
+    return Response.ok().build();
+  }
+}

http://git-wip-us.apache.org/repos/asf/tajo/blob/a9ae3cab/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp2/Directory.java
----------------------------------------------------------------------
diff --git a/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp2/Directory.java b/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp2/Directory.java
new file mode 100644
index 0000000..3851020
--- /dev/null
+++ b/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp2/Directory.java
@@ -0,0 +1,52 @@
+/**
+ * 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.tajo.ws.rs.netty.testapp2;
+
+import java.io.Serializable;
+
+public class Directory implements Serializable {
+
+  private String name;
+  private String owner;
+  private String group;
+
+  public String getName() {
+    return name;
+  }
+
+  public void setName(String name) {
+    this.name = name;
+  }
+
+  public String getOwner() {
+    return owner;
+  }
+
+  public void setOwner(String owner) {
+    this.owner = owner;
+  }
+
+  public String getGroup() {
+    return group;
+  }
+
+  public void setGroup(String group) {
+    this.group = group;
+  }
+}

http://git-wip-us.apache.org/repos/asf/tajo/blob/a9ae3cab/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp2/FileManagementApplication.java
----------------------------------------------------------------------
diff --git a/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp2/FileManagementApplication.java b/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp2/FileManagementApplication.java
new file mode 100644
index 0000000..49026a0
--- /dev/null
+++ b/tajo-rpc/tajo-ws-rs/src/test/java/org/apache/tajo/ws/rs/netty/testapp2/FileManagementApplication.java
@@ -0,0 +1,35 @@
+/**
+ * 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.tajo.ws.rs.netty.testapp2;
+
+import javax.ws.rs.core.Application;
+import java.util.HashSet;
+import java.util.Set;
+
+public class FileManagementApplication extends Application {
+
+  @Override
+  public Set<Class<?>> getClasses() {
+    Set<Class<?>> classes = new HashSet<Class<?>>();
+
+    classes.add(DirectoriesResource.class);
+
+    return classes;
+  }
+}