Merge "RestMusicQAPI.java - Sonar Fixes"
[music.git] / src / test / java / org / onap / music / unittests / TstRestMusicLockAPI.java
index 7e45e16..92c5d81 100644 (file)
@@ -1,16 +1,20 @@
 /*
- * ============LICENSE_START========================================== org.onap.music
- * =================================================================== Copyright (c) 2017 AT&T
- * Intellectual Property ===================================================================
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
- * in compliance with the License. You may obtain a copy of the License at
+ * ============LICENSE_START==========================================
+ * org.onap.music
+ * ===================================================================
+ *  Copyright (c) 2017 AT&T Intellectual Property
+ * ===================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
  * 
- * http://www.apache.org/licenses/LICENSE-2.0
+ *     http://www.apache.org/licenses/LICENSE-2.0
  * 
- * Unless required by applicable law or agreed to in writing, software distributed under the License
- * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
- * or implied. See the License for the specific language governing permissions and limitations under
- * the License.
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
  * 
  * ============LICENSE_END=============================================
  * ====================================================================
@@ -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<String,String>) 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<String,String>) 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<String,String>) 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