CADI and a few small updates.
[music.git] / src / test / java / org / onap / music / unittests / TestRestMusicQAPI.java
index f44f717..a9e6e4b 100644 (file)
@@ -1,16 +1,20 @@
 /*
- * ============LICENSE_START========================================== org.onap.music
- * =================================================================== Copyright (c) 2017 AT&T
- * Intellectual Property ===================================================================
- * Licensed 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
+ * ============LICENSE_START==========================================
+ * org.onap.music
+ * ===================================================================
+ *  Copyright (c) 2017 AT&T Intellectual Property
+ * ===================================================================
+ *  Licensed 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
+ *     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.
+ *  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.
  * 
  * ============LICENSE_END=============================================
  * ====================================================================
@@ -35,7 +39,6 @@ import javax.servlet.http.HttpServletResponse;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
-import org.apache.curator.test.TestingServer;
 import org.junit.After;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
@@ -49,6 +52,7 @@ import org.mindrot.jbcrypt.BCrypt;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.junit.MockitoJUnitRunner;
+import org.onap.music.datastore.MusicDataStore;
 import org.onap.music.datastore.MusicDataStoreHandle;
 import org.onap.music.datastore.PreparedQueryObject;
 import org.onap.music.datastore.jsonobjects.JsonDelete;
@@ -63,9 +67,10 @@ import org.onap.music.lockingservice.cassandra.CassaLockStore;
 import org.onap.music.main.MusicCore;
 import org.onap.music.main.MusicUtil;
 //import org.onap.music.main.ResultType;
-import org.onap.music.rest.RestMusicAdminAPI;
+//import org.onap.music.rest.RestMusicAdminAPI;
 import org.onap.music.rest.RestMusicDataAPI;
 import org.onap.music.rest.RestMusicQAPI;
+import org.springframework.test.util.ReflectionTestUtils;
 import org.onap.music.rest.RestMusicLocksAPI;
 import com.datastax.driver.core.DataType;
 import com.datastax.driver.core.ResultSet;
@@ -80,19 +85,18 @@ import com.sun.jersey.core.util.MultivaluedMapImpl;
 @RunWith(MockitoJUnitRunner.class)
 public class TestRestMusicQAPI {
 
-   
-    RestMusicAdminAPI admin = new RestMusicAdminAPI();
+    
+    //RestMusicAdminAPI admin = new RestMusicAdminAPI();
     RestMusicLocksAPI lock = new RestMusicLocksAPI(); 
     RestMusicQAPI qData = new RestMusicQAPI();
     static PreparedQueryObject testObject;
-    static TestingServer zkServer;
 
     @Mock
     static HttpServletResponse http;
 
     @Mock
     UriInfo info;
-  
+    
     static String appName = "TestApp";
     static String userId = "TestUser";
     static String password = "TestPassword";
@@ -126,17 +130,18 @@ public class TestRestMusicQAPI {
     @BeforeClass
     public static void init() throws Exception {
         try {
-            MusicDataStoreHandle.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra();
-            MusicCore.mLockHandle = new CassaLockStore(MusicDataStoreHandle.mDstoreHandle);
+            ReflectionTestUtils.setField(MusicDataStoreHandle.class, "mDstoreHandle",
+                    CassandraCQL.connectToEmbeddedCassandra());
+            MusicCore.mLockHandle = new CassaLockStore(MusicDataStoreHandle.getDSHandle());
 
-           // System.out.println("before class keysp");
+            // System.out.println("before class keysp");
             //resp=data.createKeySpace(majorV,minorV,patchV,aid,appName,userId,password,kspObject,keyspaceName);
             //System.out.println("after keyspace="+keyspaceName);
         } catch (Exception e) {
-          System.out.println("before class exception ");
+            System.out.println("before class exception ");
             e.printStackTrace();
         }
-      // admin keyspace and table
+        // admin keyspace and table
         testObject = new PreparedQueryObject();
         testObject.appendQueryString("CREATE KEYSPACE admin WITH REPLICATION = "
                         + "{'class' : 'SimpleStrategy' , "
@@ -188,7 +193,7 @@ public class TestRestMusicQAPI {
         if (rows.size() > 0) {
             System.out.println("#######UUID is:" + rows.get(0).getUUID("uuid"));
         }
-          
+            
         JsonKeySpace jsonKeyspace = new JsonKeySpace();
         Map<String, String> consistencyInfo = new HashMap<>();
         Map<String, Object> replicationInfo = new HashMap<>();
@@ -214,10 +219,9 @@ public class TestRestMusicQAPI {
         testObject = new PreparedQueryObject();
         testObject.appendQueryString("DROP KEYSPACE IF EXISTS admin");
         MusicCore.eventualPut(testObject);
-        if (MusicDataStoreHandle.mDstoreHandle!=null)
-            MusicDataStoreHandle.mDstoreHandle.close();
-        if (zkServer!=null)
-            zkServer.stop();
+        MusicDataStore mds = (MusicDataStore) ReflectionTestUtils.getField(MusicDataStoreHandle.class, "mDstoreHandle");
+        if (mds != null)
+            mds.close();
     }
 
     
@@ -247,7 +251,7 @@ public class TestRestMusicQAPI {
         System.out.println("Entity" + response.getEntity());
         assertEquals(200, response.getStatus());
     }*/
-  
+    
     @Test
     public void Test1_createQ_FieldsEmpty() throws Exception {
         JsonTable jsonTable = new JsonTable();
@@ -302,7 +306,7 @@ public class TestRestMusicQAPI {
         System.out.println("Entity" + response.getEntity());
         assertEquals(200, response.getStatus());
     }*/
-   
+    
 /*    @Test
     public void Test1_createQ_ClusterOrderGood1() throws Exception {
         String tableNameC="testcjcO";
@@ -472,13 +476,13 @@ public class TestRestMusicQAPI {
         //                      "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, userId, password,
         System.out.println("#######status is " + response.getStatus()+"table namec="+tableNameC);
         System.out.println("Entity" + response.getEntity());
-        assertEquals(401, response.getStatus());
+        assertEquals(400, response.getStatus());
     } 
     
     
     @Test
     public void Test3_createQ_0() throws Exception {
-         //duplicate testing ...
+        //duplicate testing ...
         JsonTable jsonTable = new JsonTable();
         Map<String, String> consistencyInfo = new HashMap<>();
         Map<String, String> fields = new HashMap<>();
@@ -514,34 +518,9 @@ public class TestRestMusicQAPI {
         //assertNotEquals(200,response0.getStatus());
     }
 
-    // Improper Auth
-    @Test
-    public void Test3_createQ1() throws Exception {
-        JsonTable jsonTable = new JsonTable();
-        Map<String, String> consistencyInfo = new HashMap<>();
-        Map<String, String> fields = new HashMap<>();
-        fields.put("uuid", "text");
-        fields.put("emp_name", "text");
-        fields.put("emp_salary", "varint");
-        fields.put("PRIMARY KEY", "(emp_name)");
-        consistencyInfo.put("type", "eventual");
-        jsonTable.setConsistencyInfo(consistencyInfo);
-        jsonTable.setKeyspaceName(keyspaceName);
-        jsonTable.setPartitionKey("emp_name");
-        jsonTable.setClusteringKey("uuid");
-        jsonTable.setTableName(tableName);
-        jsonTable.setClusteringOrder("uuid DESC");
-        jsonTable.setFields(fields);
-        //Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
-        Response response = qData.createQ(majorV, minorV,patchV,
-                        "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, wrongAuthorization,
-                        jsonTable, keyspaceName, tableName);
-        System.out.println("#######status is " + response.getStatus());
-        System.out.println("Entity" + response.getEntity());
-        assertEquals(401, response.getStatus());
-    }
 
     // Improper keyspace
+    @Ignore
     @Test
     public void Test3_createQ2() throws Exception {
         JsonTable jsonTable = new JsonTable();
@@ -631,48 +610,8 @@ public class TestRestMusicQAPI {
         assertEquals(200, response.getStatus());
     }*/
 
-    // Auth Error
-    @Test
-    public void Test4_insertIntoQ3() throws Exception {
-        JsonInsert jsonInsert = new JsonInsert();
-        Map<String, String> consistencyInfo = new HashMap<>();
-        Map<String, Object> values = new HashMap<>();
-        values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
-        values.put("emp_name", "test1");
-        values.put("emp_salary", 1500);
-        consistencyInfo.put("type", "eventual");
-        jsonInsert.setConsistencyInfo(consistencyInfo);
-        jsonInsert.setKeyspaceName(keyspaceName);
-        jsonInsert.setTableName(tableName);
-        jsonInsert.setValues(values);
-        //Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
-        Response response = qData.insertIntoQ(majorV, minorV,patchV,
-                        "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, wrongAuthorization,
-                        jsonInsert, keyspaceName, tableName);
-        assertEquals(401, response.getStatus());
-    }
 
-    // Table wrong
-    @Test
-    public void Test4_insertIntoQ4() throws Exception {
-        JsonInsert jsonInsert = new JsonInsert();
-        Map<String, String> consistencyInfo = new HashMap<>();
-        Map<String, Object> values = new HashMap<>();
-        values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6");
-        values.put("emp_name", "test1");
-        values.put("emp_salary", 1500);
-        consistencyInfo.put("type", "eventual");
-        jsonInsert.setConsistencyInfo(consistencyInfo);
-        jsonInsert.setKeyspaceName(keyspaceName);
-        jsonInsert.setTableName(tableName);
-        jsonInsert.setValues(values);
-        //Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
-        Response response = qData.insertIntoQ(majorV, minorV,patchV,
-                        "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization,
-                        jsonInsert, keyspaceName, "wrong");
-        assertEquals(401, response.getStatus());
-    }
-      
+    
 /*    @Test
     public void Test5_updateQ() throws Exception {
         JsonUpdate jsonUpdate = new JsonUpdate();
@@ -694,25 +633,25 @@ public class TestRestMusicQAPI {
         assertEquals(200, response.getStatus());
     }*/
     
-  @Test
-  public void Test5_updateQEmptyValues() throws Exception {
-      JsonUpdate jsonUpdate = new JsonUpdate();
-      Map<String, String> consistencyInfo = new HashMap<>();
-      MultivaluedMap<String, String> row = new MultivaluedMapImpl();
-      Map<String, Object> values = new HashMap<>();
-      row.add("emp_name", "testName");
-      //values.put("emp_salary", 2500);
-      consistencyInfo.put("type", "atomic");
-      jsonUpdate.setConsistencyInfo(consistencyInfo);
-      jsonUpdate.setKeyspaceName(keyspaceName);
-      jsonUpdate.setTableName(tableName);
-      jsonUpdate.setValues(values);
-      //Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
-      //Mockito.when(info.getQueryParameters()).thenReturn(row);
-      Response response = qData.updateQ(majorV, minorV,patchV, "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
-              authorization, jsonUpdate, keyspaceName, tableName, info);
-      assertNotEquals(200, response.getStatus());
-  }
+    @Test
+    public void Test5_updateQEmptyValues() throws Exception {
+        JsonUpdate jsonUpdate = new JsonUpdate();
+        Map<String, String> consistencyInfo = new HashMap<>();
+        MultivaluedMap<String, String> row = new MultivaluedMapImpl();
+        Map<String, Object> values = new HashMap<>();
+        row.add("emp_name", "testName");
+        //values.put("emp_salary", 2500);
+        consistencyInfo.put("type", "atomic");
+        jsonUpdate.setConsistencyInfo(consistencyInfo);
+        jsonUpdate.setKeyspaceName(keyspaceName);
+        jsonUpdate.setTableName(tableName);
+        jsonUpdate.setValues(values);
+        //Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
+        //Mockito.when(info.getQueryParameters()).thenReturn(row);
+        Response response = qData.updateQ(majorV, minorV,patchV, "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName,
+            authorization, jsonUpdate, keyspaceName, tableName, info);
+        assertNotEquals(200, response.getStatus());
+    }
 
 /*    @Test
     public void Test6_filterQ() throws Exception {  //select
@@ -762,7 +701,7 @@ public class TestRestMusicQAPI {
         jsonSelect.setConsistencyInfo(consistencyInfo);
         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
         UriInfo infoe= mockUriInfo("/peek?");//empty queryParam: cause exception
-       // infoe.setQueryParameters("");
+        // infoe.setQueryParameters("");
         System.out.println("uriinfo="+infoe.getQueryParameters());
         Mockito.when(infoe.getQueryParameters()).thenReturn(row);
         Response response = qData.peek(majorV, minorV,patchV,"abc66ccc-d857-4e90-b1e5-df98a3d40ce6", 
@@ -802,8 +741,8 @@ public class TestRestMusicQAPI {
         //Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
         Mockito.when(info.getQueryParameters()).thenReturn(row);
         Response response = qData.deleteFromQ(majorV, minorV,patchV,
-                        "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization,
-                        jsonDelete, keyspaceName, tableName, info);
+            "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization,
+            jsonDelete, keyspaceName, tableName, info);
         assertEquals(400, response.getStatus());
     }
 
@@ -833,17 +772,17 @@ public class TestRestMusicQAPI {
         Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion());
         Response response = qData.dropQ(majorV, minorV,patchV,
                         "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization,
-                         keyspaceName, tableName);
+                        keyspaceName, tableName);
         assertEquals(200, response.getStatus());
     }*/
-   
+    
     private UriInfo mockUriInfo(String urix) throws URISyntaxException {
-      String uri="http://localhost:8080/MUSIC/rest/v"+majorV+"/priorityq/keyspaces/"+keyspaceName+"/"+tableName+urix;
-      UriInfo uriInfo = Mockito.mock(UriInfo.class);
-      System.out.println("mock urix="+urix+" uri="+uri);
-      Mockito.when(uriInfo.getRequestUri()).thenReturn(new URI(uri));
-      return uriInfo;
-      }
+        String uri="http://localhost:8080/MUSIC/rest/v"+majorV+"/priorityq/keyspaces/"+keyspaceName+"/"+tableName+urix;
+        UriInfo uriInfo = Mockito.mock(UriInfo.class);
+        System.out.println("mock urix="+urix+" uri="+uri);
+        Mockito.when(uriInfo.getRequestUri()).thenReturn(new URI(uri));
+        return uriInfo;
+    }
     
 
     //Empty Fields
@@ -895,7 +834,7 @@ public class TestRestMusicQAPI {
         assertEquals(400, response.getStatus());
     }
 
-  //Clustering key null
+    //Clustering key null
     @Test
     public void Test8_createQ_ClusteringKey_empty() throws Exception {
         String tableNameC="testcjcC";
@@ -949,7 +888,7 @@ public class TestRestMusicQAPI {
         assertEquals(400, response.getStatus());
     }
 
-  //Invalid primary key
+    //Invalid primary key
     @Test
     public void Test8_createQ_primaryKey_invalid() throws Exception {
         String tableNameC="testcjcC";