1 /*******************************************************************************
2 * ============LICENSE_START====================================================
4 * * ===========================================================================
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * * ===========================================================================
7 * * Licensed under the Apache License, Version 2.0 (the "License");
8 * * you may not use this file except in compliance with the License.
9 * * You may obtain a copy of the License at
11 * * http://www.apache.org/licenses/LICENSE-2.0
13 * * Unless required by applicable law or agreed to in writing, software
14 * * distributed under the License is distributed on an "AS IS" BASIS,
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * * See the License for the specific language governing permissions and
17 * * limitations under the License.
18 * * ============LICENSE_END====================================================
21 ******************************************************************************/
22 package org.onap.aaf.auth.dao.aaf.test;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
27 import java.io.IOException;
28 import java.nio.ByteBuffer;
29 import java.security.NoSuchAlgorithmException;
30 import java.util.Date;
31 import java.util.List;
33 import org.junit.Test;
34 import org.onap.aaf.auth.dao.cass.CredDAO;
35 import org.onap.aaf.auth.dao.cass.CredDAO.Data;
36 import org.onap.aaf.auth.layer.Result;
37 import org.onap.aaf.misc.env.APIException;
44 public class JU_CredDAO extends AbsJUCass {
46 public void test() throws IOException, NoSuchAlgorithmException, APIException {
47 CredDAO udao = new CredDAO(trans,cluster,"authz");
50 CredDAO.Data data = new CredDAO.Data();
51 data.id = "m55555@aaf.att.com";
52 data.type = CredDAO.BASIC_AUTH;
53 data.notes = "temp pass";
54 data.cred = ByteBuffer.wrap(userPassToBytes("m55555","mypass"));
56 data.expires = new Date(System.currentTimeMillis() + 60000*60*24*90);
57 udao.create(trans,data);
60 ByteBuffer bb = data.bytify();
61 Data bdata = new CredDAO.Data();
62 bdata.reconstitute(bb);
63 checkData1(data, bdata);
65 // Validate Read with key fields in Data
66 Result<List<CredDAO.Data>> rlcd = udao.read(trans,data);
67 assertTrue(rlcd.isOKhasData());
68 for(CredDAO.Data d : rlcd.value) {
73 data.cred = ByteBuffer.wrap(userPassToBytes("m55555","mynewpass"));
74 udao.update(trans,data);
75 rlcd = udao.read(trans,data);
76 assertTrue(rlcd.isOKhasData());
77 for(CredDAO.Data d : rlcd.value) {
81 udao.delete(trans,data, true);
89 private void checkData1(Data data, Data d) {
90 assertEquals(data.id,d.id);
91 assertEquals(data.type,d.type);
92 assertEquals(data.ns,d.ns);
93 assertEquals(data.notes,d.notes);
94 assertEquals(data.cred,d.cred);
95 assertEquals(data.other,d.other);
96 assertEquals(data.expires,d.expires);
99 // private String CONST_myName = "MyName";
100 // public static final java.nio.ByteBuffer CONST_MY_CRED = get_CONST_MY_CRED();
101 // public static final int CONST_CRED_TYPE = 11;
103 // public static final Date CONST_UPDATE_DATE = new Date(System.currentTimeMillis()+60000*24);
105 // public void test() {
106 // UserDAO ud = new UserDAO(trans, cluster,"authz");
108 // UserDAO.Data data = createPrototypeUserData();
109 // ud.create(trans, data);
111 // // Validate Read with key fields in Data
112 // for(UserDAO.Data d : ud.read(trans, data)) {
113 // checkData1(data,d);
116 // // Validate readByName
117 // for(UserDAO.Data d : ud.read(trans, CONST_myName)) {
118 // checkData1(data,d);
121 // ud.delete(trans, data);
122 // List<UserDAO.Data> d_2 = ud.read(trans, CONST_myName);
124 // // Validate that data was deleted
125 // assertEquals("User should not be found after deleted", 0, d_2.size() );
127 // data = new UserDAO.Data();
128 // data.name = CONST_myName;
129 // data.cred = CONST_MY_CRED;
130 // data.cred_type= CONST_CRED_TYPE;
131 // data.expires = new Date(System.currentTimeMillis()+60000*24);
132 // final Result<UserDAO.Data> user = ud.r_create(trans, data);
133 // assertEquals("ud.createUser should work", Result.Status.OK, user.status);
135 // checkDataIgnoreDateDiff(data, user.value);
137 // // finally leave system in consistent state by deleting user again
138 // ud.delete(trans,data);
140 // } catch (DAOException e) {
141 // e.printStackTrace();
142 // fail("Fail due to Exception");
148 // private UserDAO.Data createPrototypeUserData() {
149 // UserDAO.Data data = new UserDAO.Data();
150 // data.name = CONST_myName;
152 // data.cred_type = CONST_CRED_TYPE;
153 // data.cred = CONST_MY_CRED;
154 // data.expires = CONST_UPDATE_DATE;
159 // // public void testReadByUser() throws Exception {
160 // // // this test was done above in our super test, since it uses the same setup
164 // public void testFunctionCreateUser() throws Exception {
165 // String name = "roger_rabbit";
166 // Integer credType = CONST_CRED_TYPE;
167 // java.nio.ByteBuffer cred = CONST_MY_CRED;
168 // final UserDAO ud = new UserDAO(trans, cluster,"authz");
169 // final UserDAO.Data data = createPrototypeUserData();
170 // Result<UserDAO.Data> ret = ud.r_create(trans, data);
171 // Result<List<Data>> byUserNameLookup = ud.r_read(trans, name);
173 // assertEquals("sanity test w/ different username (different than other test cases) failed", name, byUserNameLookup.value.get(0).name);
174 // assertEquals("delete roger_rabbit failed", true, ud.delete(trans, byUserNameLookup.value.get(0)));
178 // public void testLowLevelCassandraCreateData_Given_UserAlreadyPresent_ShouldPass() throws Exception {
179 // UserDAO ud = new UserDAO(trans, cluster,"authz");
181 // final UserDAO.Data data = createPrototypeUserData();
182 // final UserDAO.Data data1 = ud.create(trans, data);
183 // final UserDAO.Data data2 = ud.create(trans, data);
185 // assertNotNull(data1);
186 // assertNotNull(data2);
188 // assertEquals(CONST_myName, data1.name);
189 // assertEquals(CONST_myName, data2.name);
193 // public void testCreateUser_Given_UserAlreadyPresent_ShouldFail() throws Exception {
194 // UserDAO ud = new UserDAO(trans, cluster,"authz");
196 // final UserDAO.Data data = createPrototypeUserData();
198 // // make sure that some prev test did not leave the user in the DB
199 // ud.delete(trans, data);
201 // // attempt to create same user twice !!!
203 // final Result<UserDAO.Data> data1 = ud.r_create(trans, data);
204 // final Result<UserDAO.Data> data2 = ud.r_create(trans, data);
206 // assertNotNull(data1);
207 // assertNotNull(data2);
209 // assertEquals(true, Result.Status.OK == data1.status);
210 // assertEquals(false, Result.Status.OK == data2.status);
213 // private void checkData1(UserDAO.Data data, UserDAO.Data d) {
214 // data.name = CONST_myName;
216 // data.cred_type = CONST_CRED_TYPE;
217 // data.cred = CONST_MY_CRED;
218 // data.expires = CONST_UPDATE_DATE;
220 // assertEquals(data.name, d.name);
221 // assertEquals(data.cred_type, d.cred_type);
222 // assertEquals(data.cred, d.cred);
223 // assertEquals(data.expires, d.expires);
227 // private void checkDataIgnoreDateDiff(UserDAO.Data data, UserDAO.Data d) {
228 // data.name = CONST_myName;
230 // data.cred_type = CONST_CRED_TYPE;
231 // data.cred = CONST_MY_CRED;
232 // data.expires = CONST_UPDATE_DATE;
234 // assertEquals(data.name, d.name);
235 // assertEquals(data.cred_type, d.cred_type);
236 // assertEquals(data.cred, d.cred);
237 // // we allow dates to be different, e.g. high level calls e.g. createUser sets the date itself.
238 // //assertEquals(data.updated, d.updated);
243 // * Get a CONST_MY_CRED ByteBuffer, which is the java type for a cass blob.
246 // private static java.nio.ByteBuffer get_CONST_MY_CRED() {
247 // return ByteBuffer.wrap("Hello".getBytes());