From: Thomas Nelson Date: Fri, 8 Mar 2019 21:30:09 +0000 (+0000) Subject: Merge "CassaLockStore.java - Sonar Fixes" X-Git-Tag: 2.5.8~6 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=df0b1f769eae01724c8d6d61608190ab8c3446c9;hp=db64dbc34627ef9fd562e8ad9c552564ca054b22;p=music.git Merge "CassaLockStore.java - Sonar Fixes" --- diff --git a/README.md b/README.md index 9d5c4cd1..e826f7c0 100644 --- a/README.md +++ b/README.md @@ -32,25 +32,19 @@ MUSIC is to be installed in a single Dir. /opt/app/music/etc /opt/app/music/logs ``` -When installing Tomcat, Cassandra and Zookeeper they should also be installed here. +When installing Cassandra it should also be installed here. ```bash /opt/app/music/apache-cassandra-n.n.n -/opt/app/music/zookeeper-n.n.n -/opt/app/music/apache-tomcat-n.n.n ``` Its suggested you create links from install dirs to a common name ie: ```bash ln -s /opt/app/music/apache-cassandra-n.n.n cassandra -ln -s /opt/app/music/zookeeper-n.n.n zookeeper -ln -s /opt/app/music/apache-tomcat-n.n.n tomcat ``` -Cassandra and Zookeeper have data dirs. +Cassandra has data dirs. ```bash # For cassandra it should be (This is the default) /opt/app/music/cassandra/data -# For Zookeeper it should be -/opt/app/music/var/zookeeper/ ``` If you are using a VM make sure it has at least 8 GB of RAM (It may work with 4 GB, but with 2 GB it @@ -69,7 +63,6 @@ does give issues). - Ensure you have java jdk 8 or above working on your machine. - Download apache Apache Cassandra 3.2, install into /opt/app/music and follow these instructions http://cassandra.apache.org/doc/latest/getting_started/installing.html till and including Step - By the end of this you should have Cassandra working. -- Download Apache Zookeeper 3.4.6, install into /opt/app/music and follow these instructions https://zookeeper.apache.org/doc/trunk/zookeeperStarted.html pertaining to the standalone operation. By the end of this you should have Zookeeper working. - Create a music.properties file and place it in /opt/app/music/etc/. Here is a sample of the file: ```properties @@ -80,7 +73,6 @@ all.public.ips=localhost ####################################### # Optional current values are defaults ####################################### -#zookeeper.host=localhost #cassandra.host=localhost #music.ip=localhost #debug=true @@ -92,10 +84,11 @@ aaf.endpoint.url=http://aafendpoint/proxy/authz/nss/ ``` - Make a dir /opt/app/music/logs MUSIC dir with MUSIC logs will be created in this dir after MUSIC starts. -- Download the latest Apache Tomcat and install it using these instructions http://tecadmin.net/install-tomcat-9-on-ubuntu/ (this is for version 9). -- Build the MUSIC.war (or download it from https://github.com/att/music/blob/master/MUSIC.war) and place it within the webapps folder of the tomcat installation. - Authentications/AAF Setup For Authentication setup. -- Start tomcat and you should now have MUSIC running. +- Start MUSIC using the jar built in distribution/music folder +```bash +java -jar music.jar +``` Extra Cassandra information for Authentication: @@ -207,50 +200,6 @@ cassandra.password= To access keyspace through cqlsh, login with credentials that are passed to MUSIC while creating the keyspace. -#### Zookeeper: - -Once zookeeper has been installed on all the nodes, modify the zk_install_location/conf/zoo.cfg on all the nodes with the following lines: -```properties -tickTime=2000 -dataDir=/opt/app/music/var/zookeeper -clientPort=2181 -initLimit=5 -syncLimit=2 -quorumListenOnAllIPs=true -server.1=public IP of node 1:2888:3888 -server.2=public IP of node 2:2888:3888 -server.3=public IP of node 3:2888:3888 -``` -In /opt/app/music/var/zookeeper in all the machines, create a file called myid that contains the id of the machine. The machine running node.i will contain just the number i in the file myid. - -Start each of the nodes one by one from the zk_install_location/bin folder using the command: -```bash -./zkServer.sh start -``` -On each node check the file zookeeper.out in the zk_install_location/ bin to make sure all the machines are talking to each other and there are no errors. Note that while the machines are yet to come up there maybe error messages saying that connection has not yet been established. Clearly, this is ok. - -If there are no errors, then from zk_install_location/bin simply run the following to get command line access to zookeeper. ./zkCli.sh - -Run these commands on different machines to make sure the zk nodes are syncing. -```bash -[zkshell] ls / -[zookeeper] -``` -Next, create a new znode by running -```bash -create /zk_test my_data. -``` -This creates a new znode and associates the string "my_data" with the node. You should see: -```bash -[zkshell] create /zk_test my_data -Created /zk_test -``` -Issue another ls / command to see what the directory looks like: -```bash -[zkshell] ls / -[zookeeper, zk_test] -``` - #### MUSIC Create a music.properties file and place it in /opt/app/music/etc at each node. Here is a sample of the file: If this location is to be changed please update the file project.properties in the src/main/resources directory before compiling MUSIC to a war. @@ -263,7 +212,6 @@ all.public.ips=public IP of node 0:public IP of node 1:public IP of node 2 ####################################### # Optional current values are defaults ####################################### -#zookeeper.host=localhost #cassandra.host=localhost #music.ip=localhost #debug=true diff --git a/WebContent/WEB-INF/music.properties.sample b/WebContent/WEB-INF/music.properties.sample index d562429b..871b0c3f 100644 --- a/WebContent/WEB-INF/music.properties.sample +++ b/WebContent/WEB-INF/music.properties.sample @@ -6,7 +6,6 @@ all.public.ips=12.13.14.45:12.13.14.46:12.13.14.47 ####################################### # Optional current values are defaults ####################################### -#zookeeper.host=localhost #cassandra.host=localhost #music.ip=localhost #debug=true diff --git a/distribution/tomcat/Dockerfile b/distribution/tomcat/Dockerfile deleted file mode 100644 index 7d4f2ad9..00000000 --- a/distribution/tomcat/Dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM library/tomcat:8.5 -LABEL purpose="Tomcat for MUSIC" - diff --git a/distribution/zookeeper/Dockerfile b/distribution/zookeeper/Dockerfile deleted file mode 100644 index fdc89dc0..00000000 --- a/distribution/zookeeper/Dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM library/zookeeper:3.4 -LABEL purpose="For MUSIC" - diff --git a/docs/multi.rst b/docs/multi.rst index bc26465f..380f4ea5 100644 --- a/docs/multi.rst +++ b/docs/multi.rst @@ -72,63 +72,6 @@ To access keyspace through cqlsh, login with credentials that are passed to MUSI -Zookeeper: ----------- -Once zookeeper has been installed on all the nodes, modify the **zk_install_location/conf/zoo.cfg** on all the nodes with the following lines: - -:: - - tickTime=2000 - dataDir=/opt/app/music/var/zookeeper - clientPort=2181 - initLimit=5 - syncLimit=2 - quorumListenOnAllIPs=true - server.1=public IP of node 1:2888:3888 - server.2=public IP of node 2:2888:3888 - server.3=public IP of node 3:2888:3888 - -Create the directory /var/zookeeper in all the machines and within that create a file called myid that contains the id of the machine. The machine running node.i will contain just the number i in the file myid. - -Start each of the nodes one by one from the zk_install_location/bin folder using the command: - - - - ./zkServer.sh start - -On each node check the file zookeeper.out in the zk_install_location/ bin to make sure all the machines are talking to each other and there are no errors. Note that while the machines are yet to come up there maybe error messages saying that connection has not yet been established. Clearly, this is ok. - - -If there are no errors, then from zk_install_location/bin simply run the following to get command line access to zookeeper. ./zkCli.sh - - -Run these commands on different machines to make sure the zk nodes are syncing. - -:: - - [zkshell] ls / - [zookeeper] - -Next, create a new znode by running - -:: - - create /zk_test my_data. - -This creates a new znode and associates the string "my_data" with the node. You should see: - -:: - - [zkshell] create /zk_test my_data - Created /zk_test - -Issue another ls / command to see what the directory looks like: - -:: - - [zkshell] ls / - [zookeeper, zk_test] - MUSIC Create a music.properties file and place it in /opt/app/music/etc at each node. Here is a sample of the file: cassandra.yaml:: diff --git a/docs/setup.rst b/docs/setup.rst index b12acd04..7fb5bf49 100644 --- a/docs/setup.rst +++ b/docs/setup.rst @@ -17,27 +17,20 @@ The main MUSIC dir should be:: # These also need to be set up /opt/app/music/etc /opt/app/music/logs - /opt/app/music/lib/zookeeper -When installing Tomcat, Cassandra and Zookeeper they should also be installed here.:: +When installing, Cassandra should also be installed here.:: /opt/app/music/apache-cassandra-n.n.n - /opt/app/music/zookeeper-n.n.n - /opt/app/music/apache-tomcat-n.n.n You could also create links from install dirs to a common name ie\::: ln -s /opt/app/music/apache-cassandra-n.n.n cassandra - ln -s /opt/app/music/zookeeper-n.n.n zookeeper - ln -s /opt/app/music/apache-tomcat-n.n.n tomcat -Cassandra and Zookeeper have data dirs.:: +Cassandra has data dirs.:: # For cassandra it should be (This is the default) /opt/app/music/cassandra/data - # For Zookeeper it should be - /opt/app/music/zookeeper/ Continue by selecting the link to the setup you are doing. diff --git a/docs/single.rst b/docs/single.rst index 060f02b4..632db5ac 100644 --- a/docs/single.rst +++ b/docs/single.rst @@ -15,8 +15,6 @@ Instructions - Ensure you have OpenJDK 8 on your machine. - Download Apache Cassandra 3.0, install into /opt/app/music and follow these instructions http://cassandra.apache.org/doc/latest/getting_started/installing.html till and including Step - By the end of this you should have Cassandra working. -- Download Apache Zookeeper 3.4.6, install into /opt/app/music and follow these instructions https://zookeeper.apache.org/doc/r3.4.6/zookeeperStarted.html pertaining to the standalone operation. By the end of this you should have Zookeeper working. -- Download the Version 8.5 Apache Tomcat and install it using these instructions https://tomcat.apache.org/download-80.cgi (this is for version 8.5). - Create a music.properties file and place it in /opt/app/music/etc/. Here is a sample of the file: music.properties:: @@ -30,7 +28,6 @@ music.properties:: # Optional current values are defaults ###################################### # If using docker this would point to the specific docker name. - #zookeeper.host=localhost #cassandra.host=localhost #music.ip=localhost diff --git a/docs/swagger.json b/docs/swagger.json index 534d2af4..3bc67050 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -964,7 +964,7 @@ "Lock Api" ], "summary": "Create Lock", - "description": "Puts the requesting process in the q for this lock. The corresponding node will be created in zookeeper if it did not already exist. Lock Name is the \"key\" of the form keyspaceName.tableName.rowId", + "description": "Puts the requesting process in the q for this lock. The corresponding lock will be created if it did not already exist. Lock Name is the \"key\" of the form keyspaceName.tableName.rowId", "operationId": "createLockReference", "produces": [ "application/json" diff --git a/examples/VotingApp/src/main/java/main/VotingApp.java b/examples/VotingApp/src/main/java/main/VotingApp.java index 8846ba22..653cef2e 100644 --- a/examples/VotingApp/src/main/java/main/VotingApp.java +++ b/examples/VotingApp/src/main/java/main/VotingApp.java @@ -29,6 +29,7 @@ import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; +import java.util.Base64; import javax.ws.rs.core.MediaType; @@ -66,13 +67,11 @@ public class VotingApp { if (!namespace.equals("")) { builder.header("ns", namespace); } - if (!userId.equals("")) { - builder.header("userId", userId); + if (!userId.equals("") && !password.equals("")) { + String authString = Base64.getEncoder().encodeToString((userId + ":" + password).getBytes()); + builder.header("Authorization", "Basic " + authString); } - if (!password.equals("")) { - builder.header("password", password); - } - + return builder; } @@ -511,4 +510,4 @@ public class VotingApp { } -} \ No newline at end of file +} diff --git a/pom.xml b/pom.xml index a623853c..ef1fbbf3 100755 --- a/pom.xml +++ b/pom.xml @@ -65,7 +65,6 @@ 2.25.1 2.0.1 3.6.0 - 3.4.11 UTF-8 UTF-8 @@ -341,19 +340,6 @@ - - - org.apache.zookeeper - zookeeper - ${zookeeper.version} - - - org.slf4j - slf4j-log4j12 - - - - com.sun.jersey @@ -454,22 +440,6 @@ metrics-core 3.1.0 - - org.apache.curator - curator-test - 2.3.0 - - - org.apache.zookeeper - zookeeper - - - org.slf4j - slf4j-log4j12 - - - test - org.mockito mockito-core @@ -627,12 +597,6 @@ ${project.basedir}/distribution/trigger - - - " + resultMap.toString()); - //assertEquals("Unauthorized: Please check the request parameters. Some of the required values appName(ns), userId, password, isAAF are missing.", resultMap.get("Exception")); + Response response = admin.onboardAppWithMusic(jsonOnboard,adminAuthorization); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(400, response.getStatus()); } @Test - public void test7_onboardSearch() throws Exception { - System.out.println("Testing application onboarding search w/o reaching aaf"); + public void test7_onboardSearch_notOnboarded() throws Exception { + System.out.println("Testing application onboarding search for app that isn't onboarded"); + JsonOnboard jsonOnboard = new JsonOnboard(); jsonOnboard.setAppname("TestApp2"); jsonOnboard.setIsAAF("false"); jsonOnboard.setAid(onboardUUID); Response response = admin.getOnboardedInfoSearch(jsonOnboard,adminAuthorization); System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - //TODO FIX when we can authenticate - //should be 401 error - assertEquals(204, response.getStatus()); + //Application is not onboarded + assertEquals(400, response.getStatus()); } - - // Missing appname - @Ignore + @Test - public void test7_onboardSearch1() throws Exception { + public void test7_onboardSearch() throws Exception { + System.out.println("Testing application onboarding search no matching app"); + onboardApp(); + JsonOnboard jsonOnboard = new JsonOnboard(); + jsonOnboard.setAppname("TestApp2"); jsonOnboard.setIsAAF("false"); jsonOnboard.setAid(onboardUUID); - Map resultMap = (Map) admin.getOnboardedInfoSearch(jsonOnboard,adminAuthorization).getEntity(); - System.out.println("--->" + resultMap.toString()); - resultMap.containsKey("success"); - assertEquals(null, resultMap.get(onboardUUID)); + + Response response = admin.getOnboardedInfoSearch(jsonOnboard,adminAuthorization); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(200, response.getStatus()); } - @Ignore @Test - public void test7_onboardSearch_empty() throws Exception { + public void test7_onboardSearch_wrongCredentials() throws Exception { + System.out.println("Testing application onboarding search w/ wrong credentials"); + JsonOnboard jsonOnboard = new JsonOnboard(); - Response response = admin.getOnboardedInfoSearch(jsonOnboard,adminAuthorization); - // assertEquals(400, response.getStatus()); + jsonOnboard.setAppname("TestApp2"); + jsonOnboard.setIsAAF("false"); + jsonOnboard.setAid(onboardUUID); + Response response = admin.getOnboardedInfoSearch(jsonOnboard,wrongAdminAuthorization); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(401, response.getStatus()); } - @Ignore + // Missing appname @Test - public void test7_onboardSearch_invalidAid() throws Exception { + public void test7_onboardSearch_noAppName() throws Exception { + System.out.println("Testing application onboarding search w/o appname"); + onboardApp(); + JsonOnboard jsonOnboard = new JsonOnboard(); - jsonOnboard.setAppname("TestApp2"); jsonOnboard.setIsAAF("false"); - jsonOnboard.setAid("abc66ccc-d857-4e90-b1e5-df98a3d40ce6"); + jsonOnboard.setAid(onboardUUID); Response response = admin.getOnboardedInfoSearch(jsonOnboard,adminAuthorization); - // assertEquals(400, response.getStatus()); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(200, response.getStatus()); + } + + @Test + public void test7_onboardSearch_empty() throws Exception { + System.out.println("Testing onboard search no app information"); + + JsonOnboard jsonOnboard = new JsonOnboard(); + Response response = admin.getOnboardedInfoSearch(jsonOnboard,adminAuthorization); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(400, response.getStatus()); } - @Ignore @Test public void test8_onboardUpdate() throws Exception { + System.out.println("Testing application onboarding update"); + onboardApp(); + JsonOnboard jsonOnboard = new JsonOnboard(); jsonOnboard.setIsAAF("false"); jsonOnboard.setUserId("TestUser3"); jsonOnboard.setPassword("TestPassword3"); jsonOnboard.setAid(onboardUUID); - Map resultMap = (Map) admin.updateOnboardApp(jsonOnboard,adminAuthorization).getEntity(); - System.out.println("--->" + resultMap.toString()); - resultMap.containsKey("success"); - assertNotNull(resultMap); + Response response = admin.updateOnboardApp(jsonOnboard,adminAuthorization); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(200, response.getStatus()); } + @Ignore //not working correctly @Test - public void test8_onboardUpdateNoAAF() throws Exception { - System.out.println("Testing update application onboarding search w/o reaching aaf"); + public void test8_onboardUpdate_withAppName() throws Exception { + System.out.println("Testing application onboarding update w appname"); + onboardApp(); + JsonOnboard jsonOnboard = new JsonOnboard(); jsonOnboard.setIsAAF("false"); jsonOnboard.setUserId("TestUser3"); jsonOnboard.setPassword("TestPassword3"); + jsonOnboard.setAppname("TestApp2"); jsonOnboard.setAid(onboardUUID); Response response = admin.updateOnboardApp(jsonOnboard,adminAuthorization); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - //Should be 401 error - assertEquals(204, response.getStatus()); - } - // Aid null - @Ignore + assertEquals(200, response.getStatus()); + } + @Test - public void test8_onboardUpdate1() throws Exception { + public void test8_onboardUpdate_noUUID() throws Exception { + System.out.println("Testing application onboarding update null uuid"); + onboardApp(); + JsonOnboard jsonOnboard = new JsonOnboard(); jsonOnboard.setIsAAF("false"); jsonOnboard.setUserId("TestUser3"); jsonOnboard.setPassword("TestPassword3"); - Map resultMap = (Map) admin.updateOnboardApp(jsonOnboard,adminAuthorization).getEntity(); - System.out.println("--->" + resultMap.toString()); - assertNotNull(resultMap); - } + jsonOnboard.setAid(null); + Response response = admin.updateOnboardApp(jsonOnboard,adminAuthorization); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - // Appname not null - @Ignore + assertEquals(400, response.getStatus()); + } + @Test - public void test8_onboardUpdate2() throws Exception { + public void test8_onboardUpdate_wrongCredentialsNoAAF() throws Exception { + System.out.println("Testing update application onboarding search w/ wrong credentials"); JsonOnboard jsonOnboard = new JsonOnboard(); - jsonOnboard.setAppname("TestApp2"); jsonOnboard.setIsAAF("false"); jsonOnboard.setUserId("TestUser3"); jsonOnboard.setPassword("TestPassword3"); jsonOnboard.setAid(onboardUUID); - Map resultMap = (Map) admin.updateOnboardApp(jsonOnboard,adminAuthorization).getEntity(); - assertNotNull(resultMap); + Response response = admin.updateOnboardApp(jsonOnboard,wrongAdminAuthorization); + + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); } // All null - @Ignore @Test - public void test8_onboardUpdate3() throws Exception { + public void test8_onboardUpdate_noAppInfo() throws Exception { + System.out.println("Testing update application onboarding update no app information"); + onboardApp(); + JsonOnboard jsonOnboard = new JsonOnboard(); jsonOnboard.setAid(onboardUUID); - Map resultMap = (Map) admin.updateOnboardApp(jsonOnboard,adminAuthorization).getEntity(); - assertNotNull(resultMap); + Response response = admin.updateOnboardApp(jsonOnboard,adminAuthorization); + + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); } @Test public void test9_onboardDelete() throws Exception { + System.out.println("Testing update application onboarding delete"); + onboardApp(); + JsonOnboard jsonOnboard = new JsonOnboard(); jsonOnboard.setAppname("TestApp2"); jsonOnboard.setAid(onboardUUID); - Map resultMap = (Map) admin.deleteOnboardApp(jsonOnboard,adminAuthorization).getEntity(); - resultMap.containsKey("success"); - assertNotNull(resultMap); + Response response = admin.deleteOnboardApp(jsonOnboard,adminAuthorization); + + //only 1 app matches keyspace + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(200, response.getStatus()); } + @Ignore //not working as expected @Test - public void test9_onboardDeleteNoAAF() throws Exception { - System.out.println("Testing onboard delete without aaf"); + public void test9_onboardDelete_noAID() throws Exception { + System.out.println("Testing update application onboarding delete no AID"); + onboardApp(); + JsonOnboard jsonOnboard = new JsonOnboard(); jsonOnboard.setAppname("TestApp2"); - jsonOnboard.setAid(onboardUUID); + jsonOnboard.setAid(null); Response response = admin.deleteOnboardApp(jsonOnboard,adminAuthorization); + //only 1 app matches name System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - //Should be 401 error - assertEquals(204, response.getStatus()); + assertEquals(200, response.getStatus()); } - - @Ignore + + @Test + public void test9_onboardDelete_noAIDManyMatch() throws Exception { + System.out.println("Testing update application onboarding delete no AID many apps in namespace"); + onboardApp(); + onboardApp(); + + JsonOnboard jsonOnboard = new JsonOnboard(); + jsonOnboard.setAppname("TestApp2"); + jsonOnboard.setAid(null); + Response response = admin.deleteOnboardApp(jsonOnboard,adminAuthorization); + + //multiple apps matches name + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } + + @Test + public void test9_onboardDelete_noAID_noApp() throws Exception { + System.out.println("Testing update application onboarding delete no AID, app not onboarded"); + + JsonOnboard jsonOnboard = new JsonOnboard(); + jsonOnboard.setAppname("TestApp2"); + jsonOnboard.setAid(null); + Response response = admin.deleteOnboardApp(jsonOnboard,adminAuthorization); + + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } + @Test - public void test9_onboardDelete1() throws Exception { + public void test9_onboardDelete_noAppToDelete() throws Exception { + System.out.println("Testing update application onboarding delete no app information"); + onboardApp(); + JsonOnboard jsonOnboard = new JsonOnboard(); - Map resultMap = (Map) admin.deleteOnboardApp(jsonOnboard,adminAuthorization).getEntity(); - assertNotNull(resultMap); + jsonOnboard.setAppname(null); + jsonOnboard.setAid(null); + Response response = admin.deleteOnboardApp(jsonOnboard,adminAuthorization); + + //only 1 app matches keyspace + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); } + @Test + public void test9_onboardDelete_wrongCredentials() throws Exception { + System.out.println("Testing onboard delete with wrong credentials"); + JsonOnboard jsonOnboard = new JsonOnboard(); + jsonOnboard.setAppname("TestApp2"); + jsonOnboard.setAid(onboardUUID); + Response response = admin.deleteOnboardApp(jsonOnboard,wrongAdminAuthorization); + + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } - private static void createAdminTable() throws Exception { - testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQL.createAdminKeyspace); - MusicCore.eventualPut(testObject); - testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQL.createAdminTable); - MusicCore.eventualPut(testObject); - - testObject = new PreparedQueryObject(); - testObject.appendQueryString( - "INSERT INTO admin.keyspace_master (uuid, keyspace_name, application_name, is_api, " - + "password, username, is_aaf) VALUES (?,?,?,?,?,?,?)"); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid)); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), - MusicUtil.DEFAULTKEYSPACENAME)); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True")); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), BCrypt.hashpw(password, BCrypt.gensalt()))); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF)); - MusicCore.eventualPut(testObject); - - testObject = new PreparedQueryObject(); - testObject.appendQueryString( - "select uuid from admin.keyspace_master where application_name = ? allow filtering"); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); - ResultSet rs = MusicCore.get(testObject); - List rows = rs.all(); - if (rows.size() > 0) { - System.out.println("#######UUID is:" + rows.get(0).getUUID("uuid")); - } - } + @Test + public void test10_delete() throws Exception { + System.out.println("Testing GUI delete call"); + onboardApp(); + + assertTrue(admin.delete(adminAuthorization, onboardUUID)); + } + + @Test + public void test11_getAll() { + System.out.println("Testing GUI getall call"); + + System.out.println("admin.getall(adminAuthorization)"); + } + + /** + * Inject our mocked class and accept admin credentials + * @throws NoSuchFieldException + */ + public void authenticateAdminTrue() throws NoSuchFieldException { + authMock = Mockito.mock(MusicAAFAuthentication.class); + FieldSetter.setField(admin, admin.getClass().getDeclaredField("authenticator"), authMock); + + Mockito.when(authMock.authenticateAdmin(Mockito.matches(adminAuthorization))).thenReturn(true); + } + + /** + * onboard the application and store generate uuid into {@link onboardUUID} + * @param onboard + * @throws Exception + */ + public void onboardApp() throws Exception { + JsonOnboard jsonOnboard = new JsonOnboard(); + jsonOnboard.setAppname("TestApp2"); + jsonOnboard.setIsAAF("false"); + jsonOnboard.setUserId("TestUser2"); + jsonOnboard.setPassword("TestPassword2"); + + Response response = admin.onboardAppWithMusic(jsonOnboard,adminAuthorization); + Map respMap = (Map) response.getEntity(); + onboardUUID = respMap.get("Generated AID"); + } } \ No newline at end of file diff --git a/src/test/java/org/onap/music/unittests/TstRestMusicConditionalAPI.java b/src/test/java/org/onap/music/unittests/TstRestMusicConditionalAPI.java index 1f0e24c2..3adab912 100644 --- a/src/test/java/org/onap/music/unittests/TstRestMusicConditionalAPI.java +++ b/src/test/java/org/onap/music/unittests/TstRestMusicConditionalAPI.java @@ -1,16 +1,15 @@ /* * ============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 + * =================================================================== 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 * - * 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============================================= * ==================================================================== @@ -40,6 +39,7 @@ import org.mindrot.jbcrypt.BCrypt; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import org.onap.music.authentication.CachingUtil; import org.onap.music.conductor.conditionals.JsonConditional; import org.onap.music.conductor.conditionals.RestMusicConditionalAPI; import org.onap.music.datastore.MusicDataStoreHandle; @@ -51,7 +51,6 @@ import org.onap.music.datastore.jsonobjects.JsonSelect; import org.onap.music.datastore.jsonobjects.JsonTable; import org.onap.music.datastore.jsonobjects.JsonUpdate; import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.main.CachingUtil; import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; @@ -66,58 +65,58 @@ import com.sun.jersey.core.util.MultivaluedMapImpl; public class TstRestMusicConditionalAPI { RestMusicDataAPI data = new RestMusicDataAPI(); - RestMusicConditionalAPI cond = new RestMusicConditionalAPI(); - static PreparedQueryObject testObject; + RestMusicConditionalAPI cond = new RestMusicConditionalAPI(); + static PreparedQueryObject testObject; - @Mock - HttpServletResponse http; + @Mock + HttpServletResponse http; - @Mock - UriInfo info; + @Mock + UriInfo info; - static String appName = "TestApp"; - static String userId = "TestUser"; - static String password = "TestPassword"; - static String authData = userId+":"+password; - static String wrongAuthData = userId+":"+"pass"; - static String authorization = new String(Base64.encode(authData.getBytes())); - static String wrongAuthorization = new String(Base64.encode(wrongAuthData.getBytes())); - static boolean isAAF = false; - static UUID uuid = UUID.fromString("abc66ccc-d857-4e90-b1e5-df98a3d40ce6"); - static String keyspaceName = "testcassa"; - static String tableName = "employees"; - static String xLatestVersion = "X-latestVersion"; - static String onboardUUID = null; + static String appName = "TestApp"; + static String userId = "TestUser"; + static String password = "TestPassword"; + static String authData = userId + ":" + password; + static String wrongAuthData = userId + ":" + "pass"; + static String authorization = new String(Base64.encode(authData.getBytes())); + static String wrongAuthorization = new String(Base64.encode(wrongAuthData.getBytes())); + static boolean isAAF = false; + static UUID uuid = UUID.fromString("abc66ccc-d857-4e90-b1e5-df98a3d40ce6"); + static String keyspaceName = "testcassa"; + static String tableName = "employees"; + static String xLatestVersion = "X-latestVersion"; + static String onboardUUID = null; - @BeforeClass - public static void init() throws Exception { - System.out.println("Testing RestMusicConditional class"); - try { - createKeyspace(); - } catch (Exception e) { - e.printStackTrace(); - throw new Exception("Unable to initialize before TestRestMusicData test class. " + e.getMessage()); - } - } - - @After - public void afterEachTest( ) throws MusicServiceException { - clearAllTablesFromKeyspace(); - } + @BeforeClass + public static void init() throws Exception { + System.out.println("Testing RestMusicConditional class"); + try { + createKeyspace(); + } catch (Exception e) { + e.printStackTrace(); + throw new Exception("Unable to initialize before TestRestMusicData test class. " + e.getMessage()); + } + } - @AfterClass - public static void tearDownAfterClass() throws Exception { - testObject = new PreparedQueryObject(); - testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName); - MusicCore.eventualPut(testObject); - } - - @Test - public void test_insertIntoTable() throws Exception { - System.out.println("Testing conditional insert into table"); - createTable(); - - JsonConditional jsonCond = new JsonConditional(); + @After + public void afterEachTest() throws MusicServiceException { + clearAllTablesFromKeyspace(); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + testObject = new PreparedQueryObject(); + testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName); + MusicCore.eventualPut(testObject); + } + + @Test + public void test_insertIntoTable() throws Exception { + System.out.println("Testing conditional insert into table"); + createTable(); + + JsonConditional jsonCond = new JsonConditional(); Map consistencyInfo = new HashMap<>(); Map values = new HashMap<>(); values.put("id", "test_id"); @@ -135,7 +134,7 @@ public class TstRestMusicConditionalAPI { nonexists.put("status", "underway"); condition.put("exists", exists); condition.put("nonexists", nonexists); - + jsonCond.setPrimaryKey("id"); jsonCond.setPrimaryKeyValue("testname"); jsonCond.setCasscadeColumnName("plans"); @@ -143,94 +142,62 @@ public class TstRestMusicConditionalAPI { jsonCond.setCasscadeColumnData(cascadeData); jsonCond.setConditions(condition); - Response response = cond.insertConditional("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - appName, authorization, keyspaceName, tableName, jsonCond); - - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - assertEquals(200, response.getStatus()); - } -/* - @Test - public void test4_insertIntoTable2() throws Exception { - System.out.println("Testing insert into table #2"); - createTable(); - JsonInsert jsonInsert = new JsonInsert(); - Map consistencyInfo = new HashMap<>(); - Map 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); - Response response = data.insertIntoTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonInsert, keyspaceName, tableName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - assertEquals(200, response.getStatus()); - } - - // Auth Error - @Test - public void test4_insertIntoTable3() throws Exception { - System.out.println("Testing insert into table with bad credentials"); - createTable(); - JsonInsert jsonInsert = new JsonInsert(); - Map consistencyInfo = new HashMap<>(); - Map 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); - Response response = data.insertIntoTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, wrongAuthorization, - jsonInsert, keyspaceName, tableName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + Response response = cond.insertConditional("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, keyspaceName, tableName, jsonCond); - assertEquals(401, response.getStatus()); - } + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - // Table wrong - @Test - public void test4_insertIntoTable4() throws Exception { - System.out.println("Testing insert into wrong table"); - createTable(); - JsonInsert jsonInsert = new JsonInsert(); - Map consistencyInfo = new HashMap<>(); - Map 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); - Response response = data.insertIntoTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonInsert, keyspaceName, "wrong"); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(200, response.getStatus()); + } + /* + * @Test public void test4_insertIntoTable2() throws Exception { System.out.println("Testing insert into table #2"); + * createTable(); JsonInsert jsonInsert = new JsonInsert(); Map consistencyInfo = new HashMap<>(); + * Map 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); Response response = data.insertIntoTable("1", + * "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, jsonInsert, keyspaceName, tableName); + * System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + * + * assertEquals(200, response.getStatus()); } + * + * // Auth Error + * + * @Test public void test4_insertIntoTable3() throws Exception { + * System.out.println("Testing insert into table with bad credentials"); createTable(); JsonInsert jsonInsert = new + * JsonInsert(); Map consistencyInfo = new HashMap<>(); Map 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); Response response = data.insertIntoTable("1", + * "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, wrongAuthorization, jsonInsert, keyspaceName, + * tableName); System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + * + * assertEquals(401, response.getStatus()); } + * + * // Table wrong + * + * @Test public void test4_insertIntoTable4() throws Exception { + * System.out.println("Testing insert into wrong table"); createTable(); JsonInsert jsonInsert = new JsonInsert(); + * Map consistencyInfo = new HashMap<>(); Map 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); Response response = data.insertIntoTable("1", + * "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, jsonInsert, keyspaceName, "wrong"); + * System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + * + * assertEquals(400, response.getStatus()); } + */ - assertEquals(400, response.getStatus()); - } -*/ + @Test + public void test5_updateTable() throws Exception { + System.out.println("Testing conditional update table"); + createAndInsertIntoTable(); - @Test - public void test5_updateTable() throws Exception { - System.out.println("Testing conditional update table"); - createAndInsertIntoTable(); - Map consistencyInfo = new HashMap<>(); consistencyInfo.put("type", "eventual"); - + JsonConditional jsonCond = new JsonConditional(); Map values = new HashMap<>(); values.put("id", "test_id"); @@ -240,154 +207,85 @@ public class TstRestMusicConditionalAPI { cascadeValue.put("updated", "world"); cascadeData.put("key", "p1"); cascadeData.put("value", cascadeValue); - + jsonCond.setPrimaryKey("id"); jsonCond.setPrimaryKeyValue("test_id"); jsonCond.setCasscadeColumnName("plans"); jsonCond.setTableValues(values); jsonCond.setCasscadeColumnData(cascadeData); - - Response response = cond.updateConditional("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - keyspaceName, tableName, jsonCond); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - assertEquals(200, response.getStatus()); - } -/* - // need mock code to create error for MusicCore methods - @Test - public void test5_updateTableAuthE() throws Exception { - System.out.println("Testing update table #2"); - createTable(); - //MockitoAnnotations.initMocks(this); - JsonUpdate jsonUpdate = new JsonUpdate(); - Map consistencyInfo = new HashMap<>(); - MultivaluedMap row = new MultivaluedMapImpl(); - Map 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); - //add ttl & timestamp - //Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Mockito.when(info.getQueryParameters()).thenReturn(row); - //Map m1= new HashMap<>() ; - //Mockito.when(MusicCore.autheticateUser(appName,userId,password,keyspaceName,"abc66ccc-d857-4e90-b1e5-df98a3d40ce6","updateTable")).thenReturn(m1); - Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorization, jsonUpdate, keyspaceName, tableName, info); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - assertEquals(200, response.getStatus()); - } - - @Ignore - @Test - public void test5_updateTableAuthException1() throws Exception { - System.out.println("Testing update table authentication error"); - createTable(); - JsonUpdate jsonUpdate = new JsonUpdate(); - Map consistencyInfo = new HashMap<>(); - MultivaluedMap row = new MultivaluedMapImpl(); - Map 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.when(info.getQueryParameters()).thenReturn(row); - String authDatax = ":"; - String authorizationx = new String(Base64.encode(authDatax.getBytes())); - Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorizationx, jsonUpdate, keyspaceName, tableName, info); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(401, response.getStatus()); - } + Response response = cond.updateConditional("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, keyspaceName, tableName, jsonCond); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - @Ignore - @Test - public void test5_updateTableAuthEmpty() throws Exception { - System.out.println("Testing update table without authentication"); - createTable(); - - JsonUpdate jsonUpdate = new JsonUpdate(); - Map consistencyInfo = new HashMap<>(); - MultivaluedMap row = new MultivaluedMapImpl(); - Map 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); + assertEquals(200, response.getStatus()); + } + /* + * // need mock code to create error for MusicCore methods + * + * @Test public void test5_updateTableAuthE() throws Exception { System.out.println("Testing update table #2"); + * createTable(); //MockitoAnnotations.initMocks(this); JsonUpdate jsonUpdate = new JsonUpdate(); Map consistencyInfo = new HashMap<>(); MultivaluedMap row = new MultivaluedMapImpl(); + * Map 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); //add + * ttl & timestamp //Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); + * Mockito.when(info.getQueryParameters()).thenReturn(row); //Map m1= new HashMap<>() ; + * //Mockito.when(MusicCore.autheticateUser(appName,userId,password,keyspaceName, + * "abc66ccc-d857-4e90-b1e5-df98a3d40ce6","updateTable")).thenReturn(m1); Response response = data.updateTable("1", + * "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, jsonUpdate, keyspaceName, tableName, + * info); System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + * + * assertEquals(200, response.getStatus()); } + * + * @Ignore + * + * @Test public void test5_updateTableAuthException1() throws Exception { + * System.out.println("Testing update table authentication error"); createTable(); JsonUpdate jsonUpdate = new + * JsonUpdate(); Map consistencyInfo = new HashMap<>(); MultivaluedMap row = new + * MultivaluedMapImpl(); Map 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.when(info.getQueryParameters()).thenReturn(row); String authDatax = ":"; String authorizationx = new + * String(Base64.encode(authDatax.getBytes())); Response response = data.updateTable("1", "1", "1", + * "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorizationx, jsonUpdate, keyspaceName, tableName, info); + * System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + * + * assertEquals(401, response.getStatus()); } + * + * @Ignore + * + * @Test public void test5_updateTableAuthEmpty() throws Exception { + * System.out.println("Testing update table without authentication"); createTable(); + * + * JsonUpdate jsonUpdate = new JsonUpdate(); Map consistencyInfo = new HashMap<>(); + * MultivaluedMap row = new MultivaluedMapImpl(); Map 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.when(info.getQueryParameters()).thenReturn(row); String authDatax =":"+password; String authorizationx = + * new String(Base64.encode(authDatax.getBytes())); String appNamex="xx"; Response response = data.updateTable("1", + * "1", "1", "", appNamex, authorizationx, jsonUpdate, keyspaceName, tableName, info); System.out.println("Status: " + * + response.getStatus() + ". Entity " + response.getEntity()); + * + * assertEquals(401, response.getStatus()); } + * + */ - Mockito.when(info.getQueryParameters()).thenReturn(row); - String authDatax =":"+password; - String authorizationx = new String(Base64.encode(authDatax.getBytes())); - String appNamex="xx"; - Response response = data.updateTable("1", "1", "1", "", appNamex, - authorizationx, jsonUpdate, keyspaceName, tableName, info); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - assertEquals(401, response.getStatus()); - } - -*/ - - private static void createAdminTable() throws Exception { - testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQL.createAdminKeyspace); - MusicCore.eventualPut(testObject); - testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQL.createAdminTable); - MusicCore.eventualPut(testObject); - - testObject = new PreparedQueryObject(); - testObject.appendQueryString( - "INSERT INTO admin.keyspace_master (uuid, keyspace_name, application_name, is_api, " - + "password, username, is_aaf) VALUES (?,?,?,?,?,?,?)"); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid)); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), - MusicUtil.DEFAULTKEYSPACENAME)); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True")); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), BCrypt.hashpw(password, BCrypt.gensalt()))); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF)); - MusicCore.eventualPut(testObject); - - testObject = new PreparedQueryObject(); - testObject.appendQueryString( - "select uuid from admin.keyspace_master where application_name = ? allow filtering"); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); - ResultSet rs = MusicCore.get(testObject); - List rows = rs.all(); - if (rows.size() > 0) { - System.out.println("#######UUID is:" + rows.get(0).getUUID("uuid")); - } - } - + private static void createKeyspace() throws Exception { + // shouldn't really be doing this here, but create keyspace is currently turned off + PreparedQueryObject query = new PreparedQueryObject(); + query.appendQueryString(CassandraCQL.createKeySpace); + MusicCore.eventualPut(query); - private static void createKeyspace() throws Exception { - //shouldn't really be doing this here, but create keyspace is currently turned off - PreparedQueryObject query = new PreparedQueryObject(); - query.appendQueryString(CassandraCQL.createKeySpace); - MusicCore.eventualPut(query); - - boolean isAAF = false; + boolean isAAF = false; String hashedpwd = BCrypt.hashpw(password, BCrypt.gensalt()); query = new PreparedQueryObject(); - query.appendQueryString( - "INSERT into admin.keyspace_master (uuid, keyspace_name, application_name, is_api, " - + "password, username, is_aaf) values (?,?,?,?,?,?,?)"); + query.appendQueryString("INSERT into admin.keyspace_master (uuid, keyspace_name, application_name, is_api, " + + "password, username, is_aaf) values (?,?,?,?,?,?,?)"); query.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid)); query.addValue(MusicUtil.convertToActualDataType(DataType.text(), keyspaceName)); query.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); @@ -398,52 +296,54 @@ public class TstRestMusicConditionalAPI { CachingUtil.updateMusicCache(keyspaceName, appName); CachingUtil.updateMusicValidateCache(appName, userId, hashedpwd); MusicCore.eventualPut(query); - } - - private void clearAllTablesFromKeyspace() throws MusicServiceException { - ArrayList tableNames = new ArrayList<>(); - PreparedQueryObject query = new PreparedQueryObject(); - query.appendQueryString("SELECT table_name FROM system_schema.tables WHERE keyspace_name = '"+keyspaceName+"';"); - ResultSet rs = MusicCore.get(query); - for (Row row: rs) { - tableNames.add(row.getString("table_name")); - } - for (String table: tableNames) { - query = new PreparedQueryObject(); - query.appendQueryString("DROP TABLE " + keyspaceName + "." + table); - MusicCore.eventualPut(query); - } - } - - /** - * Create a table {@link tableName} in {@link keyspaceName} - * @throws Exception - */ - private void createTable() throws Exception { - JsonTable jsonTable = new JsonTable(); - Map consistencyInfo = new HashMap<>(); - Map fields = new HashMap<>(); - fields.put("id", "text"); - fields.put("plans", "map"); - fields.put("PRIMARY KEY", "(id)"); - consistencyInfo.put("type", "eventual"); - jsonTable.setConsistencyInfo(consistencyInfo); - jsonTable.setKeyspaceName(keyspaceName); - jsonTable.setPrimaryKey("id"); - jsonTable.setTableName(tableName); - jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",appName, authorization, - jsonTable, keyspaceName, tableName); - } - - /** - * Create table {@link createTable} and insert into said table - * @throws Exception - */ - private void createAndInsertIntoTable() throws Exception { - createTable(); - + } + + private void clearAllTablesFromKeyspace() throws MusicServiceException { + ArrayList tableNames = new ArrayList<>(); + PreparedQueryObject query = new PreparedQueryObject(); + query.appendQueryString( + "SELECT table_name FROM system_schema.tables WHERE keyspace_name = '" + keyspaceName + "';"); + ResultSet rs = MusicCore.get(query); + for (Row row : rs) { + tableNames.add(row.getString("table_name")); + } + for (String table : tableNames) { + query = new PreparedQueryObject(); + query.appendQueryString("DROP TABLE " + keyspaceName + "." + table); + MusicCore.eventualPut(query); + } + } + + /** + * Create a table {@link tableName} in {@link keyspaceName} + * + * @throws Exception + */ + private void createTable() throws Exception { + JsonTable jsonTable = new JsonTable(); + Map consistencyInfo = new HashMap<>(); + Map fields = new HashMap<>(); + fields.put("id", "text"); + fields.put("plans", "map"); + fields.put("PRIMARY KEY", "(id)"); + consistencyInfo.put("type", "eventual"); + jsonTable.setConsistencyInfo(consistencyInfo); + jsonTable.setKeyspaceName(keyspaceName); + jsonTable.setPrimaryKey("id"); + jsonTable.setTableName(tableName); + jsonTable.setFields(fields); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableName); + } + + /** + * Create table {@link createTable} and insert into said table + * + * @throws Exception + */ + private void createAndInsertIntoTable() throws Exception { + createTable(); + Map consistencyInfo = new HashMap<>(); consistencyInfo.put("type", "eventual"); JsonConditional jsonCond = new JsonConditional(); @@ -462,7 +362,7 @@ public class TstRestMusicConditionalAPI { nonexists.put("status", "underway"); condition.put("exists", exists); condition.put("nonexists", nonexists); - + jsonCond.setPrimaryKey("id"); jsonCond.setPrimaryKeyValue("test_id"); jsonCond.setCasscadeColumnName("plans"); @@ -470,7 +370,7 @@ public class TstRestMusicConditionalAPI { jsonCond.setCasscadeColumnData(cascadeData); jsonCond.setConditions(condition); - Response response = cond.insertConditional("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - appName, authorization, keyspaceName, tableName, jsonCond); - } + Response response = cond.insertConditional("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, keyspaceName, tableName, jsonCond); + } } diff --git a/src/test/java/org/onap/music/unittests/TstRestMusicDataAPI.java b/src/test/java/org/onap/music/unittests/TstRestMusicDataAPI.java index 0f0545e8..18692915 100644 --- a/src/test/java/org/onap/music/unittests/TstRestMusicDataAPI.java +++ b/src/test/java/org/onap/music/unittests/TstRestMusicDataAPI.java @@ -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) 2019 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============================================= * ==================================================================== @@ -40,6 +44,7 @@ import org.mindrot.jbcrypt.BCrypt; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import org.onap.music.authentication.CachingUtil; import org.onap.music.datastore.MusicDataStoreHandle; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.datastore.jsonobjects.JsonDelete; @@ -49,11 +54,11 @@ import org.onap.music.datastore.jsonobjects.JsonSelect; import org.onap.music.datastore.jsonobjects.JsonTable; import org.onap.music.datastore.jsonobjects.JsonUpdate; import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.main.CachingUtil; import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; import org.onap.music.rest.RestMusicDataAPI; +import org.onap.music.rest.RestMusicLocksAPI; import com.datastax.driver.core.DataType; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Row; @@ -64,6 +69,7 @@ import com.sun.jersey.core.util.MultivaluedMapImpl; public class TstRestMusicDataAPI { RestMusicDataAPI data = new RestMusicDataAPI(); + RestMusicLocksAPI lock = new RestMusicLocksAPI(); static PreparedQueryObject testObject; @Mock @@ -72,88 +78,89 @@ public class TstRestMusicDataAPI { @Mock UriInfo info; - static String appName = "TestApp"; - static String userId = "TestUser"; - static String password = "TestPassword"; - static String authData = userId+":"+password; - static String wrongAuthData = userId+":"+"pass"; - static String authorization = new String(Base64.encode(authData.getBytes())); - static String wrongAuthorization = new String(Base64.encode(wrongAuthData.getBytes())); - static boolean isAAF = false; - static UUID uuid = UUID.fromString("abc66ccc-d857-4e90-b1e5-df98a3d40ce6"); - static String keyspaceName = "testcassa"; - static String tableName = "employees"; - static String xLatestVersion = "X-latestVersion"; - static String onboardUUID = null; - - @BeforeClass - public static void init() throws Exception { - System.out.println("Testing RestMusicData class"); - try { - createKeyspace(); - } catch (Exception e) { - e.printStackTrace(); - throw new Exception("Unable to initialize before TestRestMusicData test class. " + e.getMessage()); - } - } - - @After - public void afterEachTest( ) throws MusicServiceException { - clearAllTablesFromKeyspace(); - } + static String appName = "TestApp"; + static String userId = "TestUser"; + static String password = "TestPassword"; + static String authData = userId + ":" + password; + static String wrongAuthData = userId + ":" + "pass"; + static String authorization = new String(Base64.encode(authData.getBytes())); + static String wrongAuthorization = new String(Base64.encode(wrongAuthData.getBytes())); + static boolean isAAF = false; + static UUID uuid = UUID.fromString("abc66ccc-d857-4e90-b1e5-df98a3d40ce6"); + static String keyspaceName = "testcassa"; + static String tableName = "employees"; + static String xLatestVersion = "X-latestVersion"; + static String onboardUUID = null; + static String aid = TestsUsingCassandra.aid; + + @BeforeClass + public static void init() throws Exception { + System.out.println("Testing RestMusicData class"); + try { + createKeyspace(); + } catch (Exception e) { + e.printStackTrace(); + throw new Exception("Unable to initialize before TestRestMusicData test class. " + e.getMessage()); + } + } - @AfterClass - public static void tearDownAfterClass() throws Exception { - testObject = new PreparedQueryObject(); - testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName); - MusicCore.eventualPut(testObject); - } + @After + public void afterEachTest() throws MusicServiceException { + clearAllTablesFromKeyspace(); + } - @Test - public void test1_createKeyspace() throws Exception { - System.out.println("Testing create keyspace"); - JsonKeySpace jsonKeyspace = new JsonKeySpace(); - Map consistencyInfo = new HashMap<>(); - Map replicationInfo = new HashMap<>(); - consistencyInfo.put("type", "eventual"); - replicationInfo.put("class", "SimpleStrategy"); - replicationInfo.put("replication_factor", 1); - jsonKeyspace.setConsistencyInfo(consistencyInfo); - jsonKeyspace.setDurabilityOfWrites("true"); - jsonKeyspace.setKeyspaceName(keyspaceName); - jsonKeyspace.setReplicationInfo(replicationInfo); - //Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Response response = data.createKeySpace("1", "1", "1", null,authorization, appName, jsonKeyspace, keyspaceName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(400,response.getStatus()); - Map respMap = (Map) response.getEntity(); - assertEquals(ResultType.FAILURE, respMap.get("status")); - } + @AfterClass + public static void tearDownAfterClass() throws Exception { + testObject = new PreparedQueryObject(); + testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName); + MusicCore.eventualPut(testObject); + } - @Test - public void test3_createTable() throws Exception { - System.out.println("Testing create table"); - JsonTable jsonTable = new JsonTable(); - Map consistencyInfo = new HashMap<>(); - Map 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.setPrimaryKey("emp_name"); - jsonTable.setTableName(tableName); - jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",appName, authorization, - jsonTable, keyspaceName, tableName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(200, response.getStatus()); - } - - @Test + @Test + public void test1_createKeyspace() throws Exception { + System.out.println("Testing create keyspace"); + JsonKeySpace jsonKeyspace = new JsonKeySpace(); + Map consistencyInfo = new HashMap<>(); + Map replicationInfo = new HashMap<>(); + consistencyInfo.put("type", "eventual"); + replicationInfo.put("class", "SimpleStrategy"); + replicationInfo.put("replication_factor", 1); + jsonKeyspace.setConsistencyInfo(consistencyInfo); + jsonKeyspace.setDurabilityOfWrites("true"); + jsonKeyspace.setKeyspaceName(keyspaceName); + jsonKeyspace.setReplicationInfo(replicationInfo); + // Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); + Response response = + data.createKeySpace("1", "1", "1", null, authorization, appName, jsonKeyspace, keyspaceName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + Map respMap = (Map) response.getEntity(); + assertEquals(ResultType.FAILURE, respMap.get("status")); + } + + @Test + public void test3_createTable() throws Exception { + System.out.println("Testing create table"); + JsonTable jsonTable = new JsonTable(); + Map consistencyInfo = new HashMap<>(); + Map 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.setPrimaryKey("emp_name"); + jsonTable.setTableName(tableName); + jsonTable.setFields(fields); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(200, response.getStatus()); + } + + @Test public void test3_createTableNoName() throws Exception { System.out.println("Testing create table without name"); JsonTable jsonTable = new JsonTable(); @@ -169,330 +176,282 @@ public class TstRestMusicDataAPI { jsonTable.setPrimaryKey("emp_name"); jsonTable.setTableName(""); jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",appName, authorization, - jsonTable, keyspaceName, ""); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, ""); System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); assertEquals(400, response.getStatus()); } - @Test - public void test3_createTableClusterOrderBad() throws Exception { - System.out.println("Testing create table bad clustering"); - JsonTable jsonTable = new JsonTable(); - Map consistencyInfo = new HashMap<>(); - Map fields = new HashMap<>(); - fields.put("uuid", "text"); - fields.put("emp_name", "text"); - fields.put("emp_salary", "varint"); - fields.put("PRIMARY KEY", "(emp_name,emp_salary)"); - consistencyInfo.put("type", "eventual"); - jsonTable.setConsistencyInfo(consistencyInfo); - jsonTable.setKeyspaceName(keyspaceName); - jsonTable.setPrimaryKey("emp_name,emp_salary"); - jsonTable.setClusteringOrder("ASC"); - jsonTable.setTableName(tableName); - jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",appName, authorization, - jsonTable, keyspaceName, tableName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(400, response.getStatus()); - } + @Test + public void test3_createTableClusterOrderBad() throws Exception { + System.out.println("Testing create table bad clustering"); + JsonTable jsonTable = new JsonTable(); + Map consistencyInfo = new HashMap<>(); + Map fields = new HashMap<>(); + fields.put("uuid", "text"); + fields.put("emp_name", "text"); + fields.put("emp_salary", "varint"); + fields.put("PRIMARY KEY", "(emp_name,emp_salary)"); + consistencyInfo.put("type", "eventual"); + jsonTable.setConsistencyInfo(consistencyInfo); + jsonTable.setKeyspaceName(keyspaceName); + jsonTable.setPrimaryKey("emp_name,emp_salary"); + jsonTable.setClusteringOrder("ASC"); + jsonTable.setTableName(tableName); + jsonTable.setFields(fields); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } - @Test - public void test3_createTable_withPropertiesNotNull() throws Exception { - System.out.println("Testing create table with properties"); - JsonTable jsonTable = new JsonTable(); - Map consistencyInfo = new HashMap<>(); - Map 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"); - Map properties = new HashMap<>(); - properties.put("comment","Testing prperties not null"); - jsonTable.setConsistencyInfo(consistencyInfo); - jsonTable.setKeyspaceName(keyspaceName); - jsonTable.setPrimaryKey("emp_name"); - String tableName_prop=tableName+"_Prop"; - jsonTable.setTableName(tableName_prop); - jsonTable.setFields(fields); - jsonTable.setProperties(properties); - - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",appName, authorization, - jsonTable, keyspaceName, tableName_prop); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(200, response.getStatus()); - } + @Test + public void test3_createTable_withPropertiesNotNull() throws Exception { + System.out.println("Testing create table with properties"); + JsonTable jsonTable = new JsonTable(); + Map consistencyInfo = new HashMap<>(); + Map 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"); + Map properties = new HashMap<>(); + properties.put("comment", "Testing prperties not null"); + jsonTable.setConsistencyInfo(consistencyInfo); + jsonTable.setKeyspaceName(keyspaceName); + jsonTable.setPrimaryKey("emp_name"); + String tableName_prop = tableName + "_Prop"; + jsonTable.setTableName(tableName_prop); + jsonTable.setFields(fields); + jsonTable.setProperties(properties); - @Test - public void test3_createTable_duplicateTable() throws Exception { - System.out.println("Testing creating duplicate tables"); - JsonTable jsonTable = new JsonTable(); - Map consistencyInfo = new HashMap<>(); - Map 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.setPrimaryKey("emp_name"); - String tableNameDup=tableName+"x"; - jsonTable.setTableName(tableNameDup); - jsonTable.setFields(fields); - //Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Response response1 = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonTable, keyspaceName, tableNameDup); - - Response response2 = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonTable, keyspaceName, tableNameDup); - System.out.println("Status: " + response2.getStatus() + ". Entity " + response2.getEntity()); - - assertEquals(400, response2.getStatus()); - Map respMap = (Map) response2.getEntity(); - assertEquals(ResultType.FAILURE, respMap.get("status")); - assertEquals("Table " + keyspaceName + "." + tableNameDup + " already exists", - respMap.get("error")); - } + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableName_prop); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(200, response.getStatus()); + } - // Improper Auth - @Test - public void test3_createTable1() throws Exception { - System.out.println("Testing create table w/ improper authentication"); - JsonTable jsonTable = new JsonTable(); - Map consistencyInfo = new HashMap<>(); - Map 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.setPrimaryKey("emp_name"); - jsonTable.setTableName(tableName); - jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, wrongAuthorization, - jsonTable, keyspaceName, tableName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(401, response.getStatus()); - } + @Test + public void test3_createTable_duplicateTable() throws Exception { + System.out.println("Testing creating duplicate tables"); + JsonTable jsonTable = new JsonTable(); + Map consistencyInfo = new HashMap<>(); + Map 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.setPrimaryKey("emp_name"); + String tableNameDup = tableName + "x"; + jsonTable.setTableName(tableNameDup); + jsonTable.setFields(fields); + // Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); + Response response1 = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableNameDup); + + Response response2 = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableNameDup); + System.out.println("Status: " + response2.getStatus() + ". Entity " + response2.getEntity()); + + assertEquals(400, response2.getStatus()); + Map respMap = (Map) response2.getEntity(); + assertEquals(ResultType.FAILURE, respMap.get("status")); + assertEquals("Table " + keyspaceName + "." + tableNameDup + " already exists", respMap.get("error")); + } - // Improper keyspace - @Test - public void test3_createTable3() throws Exception { - System.out.println("Testing create table for wrong keyspace"); - JsonTable jsonTable = new JsonTable(); - Map consistencyInfo = new HashMap<>(); - Map 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.setPrimaryKey("emp_name"); - jsonTable.setTableName(tableName); - jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonTable, "wrong", tableName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(401, response.getStatus()); - } + // Improper Auth + @Test + public void test3_createTable1() throws Exception { + System.out.println("Testing create table w/ improper authentication"); + JsonTable jsonTable = new JsonTable(); + Map consistencyInfo = new HashMap<>(); + Map 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.setPrimaryKey("emp_name"); + jsonTable.setTableName(tableName); + jsonTable.setFields(fields); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + wrongAuthorization, jsonTable, keyspaceName, tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } - // Improper parenthesis in key field - @Test - public void test3_createTable_badParantesis() throws Exception { - System.out.println("Testing malformed create table request"); - String tableNameC ="testTable0"; - JsonTable jsonTable = new JsonTable(); - Map consistencyInfo = new HashMap<>(); - Map fields = new HashMap<>(); - fields.put("uuid", "text"); - fields.put("emp_name", "text"); - fields.put("emp_salary", "varint"); - fields.put("PRIMARY KEY", "(emp_name),emp_id)"); - fields.put("emp_id", "varint"); - consistencyInfo.put("type", "eventual"); - jsonTable.setConsistencyInfo(consistencyInfo); - jsonTable.setKeyspaceName(keyspaceName); - jsonTable.setPrimaryKey("emp_name"); - jsonTable.setTableName(tableNameC); - jsonTable.setClusteringOrder("emp_id Desc"); - jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonTable, keyspaceName, tableNameC); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + // Improper keyspace + @Test + public void test3_createTable3() throws Exception { + System.out.println("Testing create table for wrong keyspace"); + JsonTable jsonTable = new JsonTable(); + Map consistencyInfo = new HashMap<>(); + Map 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.setPrimaryKey("emp_name"); + jsonTable.setTableName(tableName); + jsonTable.setFields(fields); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, "wrong", tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } - assertEquals(400, response.getStatus()); - } + // Improper parenthesis in key field + @Test + public void test3_createTable_badParantesis() throws Exception { + System.out.println("Testing malformed create table request"); + String tableNameC = "testTable0"; + JsonTable jsonTable = new JsonTable(); + Map consistencyInfo = new HashMap<>(); + Map fields = new HashMap<>(); + fields.put("uuid", "text"); + fields.put("emp_name", "text"); + fields.put("emp_salary", "varint"); + fields.put("PRIMARY KEY", "(emp_name),emp_id)"); + fields.put("emp_id", "varint"); + consistencyInfo.put("type", "eventual"); + jsonTable.setConsistencyInfo(consistencyInfo); + jsonTable.setKeyspaceName(keyspaceName); + jsonTable.setPrimaryKey("emp_name"); + jsonTable.setTableName(tableNameC); + jsonTable.setClusteringOrder("emp_id Desc"); + jsonTable.setFields(fields); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableNameC); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } - // good clustering key - @Test - public void test3_createTable_1_clusterKey_good() throws Exception { - System.out.println("Testing create w/ clusterKey"); - String tableNameC ="testTableC1"; - JsonTable jsonTable = new JsonTable(); - Map consistencyInfo = new HashMap<>(); - Map fields = new HashMap<>(); - fields.put("uuid", "text"); - fields.put("emp_name", "text"); - fields.put("emp_salary", "varint"); - fields.put("PRIMARY KEY", "((emp_name),emp_salary)"); - consistencyInfo.put("type", "eventual"); - jsonTable.setConsistencyInfo(consistencyInfo); - jsonTable.setKeyspaceName(keyspaceName); - // jsonTable.setPrimaryKey("emp_name"); - jsonTable.setTableName(tableNameC); - jsonTable.setClusteringOrder("emp_salary ASC"); - jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonTable, keyspaceName, tableNameC); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + // good clustering key + @Test + public void test3_createTable_1_clusterKey_good() throws Exception { + System.out.println("Testing create w/ clusterKey"); - assertEquals(200, response.getStatus()); - } + String tableNameC = "testTableC1"; + JsonTable jsonTable = new JsonTable(); + Map consistencyInfo = new HashMap<>(); + Map fields = new HashMap<>(); + fields.put("uuid", "text"); + fields.put("emp_name", "text"); + fields.put("emp_salary", "varint"); + fields.put("PRIMARY KEY", "((emp_name),emp_salary)"); + consistencyInfo.put("type", "eventual"); + jsonTable.setConsistencyInfo(consistencyInfo); + jsonTable.setKeyspaceName(keyspaceName); + // jsonTable.setPrimaryKey("emp_name"); + jsonTable.setTableName(tableNameC); + jsonTable.setClusteringOrder("emp_salary ASC"); + jsonTable.setFields(fields); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableNameC); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - // bad partition key=clustering key - @Test - public void test3_createTable_2_clusterKey_bad() throws Exception { - System.out.println("Testing create w/ bad clusterKey"); - String tableNameC ="testTableC2"; - JsonTable jsonTable = new JsonTable(); - Map consistencyInfo = new HashMap<>(); - Map fields = new HashMap<>(); - fields.put("uuid", "text"); - fields.put("emp_name", "text"); - fields.put("emp_salary", "varint"); - fields.put("PRIMARY KEY", "((emp_name),emp_name)"); - consistencyInfo.put("type", "eventual"); - jsonTable.setConsistencyInfo(consistencyInfo); - jsonTable.setKeyspaceName(keyspaceName); - jsonTable.setPrimaryKey("emp_name"); // "PRIMARY KEY" overrides if primaryKey present - jsonTable.setTableName(tableNameC); - jsonTable.setClusteringOrder("emp_salary ASC"); - jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonTable, keyspaceName, tableNameC); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(200, response.getStatus()); + } - assertEquals(400, response.getStatus()); - } + // bad partition key=clustering key + @Test + public void test3_createTable_2_clusterKey_bad() throws Exception { + System.out.println("Testing create w/ bad clusterKey"); + String tableNameC = "testTableC2"; + JsonTable jsonTable = new JsonTable(); + Map consistencyInfo = new HashMap<>(); + Map fields = new HashMap<>(); + fields.put("uuid", "text"); + fields.put("emp_name", "text"); + fields.put("emp_salary", "varint"); + fields.put("PRIMARY KEY", "((emp_name),emp_name)"); + consistencyInfo.put("type", "eventual"); + jsonTable.setConsistencyInfo(consistencyInfo); + jsonTable.setKeyspaceName(keyspaceName); + jsonTable.setPrimaryKey("emp_name"); // "PRIMARY KEY" overrides if primaryKey present + jsonTable.setTableName(tableNameC); + jsonTable.setClusteringOrder("emp_salary ASC"); + jsonTable.setFields(fields); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableNameC); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - // good composite partition key,clustering key - @Test - public void test3_createTable_3_pfartition_clusterKey_good() throws Exception { - System.out.println("Testing create w/ composite partition key, clusterKey"); + assertEquals(400, response.getStatus()); + } - String tableNameC ="testTableC3"; - JsonTable jsonTable = new JsonTable(); - Map consistencyInfo = new HashMap<>(); - Map fields = new HashMap<>(); - fields.put("uuid", "text"); - fields.put("emp_name", "text"); - fields.put("emp_id", "varint"); - fields.put("emp_salary", "varint"); - fields.put("PRIMARY KEY", "((emp_name,emp_id),emp_salary)"); - consistencyInfo.put("type", "eventual"); - jsonTable.setConsistencyInfo(consistencyInfo); - jsonTable.setKeyspaceName(keyspaceName); - jsonTable.setPrimaryKey("emp_name"); - jsonTable.setTableName(tableNameC); - jsonTable.setClusteringOrder("emp_salary ASC"); - jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonTable, keyspaceName, tableNameC); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + // good composite partition key,clustering key + @Test + public void test3_createTable_3_pfartition_clusterKey_good() throws Exception { + System.out.println("Testing create w/ composite partition key, clusterKey"); - assertEquals(200, response.getStatus()); - } + String tableNameC = "testTableC3"; + JsonTable jsonTable = new JsonTable(); + Map consistencyInfo = new HashMap<>(); + Map fields = new HashMap<>(); + fields.put("uuid", "text"); + fields.put("emp_name", "text"); + fields.put("emp_id", "varint"); + fields.put("emp_salary", "varint"); + fields.put("PRIMARY KEY", "((emp_name,emp_id),emp_salary)"); + consistencyInfo.put("type", "eventual"); + jsonTable.setConsistencyInfo(consistencyInfo); + jsonTable.setKeyspaceName(keyspaceName); + jsonTable.setPrimaryKey("emp_name"); + jsonTable.setTableName(tableNameC); + jsonTable.setClusteringOrder("emp_salary ASC"); + jsonTable.setFields(fields); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableNameC); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - // bad - wrong cols in order by of composite partition key,clustering key - @Test - public void test3_createTable_5_clusteringOrder_bad() throws Exception { - System.out.println("Testing create table bad request with clustering & composite keys"); - String tableNameC ="testTableC5"; - JsonTable jsonTable = new JsonTable(); - Map consistencyInfo = new HashMap<>(); - Map fields = new HashMap<>(); - fields.put("uuid", "text"); - fields.put("emp_name", "text"); - fields.put("emp_id", "varint"); - fields.put("emp_salary", "varint"); - fields.put("PRIMARY KEY", "((uuid,emp_name),emp_id,emp_salary)"); - consistencyInfo.put("type", "eventual"); - jsonTable.setConsistencyInfo(consistencyInfo); - jsonTable.setKeyspaceName(keyspaceName); - jsonTable.setPrimaryKey("emp_name"); - jsonTable.setTableName(tableNameC); - jsonTable.setClusteringOrder("emp_idx desc, emp_salary ASC"); - jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonTable, keyspaceName, tableNameC); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(200, response.getStatus()); + } - assertEquals(400, response.getStatus()); - } + // bad - wrong cols in order by of composite partition key,clustering key + @Test + public void test3_createTable_5_clusteringOrder_bad() throws Exception { + System.out.println("Testing create table bad request with clustering & composite keys"); + String tableNameC = "testTableC5"; + JsonTable jsonTable = new JsonTable(); + Map consistencyInfo = new HashMap<>(); + Map fields = new HashMap<>(); + fields.put("uuid", "text"); + fields.put("emp_name", "text"); + fields.put("emp_id", "varint"); + fields.put("emp_salary", "varint"); + fields.put("PRIMARY KEY", "((uuid,emp_name),emp_id,emp_salary)"); + consistencyInfo.put("type", "eventual"); + jsonTable.setConsistencyInfo(consistencyInfo); + jsonTable.setKeyspaceName(keyspaceName); + jsonTable.setPrimaryKey("emp_name"); + jsonTable.setTableName(tableNameC); + jsonTable.setClusteringOrder("emp_idx desc, emp_salary ASC"); + jsonTable.setFields(fields); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableNameC); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } - // good clustering key, need to pass queryparameter - @Test - public void test3_createTableIndex_1() throws Exception { - System.out.println("Testing index in create table"); - String tableNameC ="testTableCinx"; - JsonTable jsonTable = new JsonTable(); - Map consistencyInfo = new HashMap<>(); - Map fields = new HashMap<>(); - fields.put("uuid", "text"); - fields.put("emp_name", "text"); - fields.put("emp_salary", "varint"); - fields.put("PRIMARY KEY", "((emp_name),emp_salary)"); - consistencyInfo.put("type", "eventual"); - jsonTable.setConsistencyInfo(consistencyInfo); - jsonTable.setKeyspaceName(keyspaceName); - jsonTable.setTableName(tableNameC); - jsonTable.setClusteringOrder("emp_salary ASC"); - jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonTable, keyspaceName, tableNameC); - // if 200 print to log otherwise fail assertEquals(200, response.getStatus()); - // info.setQueryParameters("index_name=inx_uuid"); - Map queryParametersMap =new HashMap(); - - queryParametersMap.put("index_name","inxuuid"); - Mockito.when(info.getQueryParameters()).thenReturn(new MultivaluedHashMap(queryParametersMap)); - response = data.createIndex("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - keyspaceName, tableNameC,"uuid",info); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(200, response.getStatus()); - } - - // create index without table name + // good clustering key, need to pass queryparameter @Test - public void test3_createTableIndexNoName() throws Exception { - System.out.println("Testing index in create table w/o tablename"); - String tableNameC ="testTableCinx"; + public void test3_createTableIndex_1() throws Exception { + System.out.println("Testing index in create table"); + String tableNameC = "testTableCinx"; JsonTable jsonTable = new JsonTable(); Map consistencyInfo = new HashMap<>(); Map fields = new HashMap<>(); @@ -506,49 +465,112 @@ public class TstRestMusicDataAPI { jsonTable.setTableName(tableNameC); jsonTable.setClusteringOrder("emp_salary ASC"); jsonTable.setFields(fields); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonTable, keyspaceName, tableNameC); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableNameC); // if 200 print to log otherwise fail assertEquals(200, response.getStatus()); // info.setQueryParameters("index_name=inx_uuid"); - Map queryParametersMap =new HashMap(); + Map queryParametersMap = new HashMap(); - queryParametersMap.put("index_name","inxuuid"); - response = data.createIndex("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - "", "","uuid",info); + queryParametersMap.put("index_name", "inxuuid"); + Mockito.when(info.getQueryParameters()).thenReturn(new MultivaluedHashMap(queryParametersMap)); + response = data.createIndex("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, + keyspaceName, tableNameC, "uuid", info); System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(400, response.getStatus()); + assertEquals(200, response.getStatus()); } - @Test - public void test4_insertIntoTable() throws Exception { - System.out.println("Testing insert into table"); - createTable(); - JsonInsert jsonInsert = new JsonInsert(); - Map consistencyInfo = new HashMap<>(); - Map values = new HashMap<>(); - values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6"); - values.put("emp_name", "testname"); - values.put("emp_salary", 500); - consistencyInfo.put("type", "eventual"); - jsonInsert.setConsistencyInfo(consistencyInfo); - jsonInsert.setKeyspaceName(keyspaceName); - jsonInsert.setTableName(tableName); - jsonInsert.setValues(values); - Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - appName, authorization, jsonInsert, keyspaceName, tableName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - assertEquals(200, response.getStatus()); - } - - @Test - public void test4_insertIntoTableCriticalNoLockID() throws Exception { - System.out.println("Testing atomic insert into table without lockid"); - createTable(); - JsonInsert jsonInsert = new JsonInsert(); + // good clustering key, need to pass queryparameter + @Test + public void test3_createTableIndex_badAuth() throws Exception { + System.out.println("Testing index in create table w/ wrong authorization"); + String tableNameC = "testTableCinx"; + JsonTable jsonTable = new JsonTable(); + Map consistencyInfo = new HashMap<>(); + Map fields = new HashMap<>(); + fields.put("uuid", "text"); + fields.put("emp_name", "text"); + fields.put("emp_salary", "varint"); + fields.put("PRIMARY KEY", "((emp_name),emp_salary)"); + consistencyInfo.put("type", "eventual"); + jsonTable.setConsistencyInfo(consistencyInfo); + jsonTable.setKeyspaceName(keyspaceName); + jsonTable.setTableName(tableNameC); + jsonTable.setClusteringOrder("emp_salary ASC"); + jsonTable.setFields(fields); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableNameC); + // if 200 print to log otherwise fail assertEquals(200, response.getStatus()); + // info.setQueryParameters("index_name=inx_uuid"); + Map queryParametersMap = new HashMap(); + + queryParametersMap.put("index_name", "inxuuid"); + response = data.createIndex("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + wrongAuthorization, keyspaceName, tableNameC, "uuid", info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(401, response.getStatus()); + } + + // create index without table name + @Test + public void test3_createTableIndexNoName() throws Exception { + System.out.println("Testing index in create table w/o tablename"); + String tableNameC = "testTableCinx"; + JsonTable jsonTable = new JsonTable(); + Map consistencyInfo = new HashMap<>(); + Map fields = new HashMap<>(); + fields.put("uuid", "text"); + fields.put("emp_name", "text"); + fields.put("emp_salary", "varint"); + fields.put("PRIMARY KEY", "((emp_name),emp_salary)"); + consistencyInfo.put("type", "eventual"); + jsonTable.setConsistencyInfo(consistencyInfo); + jsonTable.setKeyspaceName(keyspaceName); + jsonTable.setTableName(tableNameC); + jsonTable.setClusteringOrder("emp_salary ASC"); + jsonTable.setFields(fields); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableNameC); + // if 200 print to log otherwise fail assertEquals(200, response.getStatus()); + // info.setQueryParameters("index_name=inx_uuid"); + Map queryParametersMap = new HashMap(); + + queryParametersMap.put("index_name", "inxuuid"); + response = data.createIndex("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, "", + "", "uuid", info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(400, response.getStatus()); + } + + @Test + public void test4_insertIntoTable() throws Exception { + System.out.println("Testing insert into table"); + createTable(); + JsonInsert jsonInsert = new JsonInsert(); + Map consistencyInfo = new HashMap<>(); + Map values = new HashMap<>(); + values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6"); + values.put("emp_name", "testname"); + values.put("emp_salary", 500); + consistencyInfo.put("type", "eventual"); + jsonInsert.setConsistencyInfo(consistencyInfo); + jsonInsert.setKeyspaceName(keyspaceName); + jsonInsert.setTableName(tableName); + jsonInsert.setValues(values); + Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonInsert, keyspaceName, tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(200, response.getStatus()); + } + + @Test + public void test4_insertIntoTableCriticalNoLockID() throws Exception { + System.out.println("Testing critical insert into table without lockid"); + createTable(); + JsonInsert jsonInsert = new JsonInsert(); Map consistencyInfo = new HashMap<>(); Map values = new HashMap<>(); values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6"); @@ -566,7 +588,29 @@ public class TstRestMusicDataAPI { assertEquals(400, response.getStatus()); } - @Test + @Test + public void test4_insertIntoTableAtomic() throws Exception { + System.out.println("Testing atomic insert into table without lockid"); + createTable(); + JsonInsert jsonInsert = new JsonInsert(); + Map consistencyInfo = new HashMap<>(); + Map values = new HashMap<>(); + values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6"); + values.put("emp_name", "testname"); + values.put("emp_salary", 500); + consistencyInfo.put("type", "atomic"); + jsonInsert.setConsistencyInfo(consistencyInfo); + jsonInsert.setKeyspaceName(keyspaceName); + jsonInsert.setTableName(tableName); + jsonInsert.setValues(values); + Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", + appName, authorization, jsonInsert, keyspaceName, tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(200, response.getStatus()); + } + + @Test public void test4_insertIntoTableNoName() throws Exception { System.out.println("Testing insert into table w/o table name"); createTable(); @@ -588,108 +632,102 @@ public class TstRestMusicDataAPI { assertEquals(400, response.getStatus()); } - @Test - public void test4_insertIntoTable2() throws Exception { - System.out.println("Testing insert into table #2"); - createTable(); - JsonInsert jsonInsert = new JsonInsert(); - Map consistencyInfo = new HashMap<>(); - Map 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); - Response response = data.insertIntoTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonInsert, keyspaceName, tableName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - assertEquals(200, response.getStatus()); - } + @Test + public void test4_insertIntoTable2() throws Exception { + System.out.println("Testing insert into table #2"); + createTable(); + JsonInsert jsonInsert = new JsonInsert(); + Map consistencyInfo = new HashMap<>(); + Map 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); + Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonInsert, keyspaceName, tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - // Auth Error - @Test - public void test4_insertIntoTable3() throws Exception { - System.out.println("Testing insert into table with bad credentials"); - createTable(); - JsonInsert jsonInsert = new JsonInsert(); - Map consistencyInfo = new HashMap<>(); - Map 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); - Response response = data.insertIntoTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, wrongAuthorization, - jsonInsert, keyspaceName, tableName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(200, response.getStatus()); + } - assertEquals(401, response.getStatus()); - } + // Auth Error + @Test + public void test4_insertIntoTable3() throws Exception { + System.out.println("Testing insert into table with bad credentials"); + createTable(); + JsonInsert jsonInsert = new JsonInsert(); + Map consistencyInfo = new HashMap<>(); + Map 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); + Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + wrongAuthorization, jsonInsert, keyspaceName, tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - // Table wrong - @Test - public void test4_insertIntoTable4() throws Exception { - System.out.println("Testing insert into wrong table"); - createTable(); - JsonInsert jsonInsert = new JsonInsert(); - Map consistencyInfo = new HashMap<>(); - Map 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); - Response response = data.insertIntoTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonInsert, keyspaceName, "wrong"); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } - assertEquals(400, response.getStatus()); - } + // Table wrong + @Test + public void test4_insertIntoTable4() throws Exception { + System.out.println("Testing insert into wrong table"); + createTable(); + JsonInsert jsonInsert = new JsonInsert(); + Map consistencyInfo = new HashMap<>(); + Map 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); + Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonInsert, keyspaceName, "wrong"); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } - @Test - public void test5_updateTable() throws Exception { - System.out.println("Testing update table"); - createTable(); - - JsonUpdate jsonUpdate = new JsonUpdate(); - Map consistencyInfo = new HashMap<>(); - MultivaluedMap row = new MultivaluedMapImpl(); - Map 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.when(info.getQueryParameters()).thenReturn(row); - Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorization, jsonUpdate, keyspaceName, tableName, info); - - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - assertEquals(200, response.getStatus()); - } - - @Test - public void test5_updateTableNoName() throws Exception { - System.out.println("Testing update table without tablename"); + @Test + public void test4_insertBlobIntoTable() throws Exception { + System.out.println("Testing insert a blob into table"); createTable(); - + JsonInsert jsonInsert = new JsonInsert(); + Map consistencyInfo = new HashMap<>(); + Map values = new HashMap<>(); + values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6"); + values.put("emp_name", "testname"); + values.put("emp_salary", 500); + values.put("binary", "somestuffhere"); + consistencyInfo.put("type", "eventual"); + jsonInsert.setConsistencyInfo(consistencyInfo); + jsonInsert.setKeyspaceName(keyspaceName); + jsonInsert.setTableName(tableName); + jsonInsert.setValues(values); + Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonInsert, keyspaceName, tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(200, response.getStatus()); + } + + @Test + public void test5_updateTable() throws Exception { + System.out.println("Testing update table"); + createTable(); + JsonUpdate jsonUpdate = new JsonUpdate(); Map consistencyInfo = new HashMap<>(); MultivaluedMap row = new MultivaluedMapImpl(); @@ -701,125 +739,169 @@ public class TstRestMusicDataAPI { jsonUpdate.setKeyspaceName(keyspaceName); jsonUpdate.setTableName(tableName); jsonUpdate.setValues(values); + Mockito.when(info.getQueryParameters()).thenReturn(row); Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorization, jsonUpdate, "", "", info); - + authorization, jsonUpdate, keyspaceName, tableName, info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - assertEquals(400, response.getStatus()); + + assertEquals(200, response.getStatus()); } + + @Test + public void test5_updateTable_wrongAuth() throws Exception { + System.out.println("Testing update table w/ wrong credentials"); + createTable(); - // need mock code to create error for MusicCore methods - @Test - public void test5_updateTableAuthE() throws Exception { - System.out.println("Testing update table #2"); - createTable(); - //MockitoAnnotations.initMocks(this); - JsonUpdate jsonUpdate = new JsonUpdate(); - Map consistencyInfo = new HashMap<>(); - MultivaluedMap row = new MultivaluedMapImpl(); - Map 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); - //add ttl & timestamp - //Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Mockito.when(info.getQueryParameters()).thenReturn(row); - //Map m1= new HashMap<>() ; - //Mockito.when(MusicCore.autheticateUser(appName,userId,password,keyspaceName,"abc66ccc-d857-4e90-b1e5-df98a3d40ce6","updateTable")).thenReturn(m1); - Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorization, jsonUpdate, keyspaceName, tableName, info); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + JsonUpdate jsonUpdate = new JsonUpdate(); + Map consistencyInfo = new HashMap<>(); + Map values = new HashMap<>(); + values.put("emp_salary", 2500); + consistencyInfo.put("type", "atomic"); + jsonUpdate.setConsistencyInfo(consistencyInfo); + jsonUpdate.setKeyspaceName(keyspaceName); + jsonUpdate.setTableName(tableName); + jsonUpdate.setValues(values); + Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + wrongAuthorization, jsonUpdate, keyspaceName, tableName, info); - assertEquals(200, response.getStatus()); - } + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - @Ignore - @Test - public void test5_updateTableAuthException1() throws Exception { - System.out.println("Testing update table authentication error"); - createTable(); - JsonUpdate jsonUpdate = new JsonUpdate(); - Map consistencyInfo = new HashMap<>(); - MultivaluedMap row = new MultivaluedMapImpl(); - Map 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.when(info.getQueryParameters()).thenReturn(row); - String authDatax = ":"; - String authorizationx = new String(Base64.encode(authDatax.getBytes())); - Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, - authorizationx, jsonUpdate, keyspaceName, tableName, info); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } - assertEquals(401, response.getStatus()); - } + @Test + public void test5_updateTable_tableDNE() throws Exception { + System.out.println("Testing update table that does not exist"); + createTable(); - @Ignore - @Test - public void test5_updateTableAuthEmpty() throws Exception { - System.out.println("Testing update table without authentication"); - createTable(); - - JsonUpdate jsonUpdate = new JsonUpdate(); - Map consistencyInfo = new HashMap<>(); - MultivaluedMap row = new MultivaluedMapImpl(); - Map 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); + JsonUpdate jsonUpdate = new JsonUpdate(); + Map consistencyInfo = new HashMap<>(); + Map values = new HashMap<>(); + values.put("emp_salary", 2500); + consistencyInfo.put("type", "atomic"); + jsonUpdate.setConsistencyInfo(consistencyInfo); + jsonUpdate.setKeyspaceName(keyspaceName); + jsonUpdate.setTableName("wrong_"+tableName); + jsonUpdate.setValues(values); + Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonUpdate, keyspaceName, "wrong_"+ tableName, info); - Mockito.when(info.getQueryParameters()).thenReturn(row); - String authDatax =":"+password; - String authorizationx = new String(Base64.encode(authDatax.getBytes())); - String appNamex="xx"; - Response response = data.updateTable("1", "1", "1", "", appNamex, - authorizationx, jsonUpdate, keyspaceName, tableName, info); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(401, response.getStatus()); - } + assertEquals(400, response.getStatus()); + } + + @Test + public void test5_updateTableNoName() throws Exception { + System.out.println("Testing update table without tablename"); + createTable(); + JsonUpdate jsonUpdate = new JsonUpdate(); + Map consistencyInfo = new HashMap<>(); + Map values = new HashMap<>(); + values.put("emp_salary", 2500); + consistencyInfo.put("type", "atomic"); + jsonUpdate.setConsistencyInfo(consistencyInfo); + jsonUpdate.setKeyspaceName(keyspaceName); + jsonUpdate.setTableName(tableName); + jsonUpdate.setValues(values); + Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonUpdate, "", "", info); - @Test - public void test6_select() throws Exception { - System.out.println("Testing select"); - createAndInsertIntoTable(); - JsonSelect jsonSelect = new JsonSelect(); - Map consistencyInfo = new HashMap<>(); - MultivaluedMap row = new MultivaluedMapImpl(); - row.add("emp_name", "testname"); - consistencyInfo.put("type", "atomic"); - jsonSelect.setConsistencyInfo(consistencyInfo); - Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.select("1", "1", "1","abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - appName, authorization, keyspaceName, tableName, info); - HashMap> map = (HashMap>) response.getEntity(); - HashMap result = map.get("result"); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - Map row0 = (Map) result.get("row 0"); - assertEquals("testname", row0.get("emp_name")); - assertEquals(BigInteger.valueOf(500), row0.get("emp_salary")); - } + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(400, response.getStatus()); + } + + // need mock code to create error for MusicCore methods + @Test + public void test5_updateTableAuthE() throws Exception { + System.out.println("Testing update table #2"); + createTable(); + // MockitoAnnotations.initMocks(this); + JsonUpdate jsonUpdate = new JsonUpdate(); + Map consistencyInfo = new HashMap<>(); + MultivaluedMap row = new MultivaluedMapImpl(); + Map 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); + // add ttl & timestamp + // Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); + Mockito.when(info.getQueryParameters()).thenReturn(row); + // Map m1= new HashMap<>() ; + // Mockito.when(MusicCore.autheticateUser(appName,userId,password,keyspaceName,"abc66ccc-d857-4e90-b1e5-df98a3d40ce6","updateTable")).thenReturn(m1); + Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonUpdate, keyspaceName, tableName, info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(200, response.getStatus()); + } + + @Ignore + @Test + public void test5_updateTableAuthException1() throws Exception { + System.out.println("Testing update table authentication error"); + createTable(); + JsonUpdate jsonUpdate = new JsonUpdate(); + Map consistencyInfo = new HashMap<>(); + MultivaluedMap row = new MultivaluedMapImpl(); + Map 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.when(info.getQueryParameters()).thenReturn(row); + String authDatax = ":"; + String authorizationx = new String(Base64.encode(authDatax.getBytes())); + Response response = data.updateTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorizationx, jsonUpdate, keyspaceName, tableName, info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(401, response.getStatus()); + } + + @Ignore + @Test + public void test5_updateTableAuthEmpty() throws Exception { + System.out.println("Testing update table without authentication"); + createTable(); + + JsonUpdate jsonUpdate = new JsonUpdate(); + Map consistencyInfo = new HashMap<>(); + MultivaluedMap row = new MultivaluedMapImpl(); + Map 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.when(info.getQueryParameters()).thenReturn(row); + String authDatax = ":" + password; + String authorizationx = new String(Base64.encode(authDatax.getBytes())); + String appNamex = "xx"; + Response response = data.updateTable("1", "1", "1", "", appNamex, authorizationx, jsonUpdate, keyspaceName, + tableName, info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(401, response.getStatus()); + } @Test - public void test6_selectCritical() throws Exception { - System.out.println("Testing select critical"); + public void test6_critical_selectAtomic() throws Exception { + System.out.println("Testing critical select atomic"); createAndInsertIntoTable(); JsonInsert jsonInsert = new JsonInsert(); Map consistencyInfo = new HashMap<>(); @@ -838,149 +920,268 @@ public class TstRestMusicDataAPI { assertEquals("testname", row0.get("emp_name")); assertEquals(BigInteger.valueOf(500), row0.get("emp_salary")); } - + @Test - public void test6_deleteFromTable() throws Exception { - System.out.println("Testing delete from table"); - createAndInsertIntoTable(); - JsonDelete jsonDelete = new JsonDelete(); - Map consistencyInfo = new HashMap<>(); - MultivaluedMap row = new MultivaluedMapImpl(); - row.add("emp_name", "test1"); - consistencyInfo.put("type", "atomic"); - jsonDelete.setConsistencyInfo(consistencyInfo); - Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.deleteFromTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonDelete, keyspaceName, tableName, info); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + public void test6_critical_selectCritical_nolockid() throws Exception { + System.out.println("Testing critical select critical w/o lockid"); + createAndInsertIntoTable(); + JsonInsert jsonInsert = new JsonInsert(); + Map consistencyInfo = new HashMap<>(); + MultivaluedMap row = new MultivaluedMapImpl(); + row.add("emp_name", "testname"); + consistencyInfo.put("type", "critical"); + jsonInsert.setConsistencyInfo(consistencyInfo); + Mockito.when(info.getQueryParameters()).thenReturn(row); + Response response = data.selectCritical("1", "1", "1","abc66ccc-d857-4e90-b1e5-df98a3d40ce6", + appName, authorization, jsonInsert, keyspaceName, tableName,info); + HashMap> map = (HashMap>) response.getEntity(); + HashMap result = map.get("result"); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(400, response.getStatus()); + } + + @Test + public void test6_critical_select_wrongAuth() throws Exception { + System.out.println("Testing critical select w/ wrong authentication"); + createAndInsertIntoTable(); + JsonInsert jsonInsert = new JsonInsert(); + Map consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "atomic"); + jsonInsert.setConsistencyInfo(consistencyInfo); + Response response = data.selectCritical("1", "1", "1","abc66ccc-d857-4e90-b1e5-df98a3d40ce6", + appName, wrongAuthorization, jsonInsert, keyspaceName, tableName,info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(401, response.getStatus()); + } + + @Test + public void test6_critical_select_nulltable() throws Exception { + System.out.println("Testing critical select w/ null tablename"); + createAndInsertIntoTable(); + JsonInsert jsonInsert = new JsonInsert(); + Map consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "atomic"); + jsonInsert.setConsistencyInfo(consistencyInfo); + Response response = data.selectCritical("1", "1", "1","abc66ccc-d857-4e90-b1e5-df98a3d40ce6", + appName, authorization, jsonInsert, keyspaceName, null,info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(400, response.getStatus()); + } - assertEquals(200, response.getStatus()); - } + @Test + public void test6_select() throws Exception { + System.out.println("Testing select"); + createAndInsertIntoTable(); + JsonSelect jsonSelect = new JsonSelect(); + Map consistencyInfo = new HashMap<>(); + MultivaluedMap row = new MultivaluedMapImpl(); + row.add("emp_name", "testname"); + consistencyInfo.put("type", "atomic"); + jsonSelect.setConsistencyInfo(consistencyInfo); + Mockito.when(info.getQueryParameters()).thenReturn(row); + Response response = data.select("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, + keyspaceName, tableName, info); + HashMap> map = (HashMap>) response.getEntity(); + HashMap result = map.get("result"); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - // Values - @Ignore - @Test - public void test6_deleteFromTable1() throws Exception { - System.out.println("Testing delete from table missing delete object"); - createAndInsertIntoTable(); - - JsonDelete jsonDelete = new JsonDelete(); - Map consistencyInfo = new HashMap<>(); - MultivaluedMap row = new MultivaluedMapImpl(); - consistencyInfo.put("type", "atomic"); - jsonDelete.setConsistencyInfo(consistencyInfo); - //Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Mockito.when(info.getQueryParameters()).thenReturn(row); - Response response = data.deleteFromTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - jsonDelete, keyspaceName, tableName, info); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + Map row0 = (Map) result.get("row 0"); + assertEquals("testname", row0.get("emp_name")); + assertEquals(BigInteger.valueOf(500), row0.get("emp_salary")); + } + + @Test + public void test6_select_wrongAuth() throws Exception { + System.out.println("Testing select w/ wrong authentication"); + createAndInsertIntoTable(); + JsonSelect jsonSelect = new JsonSelect(); + Map consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "atomic"); + jsonSelect.setConsistencyInfo(consistencyInfo); + Response response = data.select("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", + appName, wrongAuthorization, keyspaceName, tableName, info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(400, response.getStatus()); - } + assertEquals(401, response.getStatus()); + } + + @Test + public void test6_select_nullTablename() throws Exception { + System.out.println("Testing select w/ null tablename"); + createAndInsertIntoTable(); + JsonSelect jsonSelect = new JsonSelect(); + Map consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "atomic"); + jsonSelect.setConsistencyInfo(consistencyInfo); + Response response = data.select("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", + appName, wrongAuthorization, keyspaceName, null, info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - // delObj - @Test - public void test6_deleteFromTable2() throws Exception { - System.out.println("Testing delete from table missing delete object"); - createAndInsertIntoTable(); - JsonDelete jsonDelete = new JsonDelete(); - Map consistencyInfo = new HashMap<>(); - consistencyInfo.put("type", "atomic"); - jsonDelete.setConsistencyInfo(consistencyInfo); - Response response = data.deleteFromTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - null, keyspaceName, tableName, info); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - assertEquals(400, response.getStatus()); - } + assertEquals(400, response.getStatus()); + } - @Test - public void test7_dropTable() throws Exception { - System.out.println("Testing drop table"); - createTable(); - JsonTable jsonTable = new JsonTable(); - Map consistencyInfo = new HashMap<>(); - consistencyInfo.put("type", "atomic"); - jsonTable.setConsistencyInfo(consistencyInfo); - Response response = data.dropTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, authorization, - keyspaceName, tableName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - - assertEquals(200, response.getStatus()); - } + @Test + public void test6_deleteFromTable() throws Exception { + System.out.println("Testing delete from table"); + createAndInsertIntoTable(); + JsonDelete jsonDelete = new JsonDelete(); + Map consistencyInfo = new HashMap<>(); + MultivaluedMap row = new MultivaluedMapImpl(); + row.add("emp_name", "test1"); + consistencyInfo.put("type", "atomic"); + jsonDelete.setConsistencyInfo(consistencyInfo); + Mockito.when(info.getQueryParameters()).thenReturn(row); + Response response = data.deleteFromTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonDelete, keyspaceName, tableName, info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(200, response.getStatus()); + } + + @Test + public void test6_deleteFromTable_wrongAuth() throws Exception { + System.out.println("Testing delete from table"); + createAndInsertIntoTable(); + JsonDelete jsonDelete = new JsonDelete(); + Map consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "atomic"); + jsonDelete.setConsistencyInfo(consistencyInfo); + Response response = data.deleteFromTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + wrongAuthorization, jsonDelete, keyspaceName, tableName, info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } + @Test + public void test6_deleteFromTable_missingTablename() throws Exception { + System.out.println("Testing delete from table w/ null tablename"); + createAndInsertIntoTable(); + JsonDelete jsonDelete = new JsonDelete(); + Map consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "atomic"); + jsonDelete.setConsistencyInfo(consistencyInfo); + Response response = data.deleteFromTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + wrongAuthorization, jsonDelete, keyspaceName, null, info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } + + // Values + @Ignore + @Test + public void test6_deleteFromTable1() throws Exception { + System.out.println("Testing delete from table missing delete object"); + createAndInsertIntoTable(); - @Test - public void test8_deleteKeyspace() throws Exception { - System.out.println("Testing drop keyspace"); + JsonDelete jsonDelete = new JsonDelete(); + Map consistencyInfo = new HashMap<>(); + MultivaluedMap row = new MultivaluedMapImpl(); + consistencyInfo.put("type", "atomic"); + jsonDelete.setConsistencyInfo(consistencyInfo); + Mockito.when(info.getQueryParameters()).thenReturn(row); + Response response = data.deleteFromTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonDelete, keyspaceName, tableName, info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - JsonKeySpace jsonKeyspace = new JsonKeySpace(); - Map consistencyInfo = new HashMap<>(); - Map replicationInfo = new HashMap<>(); - consistencyInfo.put("type", "eventual"); - replicationInfo.put("class", "SimpleStrategy"); - replicationInfo.put("replication_factor", 1); - jsonKeyspace.setConsistencyInfo(consistencyInfo); - jsonKeyspace.setDurabilityOfWrites("true"); - jsonKeyspace.setKeyspaceName("TestApp1"); - jsonKeyspace.setReplicationInfo(replicationInfo); - Response response = data.dropKeySpace("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", authorization,appName, keyspaceName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } - assertEquals(400, response.getStatus()); - } - - private static void createAdminTable() throws Exception { - testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQL.createAdminKeyspace); - MusicCore.eventualPut(testObject); - testObject = new PreparedQueryObject(); - testObject.appendQueryString(CassandraCQL.createAdminTable); - MusicCore.eventualPut(testObject); - - testObject = new PreparedQueryObject(); - testObject.appendQueryString( - "INSERT INTO admin.keyspace_master (uuid, keyspace_name, application_name, is_api, " - + "password, username, is_aaf) VALUES (?,?,?,?,?,?,?)"); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid)); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), - MusicUtil.DEFAULTKEYSPACENAME)); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True")); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), BCrypt.hashpw(password, BCrypt.gensalt()))); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF)); - MusicCore.eventualPut(testObject); - - testObject = new PreparedQueryObject(); - testObject.appendQueryString( - "select uuid from admin.keyspace_master where application_name = ? allow filtering"); - testObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); - ResultSet rs = MusicCore.get(testObject); - List rows = rs.all(); - if (rows.size() > 0) { - System.out.println("#######UUID is:" + rows.get(0).getUUID("uuid")); - } - } - + // delObj + @Test + public void test6_deleteFromTable2() throws Exception { + System.out.println("Testing delete from table missing delete object"); + createAndInsertIntoTable(); + JsonDelete jsonDelete = new JsonDelete(); + Map consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "atomic"); + jsonDelete.setConsistencyInfo(consistencyInfo); + Response response = data.deleteFromTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, null, keyspaceName, tableName, info); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - private static void createKeyspace() throws Exception { - //shouldn't really be doing this here, but create keyspace is currently turned off - PreparedQueryObject query = new PreparedQueryObject(); - query.appendQueryString(CassandraCQL.createKeySpace); - MusicCore.eventualPut(query); - - boolean isAAF = false; + assertEquals(400, response.getStatus()); + } + + @Test + public void test7_dropTable() throws Exception { + System.out.println("Testing drop table"); + createTable(); + JsonTable jsonTable = new JsonTable(); + Map consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "atomic"); + jsonTable.setConsistencyInfo(consistencyInfo); + Response response = data.dropTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, keyspaceName, tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(200, response.getStatus()); + } + + @Test + public void test7_dropTable_wrongAuth() throws Exception { + System.out.println("Testing drop table w/ wrong auth"); + createTable(); + JsonTable jsonTable = new JsonTable(); + Map consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "atomic"); + jsonTable.setConsistencyInfo(consistencyInfo); + Response response = data.dropTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + wrongAuthorization, keyspaceName, tableName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(401, response.getStatus()); + } + + @Test + public void test7_dropTable_nullTablename() throws Exception { + System.out.println("Testing drop table w/ null tablename"); + createTable(); + JsonTable jsonTable = new JsonTable(); + Map consistencyInfo = new HashMap<>(); + consistencyInfo.put("type", "atomic"); + jsonTable.setConsistencyInfo(consistencyInfo); + Response response = data.dropTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, keyspaceName, null); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(400, response.getStatus()); + } + + + @Test + public void test8_deleteKeyspace() throws Exception { + System.out.println("Testing drop keyspace"); + + JsonKeySpace jsonKeyspace = new JsonKeySpace(); + Map consistencyInfo = new HashMap<>(); + Map replicationInfo = new HashMap<>(); + consistencyInfo.put("type", "eventual"); + replicationInfo.put("class", "SimpleStrategy"); + replicationInfo.put("replication_factor", 1); + jsonKeyspace.setConsistencyInfo(consistencyInfo); + jsonKeyspace.setDurabilityOfWrites("true"); + jsonKeyspace.setKeyspaceName("TestApp1"); + jsonKeyspace.setReplicationInfo(replicationInfo); + Response response = data.dropKeySpace("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", authorization, + appName, keyspaceName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(400, response.getStatus()); + } + + private static void createKeyspace() throws Exception { + // shouldn't really be doing this here, but create keyspace is currently turned off + PreparedQueryObject query = new PreparedQueryObject(); + query.appendQueryString(CassandraCQL.createKeySpace); + MusicCore.eventualPut(query); + + boolean isAAF = false; String hashedpwd = BCrypt.hashpw(password, BCrypt.gensalt()); query = new PreparedQueryObject(); - query.appendQueryString( - "INSERT into admin.keyspace_master (uuid, keyspace_name, application_name, is_api, " - + "password, username, is_aaf) values (?,?,?,?,?,?,?)"); + query.appendQueryString("INSERT into admin.keyspace_master (uuid, keyspace_name, application_name, is_api, " + + "password, username, is_aaf) values (?,?,?,?,?,?,?)"); query.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid)); query.addValue(MusicUtil.convertToActualDataType(DataType.text(), keyspaceName)); query.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); @@ -991,66 +1192,70 @@ public class TstRestMusicDataAPI { CachingUtil.updateMusicCache(keyspaceName, appName); CachingUtil.updateMusicValidateCache(appName, userId, hashedpwd); MusicCore.eventualPut(query); - } - - private void clearAllTablesFromKeyspace() throws MusicServiceException { - ArrayList tableNames = new ArrayList<>(); - PreparedQueryObject query = new PreparedQueryObject(); - query.appendQueryString("SELECT table_name FROM system_schema.tables WHERE keyspace_name = '"+keyspaceName+"';"); - ResultSet rs = MusicCore.get(query); - for (Row row: rs) { - tableNames.add(row.getString("table_name")); - } - for (String table: tableNames) { - query = new PreparedQueryObject(); - query.appendQueryString("DROP TABLE " + keyspaceName + "." + table); - MusicCore.eventualPut(query); - } - } - - /** - * Create a table {@link tableName} in {@link keyspaceName} - * @throws Exception - */ - private void createTable() throws Exception { - JsonTable jsonTable = new JsonTable(); - Map consistencyInfo = new HashMap<>(); - Map 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.setPrimaryKey("emp_name"); - jsonTable.setTableName(tableName); - jsonTable.setFields(fields); - //Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); - Response response = data.createTable("1", "1", "1", - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6",appName, authorization, - jsonTable, keyspaceName, tableName); - } - - /** - * Create table {@link createTable} and insert into said table - * @throws Exception - */ - private void createAndInsertIntoTable() throws Exception { - createTable(); - - JsonInsert jsonInsert = new JsonInsert(); - Map consistencyInfo = new HashMap<>(); - Map values = new HashMap<>(); - values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6"); - values.put("emp_name", "testname"); - values.put("emp_salary", 500); - consistencyInfo.put("type", "eventual"); - jsonInsert.setConsistencyInfo(consistencyInfo); - jsonInsert.setKeyspaceName(keyspaceName); - jsonInsert.setTableName(tableName); - jsonInsert.setValues(values); - Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", - appName, authorization, jsonInsert, keyspaceName, tableName); - } + } + + private void clearAllTablesFromKeyspace() throws MusicServiceException { + ArrayList tableNames = new ArrayList<>(); + PreparedQueryObject query = new PreparedQueryObject(); + query.appendQueryString( + "SELECT table_name FROM system_schema.tables WHERE keyspace_name = '" + keyspaceName + "';"); + ResultSet rs = MusicCore.get(query); + for (Row row : rs) { + tableNames.add(row.getString("table_name")); + } + for (String table : tableNames) { + query = new PreparedQueryObject(); + query.appendQueryString("DROP TABLE " + keyspaceName + "." + table); + MusicCore.eventualPut(query); + } + } + + /** + * Create a table {@link tableName} in {@link keyspaceName} + * + * @throws Exception + */ + private void createTable() throws Exception { + JsonTable jsonTable = new JsonTable(); + Map consistencyInfo = new HashMap<>(); + Map fields = new HashMap<>(); + fields.put("uuid", "text"); + fields.put("emp_name", "text"); + fields.put("emp_salary", "varint"); + fields.put("binary", "blob"); + fields.put("PRIMARY KEY", "(emp_name)"); + consistencyInfo.put("type", "eventual"); + jsonTable.setConsistencyInfo(consistencyInfo); + jsonTable.setKeyspaceName(keyspaceName); + jsonTable.setPrimaryKey("emp_name"); + jsonTable.setTableName(tableName); + jsonTable.setFields(fields); + // Mockito.doNothing().when(http).addHeader(xLatestVersion, MusicUtil.getVersion()); + Response response = data.createTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonTable, keyspaceName, tableName); + } + + /** + * Create table {@link createTable} and insert into said table + * + * @throws Exception + */ + private void createAndInsertIntoTable() throws Exception { + createTable(); + + JsonInsert jsonInsert = new JsonInsert(); + Map consistencyInfo = new HashMap<>(); + Map values = new HashMap<>(); + values.put("uuid", "cfd66ccc-d857-4e90-b1e5-df98a3d40cd6"); + values.put("emp_name", "testname"); + values.put("emp_salary", 500); + values.put("binary", "binarydatahere"); + consistencyInfo.put("type", "eventual"); + jsonInsert.setConsistencyInfo(consistencyInfo); + jsonInsert.setKeyspaceName(keyspaceName); + jsonInsert.setTableName(tableName); + jsonInsert.setValues(values); + Response response = data.insertIntoTable("1", "1", "1", "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName, + authorization, jsonInsert, keyspaceName, tableName); + } } diff --git a/src/test/java/org/onap/music/unittests/TstRestMusicLockAPI.java b/src/test/java/org/onap/music/unittests/TstRestMusicLockAPI.java index 7e45e160..92c5d818 100644 --- a/src/test/java/org/onap/music/unittests/TstRestMusicLockAPI.java +++ b/src/test/java/org/onap/music/unittests/TstRestMusicLockAPI.java @@ -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============================================= * ==================================================================== @@ -36,6 +40,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mindrot.jbcrypt.BCrypt; import org.mockito.junit.MockitoJUnitRunner; +import org.onap.music.authentication.CachingUtil; import org.onap.music.datastore.MusicDataStoreHandle; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.datastore.jsonobjects.JsonInsert; @@ -43,7 +48,6 @@ import org.onap.music.datastore.jsonobjects.JsonLeasedLock; import org.onap.music.datastore.jsonobjects.JsonTable; import org.onap.music.exceptions.MusicServiceException; import org.onap.music.lockingservice.cassandra.CassaLockStore; -import org.onap.music.main.CachingUtil; import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.rest.RestMusicDataAPI; @@ -73,6 +77,7 @@ public class TstRestMusicLockAPI { static String tableName = "employees"; static String onboardUUID = null; static String lockName = "testcassa.employees.testname"; + static String malformedLock = "malformedLock"; @BeforeClass public static void init() throws Exception { @@ -111,6 +116,28 @@ public class TstRestMusicLockAPI { assertTrue(respMap.containsKey("lock")); assertTrue(((Map) respMap.get("lock")).containsKey("lock")); } + + @Test + public void test_createLockReference_wrongAuth() throws Exception { + System.out.println("Testing create lockref with wrong auth"); + createAndInsertIntoTable(); + Response response =lock.createLockReference(lockName,"1","1", wrongAuthorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(401, response.getStatus()); + } + + @Test + public void test_createLockReference_malformedLockName() throws Exception { + System.out.println("Testing create lockref w/ malformed lock"); + createAndInsertIntoTable(); + Response response =lock.createLockReference(malformedLock,"1","1",authorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + + assertEquals(400, response.getStatus()); + } @Test public void test_accquireLock() throws Exception { @@ -124,6 +151,44 @@ public class TstRestMusicLockAPI { assertEquals(200, response.getStatus()); } + @Test + public void test_accquireLock_wrongAuth() throws Exception { + System.out.println("Testing acquire lock w/ wrong auth"); + createAndInsertIntoTable(); + String lockRef = createLockReference(); + + Response response = lock.accquireLock(lockRef, "1", "1", wrongAuthorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } + + @Test + public void test_accquireBadLock() throws Exception { + System.out.println("Testing acquire lock that is not lock-holder"); + createAndInsertIntoTable(); + + String lockRef1 = createLockReference(); + String lockRef2 = createLockReference(); + + + Response response = lock.accquireLock(lockRef2, "1", "1", authorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } + + @Test + public void test_accquireLock_malformedLock() throws Exception { + System.out.println("Testing acquire lock w/ malformedLock"); + createAndInsertIntoTable(); + + Response response = lock.accquireLock(malformedLock, "1", "1", wrongAuthorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } + @Test public void test_accquireLockWLease() throws Exception { System.out.println("Testing acquire lock with lease"); @@ -139,18 +204,31 @@ public class TstRestMusicLockAPI { } @Test - public void test_accquireBadLock() throws Exception { - System.out.println("Testing acquire lock that is not lock-holder"); - createAndInsertIntoTable(); - - String lockRef1 = createLockReference(); - String lockRef2 = createLockReference(); + public void test_accquireLockWLease_wrongAuth() throws Exception { + System.out.println("Testing acquire lock with lease w/ wrong Auth"); + createAndInsertIntoTable(); + String lockRef = createLockReference(); + JsonLeasedLock jsonLock = new JsonLeasedLock(); + jsonLock.setLeasePeriod(10000); //10 second lease period? + Response response = lock.accquireLockWithLease(jsonLock, lockRef, "1", "1", wrongAuthorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } + + @Test + public void test_accquireLockWLease_malformedLock() throws Exception { + System.out.println("Testing acquire lock with lease w/ malformed lock"); + createAndInsertIntoTable(); + String lockRef = createLockReference(); - Response response = lock.accquireLock(lockRef2, "1", "1", authorization, - "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); - System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); - assertEquals(400, response.getStatus()); + JsonLeasedLock jsonLock = new JsonLeasedLock(); + jsonLock.setLeasePeriod(10000); //10 second lease period? + Response response = lock.accquireLockWithLease(jsonLock, malformedLock, "1", "1", + wrongAuthorization, "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); } @Test @@ -168,6 +246,32 @@ public class TstRestMusicLockAPI { assertEquals(lockRef, ((Map) respMap.get("lock")).get("lock-holder")); } + @Test + public void test_currentLockHolder_wrongAuth() throws Exception { + System.out.println("Testing get current lock holder w/ wrong Auth"); + createAndInsertIntoTable(); + + String lockRef = createLockReference(); + + Response response = lock.currentLockHolder(lockName, "1", "1", wrongAuthorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } + + @Test + public void test_currentLockHolder_malformedLock() throws Exception { + System.out.println("Testing get current lock holder w/ malformed lock"); + createAndInsertIntoTable(); + + String lockRef = createLockReference(); + + Response response = lock.currentLockHolder(malformedLock, "1", "1", wrongAuthorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } + @Test public void test_unLock() throws Exception { System.out.println("Testing unlock"); @@ -180,6 +284,30 @@ public class TstRestMusicLockAPI { assertEquals(200, response.getStatus()); } + @Test + public void test_unLock_wrongAuth() throws Exception { + System.out.println("Testing unlock w/ wrong auth"); + createAndInsertIntoTable(); + String lockRef = createLockReference(); + + Response response = lock.unLock(lockRef, "1", "1", wrongAuthorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } + + @Test + public void test_unLock_malformedLock() throws Exception { + System.out.println("Testing unlock w/ malformedLock"); + createAndInsertIntoTable(); + String lockRef = createLockReference(); + + Response response = lock.unLock("malformedLock", "1", "1", wrongAuthorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } + @Test public void test_getLockState() throws Exception { System.out.println("Testing get lock state"); @@ -195,9 +323,35 @@ public class TstRestMusicLockAPI { assertEquals(lockRef, ((Map) respMap.get("lock")).get("lock-holder")); } + @Test + public void test_getLockState_wrongAuth() throws Exception { + System.out.println("Testing get lock state w/ wrong auth"); + createAndInsertIntoTable(); + + String lockRef = createLockReference(); + + Response response = lock.currentLockState(lockName, "1", "1", wrongAuthorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } + + @Test + public void test_getLockState_malformedLock() throws Exception { + System.out.println("Testing get lock state w/ malformedLock"); + createAndInsertIntoTable(); + + String lockRef = createLockReference(); + + Response response = lock.currentLockState(malformedLock, "1", "1", wrongAuthorization, + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } + @Test public void test_deleteLock() throws Exception { - System.out.println("Testing get lock state"); + System.out.println("Testing delete lock"); createAndInsertIntoTable(); String lockRef = createLockReference(); @@ -207,6 +361,32 @@ public class TstRestMusicLockAPI { System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); assertEquals(200, response.getStatus()); } + + @Test + public void test_deleteLock_wrongAuth() throws Exception { + System.out.println("Testing delete lock w/ wrong auth"); + createAndInsertIntoTable(); + + String lockRef = createLockReference(); + + Response response = lock.deleteLock(lockName, "1", "1", + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", wrongAuthorization, appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(401, response.getStatus()); + } + + @Test + public void test_deleteLock_malformedLock() throws Exception { + System.out.println("Testing delete lock w/ malformed lock"); + createAndInsertIntoTable(); + + String lockRef = createLockReference(); + + Response response = lock.deleteLock(malformedLock, "1", "1", + "abc66ccc-d857-4e90-b1e5-df98a3d40ce6", wrongAuthorization, appName); + System.out.println("Status: " + response.getStatus() + ". Entity " + response.getEntity()); + assertEquals(400, response.getStatus()); + } /** * Create table and lock reference diff --git a/src/test/java/org/onap/music/unittests/jsonobjects/JsonTableTest.java b/src/test/java/org/onap/music/unittests/jsonobjects/JsonTableTest.java index 2279cf0b..4e3b4629 100644 --- a/src/test/java/org/onap/music/unittests/jsonobjects/JsonTableTest.java +++ b/src/test/java/org/onap/music/unittests/jsonobjects/JsonTableTest.java @@ -77,13 +77,6 @@ public class JsonTableTest { assertEquals(table,jt.getTableName()); } - @Test - public void testGetSortingKey() { - String sortKey = "sortkey"; - jt.setSortingKey(sortKey); - assertEquals(sortKey,jt.getSortingKey()); - } - @Test public void testGetClusteringOrder() { String clusteringOrder = "clusteringOrder"; diff --git a/src/test/java/org/onap/music/unittests/jsonobjects/MusicHealthCheckTest.java b/src/test/java/org/onap/music/unittests/jsonobjects/MusicHealthCheckTest.java index f48ecd40..ceda3f3a 100644 --- a/src/test/java/org/onap/music/unittests/jsonobjects/MusicHealthCheckTest.java +++ b/src/test/java/org/onap/music/unittests/jsonobjects/MusicHealthCheckTest.java @@ -44,11 +44,5 @@ public class MusicHealthCheckTest { musicHealthCheck.setCassandrHost("9042"); assertEquals("9042", musicHealthCheck.getCassandrHost()); } - - @Test - public void testZookeeperHost() - { - musicHealthCheck.setZookeeperHost("ZookeeperHost"); - assertEquals("ZookeeperHost", musicHealthCheck.getZookeeperHost()); - } + }