be2783b2d1760be43dda34a077315e74c8a66e28
[music.git] / src / test / java / org / onap / music / unittests / TstCachingUtil.java
1 /*
2  * ============LICENSE_START========================================== org.onap.music
3  * =================================================================== Copyright (c) 2017 AT&T Intellectual Property
4  * =================================================================== Licensed under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the
6  * 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 is distributed on
11  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
12  * specific language governing permissions and limitations under the License.
13  * 
14  * ============LICENSE_END=============================================
15  * ====================================================================
16  */
17
18 package org.onap.music.unittests;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23 import java.util.UUID;
24 import org.junit.AfterClass;
25 import org.junit.BeforeClass;
26 import org.junit.Ignore;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mindrot.jbcrypt.BCrypt;
30 import org.mockito.junit.MockitoJUnitRunner;
31 import org.onap.music.datastore.PreparedQueryObject;
32 import org.onap.music.main.CachingUtil;
33 import org.onap.music.main.MusicCore;
34 import org.onap.music.main.MusicUtil;
35 import org.onap.music.rest.RestMusicDataAPI;
36 import com.datastax.driver.core.DataType;
37 import com.sun.jersey.core.util.Base64;
38
39 public class TstCachingUtil {
40
41     static PreparedQueryObject testObject;
42
43     static String appName = "TestApp";
44     static String userId = "TestUser";
45     static String password = "TestPassword";
46     static String authData = userId + ":" + password;
47     static String wrongAuthData = userId + ":" + "pass";
48     static String authorization = new String(Base64.encode(authData.getBytes()));
49     static String wrongAuthorization = new String(Base64.encode(wrongAuthData.getBytes()));
50     static boolean isAAF = false;
51     static UUID uuid = UUID.fromString("abc66ccc-d857-4e90-b1e5-df98a3d40ce6");
52     static String keyspaceName = "testcassa";
53     static String tableName = "employees";
54     static String xLatestVersion = "X-latestVersion";
55     static String onboardUUID = null;
56
57     @BeforeClass
58     public static void init() throws Exception {
59         System.out.println("Testing CachingUtil class");
60         try {
61             createKeyspace();
62         } catch (Exception e) {
63             e.printStackTrace();
64             throw new Exception("Unable to initialize before CachingUtil test class. " + e.getMessage());
65         }
66     }
67
68     @AfterClass
69     public static void tearDownAfterClass() throws Exception {
70         testObject = new PreparedQueryObject();
71         testObject.appendQueryString("DROP KEYSPACE IF EXISTS " + keyspaceName);
72         MusicCore.eventualPut(testObject);
73     }
74
75     @Test
76     public void test_isAAF_False() throws Exception {
77         System.out.println("Testing isAAF, nonaaf app");
78         assertEquals("false", CachingUtil.isAAFApplication(appName));
79     }
80
81     @Test
82     public void test_getUUidFromMusicCache() throws Exception {
83         System.out.println("Testing getUUID from MusicCache");
84         assertEquals(uuid.toString(), CachingUtil.getUuidFromMusicCache(keyspaceName));
85     }
86
87     @Test
88     public void test_getAppName() throws Exception {
89         System.out.println("Testing getAppName");
90         assertEquals(appName, CachingUtil.getAppName(keyspaceName));
91     }
92
93     @Test
94     public void test_verifyOnboarding() throws Exception {
95         System.out.println("Testing verifyOnboarding");
96         assertTrue(CachingUtil.verifyOnboarding(appName, userId, password).isEmpty());
97     }
98
99     @Test
100     public void test_verifyOnboardingFailure() throws Exception {
101         System.out.println("Testing verifyOnboarding with bad password");
102         assertFalse(CachingUtil.verifyOnboarding(appName, userId, "pass").isEmpty());
103     }
104
105
106     @Test
107     public void test_authenticateAIDUser() throws Exception {
108         System.out.println("Testing authenticateAIDUser");
109         assertTrue(CachingUtil.authenticateAIDUser(appName, userId, password, keyspaceName).isEmpty());
110     }
111
112     @Test
113     public void test_authenticateAIDUserFailure() throws Exception {
114         System.out.println("Testing authenticateAIDUser bad password");
115         assertFalse(CachingUtil.authenticateAIDUser(appName, userId, "pass", keyspaceName).isEmpty());
116     }
117
118     private static void createKeyspace() throws Exception {
119         // shouldn't really be doing this here, but create keyspace is currently turned off
120         PreparedQueryObject query = new PreparedQueryObject();
121         query.appendQueryString(CassandraCQL.createKeySpace);
122         MusicCore.eventualPut(query);
123
124         boolean isAAF = false;
125         String hashedpwd = BCrypt.hashpw(password, BCrypt.gensalt());
126         query = new PreparedQueryObject();
127         query.appendQueryString("INSERT into admin.keyspace_master (uuid, keyspace_name, application_name, is_api, "
128                 + "password, username, is_aaf) values (?,?,?,?,?,?,?)");
129         query.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
130         query.addValue(MusicUtil.convertToActualDataType(DataType.text(), keyspaceName));
131         query.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName));
132         query.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True"));
133         query.addValue(MusicUtil.convertToActualDataType(DataType.text(), hashedpwd));
134         query.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
135         query.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
136         CachingUtil.updateMusicCache(keyspaceName, appName);
137         CachingUtil.updateMusicValidateCache(appName, userId, hashedpwd);
138         MusicCore.eventualPut(query);
139     }
140
141 }