Changes Listed below:
[music.git] / src / test / java / org / onap / music / unittests / TstCachingUtil.java
1 /*
2  * ============LICENSE_START========================================== org.onap.music
3  * =================================================================== Copyright (c) 2017 AT&T
4  * Intellectual Property ===================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6  * in compliance with the License. You may obtain a copy of the License at
7  * 
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  * 
15  * ============LICENSE_END=============================================
16  * ====================================================================
17  */
18
19 package org.onap.music.unittests;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertTrue;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.UUID;
29
30 import javax.ws.rs.core.Response;
31
32 import org.junit.After;
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Ignore;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mindrot.jbcrypt.BCrypt;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.onap.music.authentication.CachingUtil;
41 import org.onap.music.datastore.MusicDataStoreHandle;
42 import org.onap.music.datastore.PreparedQueryObject;
43 import org.onap.music.datastore.jsonobjects.JsonInsert;
44 import org.onap.music.datastore.jsonobjects.JsonLeasedLock;
45 import org.onap.music.datastore.jsonobjects.JsonTable;
46 import org.onap.music.exceptions.MusicServiceException;
47 import org.onap.music.lockingservice.cassandra.CassaLockStore;
48 import org.onap.music.main.MusicCore;
49 import org.onap.music.main.MusicUtil;
50 import org.onap.music.rest.RestMusicDataAPI;
51 import org.onap.music.rest.RestMusicLocksAPI;
52 import com.datastax.driver.core.DataType;
53 import com.datastax.driver.core.ResultSet;
54 import com.datastax.driver.core.Row;
55 import com.sun.jersey.core.util.Base64;
56
57 @RunWith(MockitoJUnitRunner.class)
58 public class TstCachingUtil {
59
60         RestMusicLocksAPI lock = new RestMusicLocksAPI();
61         RestMusicDataAPI data = new RestMusicDataAPI();
62         static PreparedQueryObject testObject;
63
64         static String appName = TestsUsingCassandra.appName;
65         static String userId = TestsUsingCassandra.userId;
66         static String password = TestsUsingCassandra.password;
67         static String authData = TestsUsingCassandra.authData;
68         static String authorization = TestsUsingCassandra.authorization;
69         static boolean isAAF = TestsUsingCassandra.isAAF;
70         static UUID uuid = TestsUsingCassandra.uuid;
71         static String keyspaceName = TestsUsingCassandra.keyspaceName;
72         static String tableName = TestsUsingCassandra.tableName;
73         static String onboardUUID = null;
74
75         @BeforeClass
76         public static void init() throws Exception {
77                 System.out.println("Testing CachingUtil class");
78                 try {
79                         
80                 } catch (Exception e) {
81                         e.printStackTrace();
82                         throw new Exception("Unable to initialize before TestRestMusicData test class. " + e.getMessage());
83                 }
84         }
85         
86         @After
87         public void afterEachTest( ) throws MusicServiceException {
88
89         }
90
91         @AfterClass
92         public static void tearDownAfterClass() throws Exception {
93                 testObject = new PreparedQueryObject();
94                 testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName);
95                 MusicCore.eventualPut(testObject);
96         }
97
98         @Test
99     public void test_verifyOnboard() throws Exception {
100             System.out.println("Testing onboarding of app");
101         CachingUtil cache = new CachingUtil();
102         cache.initializeAafCache();
103         Map<String, Object> authMap = CachingUtil.verifyOnboarding(appName, userId, password);
104         assertEquals(0, authMap.size());
105     }
106     
107     @Test
108     public void test_authenticateAIDUser() throws Exception {
109         System.out.println("Testing authentication of AID user");
110         createKeyspace();
111         CachingUtil cache = new CachingUtil();
112         cache.initializeAafCache();
113         Map<String, Object> authMap = CachingUtil.authenticateAIDUser(appName,
114                 userId, password, keyspaceName);
115         System.out.println("authMap is: " + authMap);
116         assertEquals(0, authMap.size());
117     }
118     
119     @Test
120     public void test_getAppName() throws MusicServiceException {
121         System.out.println("Testing getAppName");
122         CachingUtil cache = new CachingUtil();
123         cache.initializeAafCache();
124         assertEquals(appName, CachingUtil.getAppName(keyspaceName));
125     }
126
127     @Test
128     public void test_getUUIDFromCache() throws MusicServiceException {
129         System.out.println("Testing getUUID");
130         CachingUtil cache = new CachingUtil();
131         cache.initializeAafCache();
132         assertEquals(uuid.toString(), CachingUtil.getUuidFromMusicCache(keyspaceName));
133     }
134     
135     @Test
136     public void test_isAAFApplcation() throws MusicServiceException {
137         System.out.println("Testing to see if cache gets correct isAAF info");
138         CachingUtil cache = new CachingUtil();
139         cache.initializeAafCache();
140         assertEquals(isAAF, Boolean.valueOf(CachingUtil.isAAFApplication(appName)));
141     }
142
143     private static void createKeyspace() throws Exception {
144         //shouldn't really be doing this here, but create keyspace is currently turned off
145         PreparedQueryObject query = new PreparedQueryObject();
146         query.appendQueryString(CassandraCQL.createKeySpace);
147         MusicCore.eventualPut(query);
148         
149         boolean isAAF = false;
150         String hashedpwd = BCrypt.hashpw(password, BCrypt.gensalt());
151         query = new PreparedQueryObject();
152         query.appendQueryString(
153                     "INSERT into admin.keyspace_master (uuid, keyspace_name, application_name, is_api, "
154                                     + "password, username, is_aaf) values (?,?,?,?,?,?,?)");
155         query.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
156         query.addValue(MusicUtil.convertToActualDataType(DataType.text(), keyspaceName));
157         query.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
158         query.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True"));
159         query.addValue(MusicUtil.convertToActualDataType(DataType.text(), hashedpwd));
160         query.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
161         query.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
162         CachingUtil.updateMusicCache(keyspaceName, appName);
163         CachingUtil.updateMusicValidateCache(appName, userId, hashedpwd);
164         MusicCore.eventualPut(query);
165     }
166
167 }