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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
15 * ============LICENSE_END=============================================
16 * ====================================================================
19 package org.onap.music.unittests;
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;
28 import java.util.UUID;
30 import javax.ws.rs.core.Response;
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;
57 @RunWith(MockitoJUnitRunner.class)
58 public class TstCachingUtil {
60 RestMusicLocksAPI lock = new RestMusicLocksAPI();
61 RestMusicDataAPI data = new RestMusicDataAPI();
62 static PreparedQueryObject testObject;
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;
76 public static void init() throws Exception {
77 System.out.println("Testing CachingUtil class");
80 } catch (Exception e) {
82 throw new Exception("Unable to initialize before TestRestMusicData test class. " + e.getMessage());
87 public void afterEachTest( ) throws MusicServiceException {
92 public static void tearDownAfterClass() throws Exception {
93 testObject = new PreparedQueryObject();
94 testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName);
95 MusicCore.eventualPut(testObject);
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());
108 public void test_authenticateAIDUser() throws Exception {
109 System.out.println("Testing authentication of AID user");
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());
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));
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));
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)));
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);
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);