TAJO-1672: Removing rest api to create table POST 
/databases/{database-name}/tables interface.

Closes #623

Signed-off-by: JaeHwa Jung <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/tajo/repo
Commit: http://git-wip-us.apache.org/repos/asf/tajo/commit/2d7212c8
Tree: http://git-wip-us.apache.org/repos/asf/tajo/tree/2d7212c8
Diff: http://git-wip-us.apache.org/repos/asf/tajo/diff/2d7212c8

Branch: refs/heads/index_support
Commit: 2d7212c835c3faaee1004e1447644b722ec13c71
Parents: 554ce5b
Author: DaeMyung Kang <[email protected]>
Authored: Fri Jul 10 01:16:47 2015 +0900
Committer: JaeHwa Jung <[email protected]>
Committed: Fri Jul 10 01:16:47 2015 +0900

----------------------------------------------------------------------
 CHANGES                                         |   4 +
 .../tajo/ws/rs/resources/QueryResource.java     |  25 ++--
 .../tajo/ws/rs/resources/TablesResource.java    | 109 ++--------------
 .../ws/rs/responses/GetSubmitQueryResponse.java |  54 ++++++++
 .../tajo/ws/rs/resources/TestQueryResource.java |  50 ++++----
 .../rs/resources/TestQueryResultResource.java   |  14 ++-
 .../ws/rs/resources/TestTablesResource.java     | 126 ++++++++++---------
 7 files changed, 191 insertions(+), 191 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tajo/blob/2d7212c8/CHANGES
----------------------------------------------------------------------
diff --git a/CHANGES b/CHANGES
index 63499b5..9cf93cf 100644
--- a/CHANGES
+++ b/CHANGES
@@ -29,6 +29,10 @@ Release 0.11.0 - unreleased
 
   IMPROVEMENT
 
+    TAJO-1672: Removing rest api to create table
+    POST /databases/{database-name}/tables interface
+    (Contributed by DaeMyung Kang, Committed by jaehwa)
+
     TAJO-1638: Remove offset parameter from rest api result/{cacheId}.
     (Contributed by DaeMyung Kang, Committed by jaehwa)
 

http://git-wip-us.apache.org/repos/asf/tajo/blob/2d7212c8/tajo-core/src/main/java/org/apache/tajo/ws/rs/resources/QueryResource.java
----------------------------------------------------------------------
diff --git 
a/tajo-core/src/main/java/org/apache/tajo/ws/rs/resources/QueryResource.java 
b/tajo-core/src/main/java/org/apache/tajo/ws/rs/resources/QueryResource.java
index abecc3a..e4ee4ea 100644
--- a/tajo-core/src/main/java/org/apache/tajo/ws/rs/resources/QueryResource.java
+++ b/tajo-core/src/main/java/org/apache/tajo/ws/rs/resources/QueryResource.java
@@ -18,6 +18,7 @@
 
 package org.apache.tajo.ws.rs.resources;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.tajo.QueryId;
@@ -34,6 +35,7 @@ import org.apache.tajo.session.Session;
 import org.apache.tajo.util.TajoIdUtils;
 import org.apache.tajo.ws.rs.*;
 import org.apache.tajo.ws.rs.requests.SubmitQueryRequest;
+import org.apache.tajo.ws.rs.responses.GetSubmitQueryResponse;
 
 import javax.ws.rs.*;
 import javax.ws.rs.core.*;
@@ -261,19 +263,28 @@ public class QueryResource {
       }
       
       SubmitQueryResponse response = 
-          masterContext.getGlobalEngine().executeQuery(session, 
request.getQuery(), false);
+        masterContext.getGlobalEngine().executeQuery(session, 
request.getQuery(), false);
       if (response.hasResultCode() && 
ClientProtos.ResultCode.ERROR.equals(response.getResultCode())) {
         return ResourcesUtil.createExceptionResponse(LOG, 
response.getErrorMessage());
       } else {
         JerseyResourceDelegateContextKey<UriInfo> uriInfoKey =
-            
JerseyResourceDelegateContextKey.valueOf(JerseyResourceDelegateUtil.UriInfoKey, 
UriInfo.class);
+          
JerseyResourceDelegateContextKey.valueOf(JerseyResourceDelegateUtil.UriInfoKey, 
UriInfo.class);
         UriInfo uriInfo = context.get(uriInfoKey);
-        
+
+        QueryId queryId = new QueryId(response.getQueryId());
         URI queryURI = uriInfo.getBaseUriBuilder()
-            .path(QueryResource.class)
-            .path(QueryResource.class, "getQuery")
-            .build(new QueryId(response.getQueryId()).toString());
-        return Response.created(queryURI).build();
+          .path(QueryResource.class)
+          .path(QueryResource.class, "getQuery")
+          .build(queryId.toString());
+
+        GetSubmitQueryResponse queryResponse = new GetSubmitQueryResponse();
+        if (queryId.isNull() == false) {
+          queryResponse.setUri(queryURI);
+        }
+
+        queryResponse.setResultCode(response.getResultCode());
+        queryResponse.setQuery(request.getQuery());
+        return Response.status(Status.OK).entity(queryResponse).build();
       }
     }
   }

http://git-wip-us.apache.org/repos/asf/tajo/blob/2d7212c8/tajo-core/src/main/java/org/apache/tajo/ws/rs/resources/TablesResource.java
----------------------------------------------------------------------
diff --git 
a/tajo-core/src/main/java/org/apache/tajo/ws/rs/resources/TablesResource.java 
b/tajo-core/src/main/java/org/apache/tajo/ws/rs/resources/TablesResource.java
index f85d25f..8e19fcb 100644
--- 
a/tajo-core/src/main/java/org/apache/tajo/ws/rs/resources/TablesResource.java
+++ 
b/tajo-core/src/main/java/org/apache/tajo/ws/rs/resources/TablesResource.java
@@ -18,34 +18,20 @@
 
 package org.apache.tajo.ws.rs.resources;
 
-import java.net.URI;
-import java.util.*;
-
-import javax.ws.rs.Consumes;
-import javax.ws.rs.DELETE;
-import javax.ws.rs.GET;
-import javax.ws.rs.POST;
-import javax.ws.rs.Path;
-import javax.ws.rs.PathParam;
-import javax.ws.rs.Produces;
-import javax.ws.rs.core.Application;
-import javax.ws.rs.core.Context;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-import javax.ws.rs.core.UriInfo;
-import javax.ws.rs.core.Response.Status;
-
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.tajo.catalog.CatalogService;
 import org.apache.tajo.catalog.CatalogUtil;
 import org.apache.tajo.catalog.TableDesc;
 import org.apache.tajo.master.TajoMaster.MasterContext;
-import org.apache.tajo.ws.rs.JerseyResourceDelegate;
-import org.apache.tajo.ws.rs.JerseyResourceDelegateContext;
-import org.apache.tajo.ws.rs.JerseyResourceDelegateContextKey;
-import org.apache.tajo.ws.rs.JerseyResourceDelegateUtil;
-import org.apache.tajo.ws.rs.ResourcesUtil;
+import org.apache.tajo.ws.rs.*;
+
+import javax.ws.rs.*;
+import javax.ws.rs.core.*;
+import javax.ws.rs.core.Response.Status;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
 
 @Path("/databases/{databaseName}/tables")
 public class TablesResource {
@@ -87,79 +73,6 @@ public class TablesResource {
     this.application = application;
   }
   
-  /**
-   * 
-   * @param databaseName
-   * @param tableMeta
-   * @return
-   */
-  @POST
-  @Consumes(MediaType.APPLICATION_JSON)
-  public Response createNewTable(TableDesc tableDesc) {
-    if (LOG.isDebugEnabled()) {
-      LOG.debug("Client sent a create table request on " + databaseName + " 
database.");
-    }
-    
-    Response response = null;
-    try {
-      initializeContext();
-      JerseyResourceDelegateContextKey<String> databaseNameKey =
-          JerseyResourceDelegateContextKey.valueOf(databaseNameKeyName, 
String.class);
-      context.put(databaseNameKey, databaseName);
-      JerseyResourceDelegateContextKey<TableDesc> tableDescKey =
-          JerseyResourceDelegateContextKey.valueOf(tableDescKeyName, 
TableDesc.class);
-      context.put(tableDescKey, tableDesc);
-      
-      response = JerseyResourceDelegateUtil.runJerseyResourceDelegate(
-          new CreateNewTableDelegate(),
-          application,
-          context,
-          LOG);
-    } catch (Throwable e) {
-      LOG.error(e.getMessage(), e);
-      
-      response = ResourcesUtil.createExceptionResponse(null, e.getMessage());
-    }
-    
-    return response;
-  }
-  
-  private static class CreateNewTableDelegate implements 
JerseyResourceDelegate {
-
-    @Override
-    public Response run(JerseyResourceDelegateContext context) {
-      JerseyResourceDelegateContextKey<String> databaseNameKey =
-          JerseyResourceDelegateContextKey.valueOf(databaseNameKeyName, 
String.class);
-      String databaseName = context.get(databaseNameKey);
-      JerseyResourceDelegateContextKey<TableDesc> tableDescKey =
-          JerseyResourceDelegateContextKey.valueOf(tableDescKeyName, 
TableDesc.class);
-      TableDesc tableDesc = context.get(tableDescKey);
-      JerseyResourceDelegateContextKey<MasterContext> masterContextKey =
-          
JerseyResourceDelegateContextKey.valueOf(JerseyResourceDelegateUtil.MasterContextKey,
 MasterContext.class);
-      MasterContext masterContext = context.get(masterContextKey);
-      
-      if (!CatalogUtil.isFQTableName(tableDesc.getName())) {
-        tableDesc.setName(CatalogUtil.getCanonicalTableName(databaseName, 
tableDesc.getName()));
-      }
-      
-      CatalogService catalogService = masterContext.getCatalog();
-      boolean tableCreated = catalogService.createTable(tableDesc);
-      if (tableCreated) {
-        JerseyResourceDelegateContextKey<UriInfo> uriInfoKey =
-            
JerseyResourceDelegateContextKey.valueOf(JerseyResourceDelegateUtil.UriInfoKey, 
UriInfo.class);
-        UriInfo uriInfo = context.get(uriInfoKey);
-        
-        URI tableUri = uriInfo.getBaseUriBuilder()
-            .path(TablesResource.class)
-            .path(TablesResource.class, "getTable")
-            .build(databaseName, 
CatalogUtil.extractSimpleName(tableDesc.getName()));
-        return Response.created(tableUri).build();
-      } else {
-        return ResourcesUtil.createExceptionResponse(LOG, "Table Creation has 
been failed.");
-      }
-    }
-  }
-  
   @GET
   @Produces(MediaType.APPLICATION_JSON)
   public Response getAllTables() {
@@ -252,10 +165,10 @@ public class TablesResource {
     public Response run(JerseyResourceDelegateContext context) {
       JerseyResourceDelegateContextKey<String> databaseNameKey =
           JerseyResourceDelegateContextKey.valueOf(databaseNameKeyName, 
String.class);
-      String databaseName = context.get(databaseNameKey);
+      String databaseName = context.get(databaseNameKey).toLowerCase();
       JerseyResourceDelegateContextKey<String> tableNameKey =
           JerseyResourceDelegateContextKey.valueOf(tableNameKeyName, 
String.class);
-      String tableName = context.get(tableNameKey);
+      String tableName = context.get(tableNameKey).toLowerCase();
       JerseyResourceDelegateContextKey<MasterContext> masterContextKey =
           
JerseyResourceDelegateContextKey.valueOf(JerseyResourceDelegateUtil.MasterContextKey,
 MasterContext.class);
       MasterContext masterContext = context.get(masterContextKey);
@@ -265,7 +178,7 @@ public class TablesResource {
       }
       
       CatalogService catalogService = masterContext.getCatalog();
-      if (!catalogService.existDatabase(databaseName) || 
+      if (!catalogService.existDatabase(databaseName) ||
           !catalogService.existsTable(databaseName, tableName)) {
         return Response.status(Status.NOT_FOUND).build();
       }

http://git-wip-us.apache.org/repos/asf/tajo/blob/2d7212c8/tajo-core/src/main/java/org/apache/tajo/ws/rs/responses/GetSubmitQueryResponse.java
----------------------------------------------------------------------
diff --git 
a/tajo-core/src/main/java/org/apache/tajo/ws/rs/responses/GetSubmitQueryResponse.java
 
b/tajo-core/src/main/java/org/apache/tajo/ws/rs/responses/GetSubmitQueryResponse.java
new file mode 100644
index 0000000..5654508
--- /dev/null
+++ 
b/tajo-core/src/main/java/org/apache/tajo/ws/rs/responses/GetSubmitQueryResponse.java
@@ -0,0 +1,54 @@
+/**
+ * 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.responses;
+
+import com.google.gson.annotations.Expose;
+import org.apache.tajo.ipc.ClientProtos;
+
+import java.net.URI;
+
+public class GetSubmitQueryResponse {
+  @Expose private ClientProtos.ResultCode resultCode;
+  @Expose private String query;
+  @Expose private URI uri;
+
+  public ClientProtos.ResultCode getResultCode() {
+    return resultCode;
+  }
+
+  public void setResultCode(ClientProtos.ResultCode resultCode) {
+    this.resultCode = resultCode;
+  }
+
+  public String getQuery() {
+    return query;
+  }
+
+  public void setQuery(String query) {
+    this.query = query;
+  }
+
+  public URI getUri() {
+    return uri;
+  }
+
+  public void setUri(URI uri) {
+    this.uri = uri;
+  }
+}

http://git-wip-us.apache.org/repos/asf/tajo/blob/2d7212c8/tajo-core/src/test/java/org/apache/tajo/ws/rs/resources/TestQueryResource.java
----------------------------------------------------------------------
diff --git 
a/tajo-core/src/test/java/org/apache/tajo/ws/rs/resources/TestQueryResource.java
 
b/tajo-core/src/test/java/org/apache/tajo/ws/rs/resources/TestQueryResource.java
index 5d0b150..7942b5e 100644
--- 
a/tajo-core/src/test/java/org/apache/tajo/ws/rs/resources/TestQueryResource.java
+++ 
b/tajo-core/src/test/java/org/apache/tajo/ws/rs/resources/TestQueryResource.java
@@ -27,6 +27,7 @@ import org.apache.tajo.master.QueryInfo;
 import org.apache.tajo.ws.rs.netty.gson.GsonFeature;
 import org.apache.tajo.ws.rs.requests.NewSessionRequest;
 import org.apache.tajo.ws.rs.requests.SubmitQueryRequest;
+import org.apache.tajo.ws.rs.responses.GetSubmitQueryResponse;
 import org.apache.tajo.ws.rs.responses.NewSessionResponse;
 import org.glassfish.jersey.client.ClientProperties;
 import org.glassfish.jersey.filter.LoggingFilter;
@@ -105,17 +106,18 @@ public class TestQueryResource extends QueryTestCaseBase {
     String sessionId = generateNewSessionAndGetId();
     SubmitQueryRequest queryRequest = createNewQueryRequest("select * from 
lineitem");
 
-    Response response = restClient.target(queriesURI)
+    GetSubmitQueryResponse response = restClient.target(queriesURI)
         .request().header(tajoSessionIdHeaderName, sessionId)
-        .post(Entity.entity(queryRequest, MediaType.APPLICATION_JSON));
-    
+        .post(Entity.entity(queryRequest, MediaType.APPLICATION_JSON),
+            new 
GenericType<GetSubmitQueryResponse>(GetSubmitQueryResponse.class));
+
     assertNotNull(response);
-    assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
-    String locationHeader = response.getHeaderString("Location");
-    assertTrue(locationHeader != null && !locationHeader.isEmpty());
+    assertEquals(ResultCode.OK, response.getResultCode());
+    String location = response.getUri().toString();
+    assertTrue(location != null && !location.isEmpty());
     
-    String queryId = locationHeader.lastIndexOf('/') >= 0?
-        locationHeader.substring(locationHeader.lastIndexOf('/')+1):null;
+    String queryId = location.lastIndexOf('/') >= 0?
+                       location.substring(location.lastIndexOf('/')+1):null;
         
     assertTrue(queryId != null && !queryId.isEmpty());
     
@@ -142,17 +144,18 @@ public class TestQueryResource extends QueryTestCaseBase {
     String sessionId = generateNewSessionAndGetId();
     SubmitQueryRequest queryRequest = createNewQueryRequest("select * from 
lineitem");
 
-    Response response = restClient.target(queriesURI)
+    GetSubmitQueryResponse response = restClient.target(queriesURI)
         .request().header(tajoSessionIdHeaderName, sessionId)
-        .post(Entity.entity(queryRequest, MediaType.APPLICATION_JSON));
-    
+        .post(Entity.entity(queryRequest, MediaType.APPLICATION_JSON),
+            new 
GenericType<GetSubmitQueryResponse>(GetSubmitQueryResponse.class));
+
     assertNotNull(response);
-    assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
-    String locationHeader = response.getHeaderString("Location");
-    assertTrue(locationHeader != null && !locationHeader.isEmpty());
+    assertEquals(ResultCode.OK, response.getResultCode());
+    String location = response.getUri().toString();
+    assertTrue(location != null && !location.isEmpty());
     
-    String queryId = locationHeader.lastIndexOf('/') >= 0?
-        locationHeader.substring(locationHeader.lastIndexOf('/')+1):null;
+    String queryId = location.lastIndexOf('/') >= 0?
+                       location.substring(location.lastIndexOf('/')+1):null;
         
     assertTrue(queryId != null && !queryId.isEmpty());
     
@@ -171,17 +174,18 @@ public class TestQueryResource extends QueryTestCaseBase {
     String sessionId = generateNewSessionAndGetId();
     SubmitQueryRequest queryRequest = createNewQueryRequest("select * from 
lineitem");
 
-    Response response = restClient.target(queriesURI)
+    GetSubmitQueryResponse response = restClient.target(queriesURI)
       .request().header(tajoSessionIdHeaderName, sessionId)
-      .post(Entity.entity(queryRequest, MediaType.APPLICATION_JSON));
+      .post(Entity.entity(queryRequest, MediaType.APPLICATION_JSON),
+          new 
GenericType<GetSubmitQueryResponse>(GetSubmitQueryResponse.class));
 
     assertNotNull(response);
-    assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
-    String locationHeader = response.getHeaderString("Location");
-    assertTrue(locationHeader != null && !locationHeader.isEmpty());
+    assertEquals(ResultCode.OK, response.getResultCode());
+    String location = response.getUri().toString();
+    assertTrue(location != null && !location.isEmpty());
 
-    String queryId = locationHeader.lastIndexOf('/') >= 0?
-      locationHeader.substring(locationHeader.lastIndexOf('/')+1):null;
+    String queryId = location.lastIndexOf('/') >= 0?
+                       location.substring(location.lastIndexOf('/')+1):null;
 
     assertTrue(queryId != null && !queryId.isEmpty());
 

http://git-wip-us.apache.org/repos/asf/tajo/blob/2d7212c8/tajo-core/src/test/java/org/apache/tajo/ws/rs/resources/TestQueryResultResource.java
----------------------------------------------------------------------
diff --git 
a/tajo-core/src/test/java/org/apache/tajo/ws/rs/resources/TestQueryResultResource.java
 
b/tajo-core/src/test/java/org/apache/tajo/ws/rs/resources/TestQueryResultResource.java
index fec1626..2e45e9a 100644
--- 
a/tajo-core/src/test/java/org/apache/tajo/ws/rs/resources/TestQueryResultResource.java
+++ 
b/tajo-core/src/test/java/org/apache/tajo/ws/rs/resources/TestQueryResultResource.java
@@ -30,6 +30,7 @@ import org.apache.tajo.ws.rs.netty.gson.GsonFeature;
 import org.apache.tajo.ws.rs.requests.NewSessionRequest;
 import org.apache.tajo.ws.rs.requests.SubmitQueryRequest;
 import org.apache.tajo.ws.rs.responses.GetQueryResultDataResponse;
+import org.apache.tajo.ws.rs.responses.GetSubmitQueryResponse;
 import org.apache.tajo.ws.rs.responses.NewSessionResponse;
 import org.glassfish.jersey.client.ClientProperties;
 import org.glassfish.jersey.filter.LoggingFilter;
@@ -110,16 +111,17 @@ public class TestQueryResultResource extends 
QueryTestCaseBase {
     SubmitQueryRequest request = new SubmitQueryRequest();
     request.setQuery(query);
 
-    Response response = restClient.target(queriesURI)
+    GetSubmitQueryResponse response = restClient.target(queriesURI)
         .request().header(tajoSessionIdHeaderName, sessionId)
-        .post(Entity.entity(request, MediaType.APPLICATION_JSON));
+        .post(Entity.entity(request, MediaType.APPLICATION_JSON),
+                                       new 
GenericType<GetSubmitQueryResponse>(GetSubmitQueryResponse.class));
 
     assertNotNull(response);
-    assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
-    String locationHeader = response.getHeaderString("Location");
-    assertTrue(locationHeader != null && !locationHeader.isEmpty());
+    assertEquals(ResultCode.OK, response.getResultCode());
+    String location = response.getUri().toString();
+    assertTrue(location != null && !location.isEmpty());
 
-    URI queryIdURI = new URI(locationHeader);
+    URI queryIdURI = new URI(location);
 
     assertNotNull(queryIdURI);
 

http://git-wip-us.apache.org/repos/asf/tajo/blob/2d7212c8/tajo-core/src/test/java/org/apache/tajo/ws/rs/resources/TestTablesResource.java
----------------------------------------------------------------------
diff --git 
a/tajo-core/src/test/java/org/apache/tajo/ws/rs/resources/TestTablesResource.java
 
b/tajo-core/src/test/java/org/apache/tajo/ws/rs/resources/TestTablesResource.java
index 3f2bd50..26a8da1 100644
--- 
a/tajo-core/src/test/java/org/apache/tajo/ws/rs/resources/TestTablesResource.java
+++ 
b/tajo-core/src/test/java/org/apache/tajo/ws/rs/resources/TestTablesResource.java
@@ -17,13 +17,17 @@
  */
 package org.apache.tajo.ws.rs.resources;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.tajo.QueryTestCaseBase;
-import org.apache.tajo.catalog.*;
-import org.apache.tajo.common.TajoDataTypes;
+import org.apache.tajo.catalog.CatalogUtil;
+import org.apache.tajo.catalog.TableDesc;
 import org.apache.tajo.conf.TajoConf.ConfVars;
-import org.apache.tajo.storage.StorageConstants;
-import org.apache.tajo.util.KeyValueSet;
+import org.apache.tajo.ipc.ClientProtos;
 import org.apache.tajo.ws.rs.netty.gson.GsonFeature;
+import org.apache.tajo.ws.rs.requests.NewSessionRequest;
+import org.apache.tajo.ws.rs.requests.SubmitQueryRequest;
+import org.apache.tajo.ws.rs.responses.GetSubmitQueryResponse;
+import org.apache.tajo.ws.rs.responses.NewSessionResponse;
 import org.glassfish.jersey.client.ClientProperties;
 import org.glassfish.jersey.filter.LoggingFilter;
 import org.junit.After;
@@ -48,11 +52,15 @@ public class TestTablesResource extends QueryTestCaseBase {
   
   private URI restServiceURI;
   private URI tablesURI;
-  private Client restClient;
+       private URI queriesURI;
+       private URI sessionsURI;
+
+       private Client restClient;
   
-  private static final String defaultDatabaseName = "TestTablesDB";
+  private static final String defaultDatabaseName = "testtablesdb";
+       private static final String tajoSessionIdHeaderName = "X-Tajo-Session";
 
-  public TestTablesResource() {
+       public TestTablesResource() {
     super(defaultDatabaseName);
   }
 
@@ -61,7 +69,9 @@ public class TestTablesResource extends QueryTestCaseBase {
     int restPort = 
testBase.getTestingCluster().getConfiguration().getIntVar(ConfVars.REST_SERVICE_PORT);
     restServiceURI = new URI("http", null, "127.0.0.1", restPort, "/rest", 
null, null);
     tablesURI = new URI(restServiceURI + "/databases/" + defaultDatabaseName + 
"/tables");
-    restClient = ClientBuilder.newBuilder()
+               queriesURI = new URI(restServiceURI + "/queries");
+               sessionsURI = new URI(restServiceURI + "/sessions");
+               restClient = ClientBuilder.newBuilder()
         .register(new GsonFeature(RestTestUtils.registerTypeAdapterMap()))
         .register(LoggingFilter.class)
         .property(ClientProperties.FEATURE_AUTO_DISCOVERY_DISABLE, true)
@@ -73,37 +83,47 @@ public class TestTablesResource extends QueryTestCaseBase {
   public void tearDown() throws Exception {
     restClient.close();
   }
-  
-  private TableDesc createNewTableForTestCreateTable(String tableName) throws 
Exception {
-    Schema tableSchema = new Schema(new Column[] {new Column("column1", 
TajoDataTypes.Type.TEXT)});
-    KeyValueSet tableOptions = new KeyValueSet();
-    tableOptions.set(StorageConstants.TEXT_DELIMITER, 
StorageConstants.DEFAULT_FIELD_DELIMITER);
-    TableMeta tableMeta = new TableMeta("CSV", tableOptions);
-    return new TableDesc(tableName, tableSchema, tableMeta, null);
-  }
-  
-  @Test
-  public void testCreateTable() throws Exception {
-    String tableName = "TestCreateTable";
-    TableDesc tableDesc = createNewTableForTestCreateTable(tableName);
-    
-    Response response = restClient.target(tablesURI)
-        .request().post(Entity.entity(tableDesc, MediaType.APPLICATION_JSON));
-    
-    assertNotNull(response);
-    assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
-  }
-  
+
+       private SubmitQueryRequest createNewQueryRequest(String query) throws 
Exception {
+               SubmitQueryRequest request = new SubmitQueryRequest();
+               request.setQuery(query);
+               return request;
+       }
+
+       private String generateNewSessionAndGetId() throws Exception {
+               NewSessionRequest request = new NewSessionRequest();
+               request.setUserName("tajo-user");
+               request.setDatabaseName(defaultDatabaseName);
+
+               NewSessionResponse response = restClient.target(sessionsURI)
+                       .request().post(Entity.entity(request, 
MediaType.APPLICATION_JSON), NewSessionResponse.class);
+
+               assertNotNull(response);
+               
assertTrue(ClientProtos.ResultCode.OK.equals(response.getResultCode()));
+               assertTrue(response.getId() != null && 
!response.getId().isEmpty());
+
+               return response.getId();
+       }
+
+       private void createNewTableForTestCreateTable(String tableName, String 
sessionId) throws Exception {
+               String query = "create table " + tableName + " (column1 text)";
+               SubmitQueryRequest queryRequest = createNewQueryRequest(query);
+
+               GetSubmitQueryResponse response = restClient.target(queriesURI)
+                       .request().header(tajoSessionIdHeaderName, sessionId)
+                       .post(Entity.entity(queryRequest, 
MediaType.APPLICATION_JSON),
+                               new 
GenericType<GetSubmitQueryResponse>(GetSubmitQueryResponse.class));
+
+               assertNotNull(response);
+               assertEquals(ClientProtos.ResultCode.OK, 
response.getResultCode());
+       }
+
   @Test
   public void testGetAllTable() throws Exception {
-    String tableName = "TestGetAllTable";
-    TableDesc tableDesc = createNewTableForTestCreateTable(tableName);
-    
-    Response response = restClient.target(tablesURI)
-        .request().post(Entity.entity(tableDesc, MediaType.APPLICATION_JSON));
-    
-    assertNotNull(response);
-    assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
+    String tableName = "testgetalltable";
+               String sessionId = generateNewSessionAndGetId();
+
+               createNewTableForTestCreateTable(tableName, sessionId);
 
     Map<String, Collection<String>> tables = restClient.target(tablesURI)
         .request().get(new GenericType<Map<String, 
Collection<String>>>(Map.class));
@@ -114,7 +134,7 @@ public class TestTablesResource extends QueryTestCaseBase {
 
     boolean tableFound = false;
     for (String table: tableNames) {
-      if (tableName.equals(CatalogUtil.extractSimpleName(table))) {
+      if (StringUtils.equalsIgnoreCase(tableName, 
CatalogUtil.extractSimpleName(table))) {
         tableFound = true;
         break;
       }
@@ -125,21 +145,17 @@ public class TestTablesResource extends QueryTestCaseBase 
{
   
   @Test
   public void testGetTable() throws Exception {
-    String tableName = "TestGetTable";
-    TableDesc tableDesc = createNewTableForTestCreateTable(tableName);
-    
-    Response response = restClient.target(tablesURI)
-        .request().post(Entity.entity(tableDesc, MediaType.APPLICATION_JSON));
-    
-    assertNotNull(response);
-    assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
+    String tableName = "testgettable";
+               String sessionId = generateNewSessionAndGetId();
+
+               createNewTableForTestCreateTable(tableName, sessionId);
     
     TableDesc selectedTable = restClient.target(tablesURI)
         .path("/{tableName}").resolveTemplate("tableName", tableName)
         .request().get(new GenericType<TableDesc>(TableDesc.class));
     
     assertNotNull(selectedTable);
-    assertEquals(tableName, 
CatalogUtil.extractSimpleName(selectedTable.getName()));
+    assertTrue(StringUtils.equalsIgnoreCase(tableName, 
CatalogUtil.extractSimpleName(selectedTable.getName())));
   }
   
   @Test
@@ -154,23 +170,19 @@ public class TestTablesResource extends QueryTestCaseBase 
{
   
   @Test
   public void testDropTable() throws Exception {
-    String tableName = "TestDropTable";
-    TableDesc tableDesc = createNewTableForTestCreateTable(tableName);
-    
-    Response response = restClient.target(tablesURI)
-        .request().post(Entity.entity(tableDesc, MediaType.APPLICATION_JSON));
-    
-    assertNotNull(response);
-    assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
+    String tableName = "testdroptable";
+               String sessionId = generateNewSessionAndGetId();
+
+               createNewTableForTestCreateTable(tableName, sessionId);
     
     TableDesc selectedTable = restClient.target(tablesURI)
         .path("/{tableName}").resolveTemplate("tableName", tableName)
         .request().get(new GenericType<TableDesc>(TableDesc.class));
     
     assertNotNull(selectedTable);
-    assertEquals(tableName, 
CatalogUtil.extractSimpleName(selectedTable.getName()));
+    assertTrue(StringUtils.equalsIgnoreCase(tableName, 
CatalogUtil.extractSimpleName(selectedTable.getName())));
     
-    response = restClient.target(tablesURI)
+    Response response = restClient.target(tablesURI)
         .path("/{tableName}").resolveTemplate("tableName", tableName)
         .request().delete();
     

Reply via email to