Remove spaces from dockerbuild script
[aaf/authz.git] / auth / auth-cass / src / test / java / org / onap / aaf / auth / dao / aaf / test / JU_NsDAO.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
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
10  * * 
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * * 
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====================================================
19  * *
20  * *
21  ******************************************************************************/
22 package org.onap.aaf.auth.dao.aaf.test;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.IOException;
29 import java.nio.ByteBuffer;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Map.Entry;
34 import java.util.Set;
35
36 import org.junit.Test;
37 import org.onap.aaf.auth.dao.cass.NsDAO;
38 import org.onap.aaf.auth.dao.cass.NsType;
39 import org.onap.aaf.auth.dao.cass.NsDAO.Data;
40 import org.onap.aaf.auth.layer.Result;
41 import org.onap.aaf.misc.env.APIException;
42
43
44 public class JU_NsDAO extends AbsJUCass {
45         private static final String CRM = "ju_crm";
46         private static final String SWM = "ju_swm";
47
48         @Test
49         public void test() throws APIException, IOException  {
50                 NsDAO nsd = new NsDAO(trans, cluster, AUTHZ);
51                 try {
52                         final String nsparent = "com.test";
53                         final String ns1 = nsparent +".ju_ns";
54                         final String ns2 = nsparent + ".ju_ns2";
55                         
56                         Map<String,String> oAttribs = new HashMap<String,String>();
57                         oAttribs.put(SWM, "swm_data");
58                         oAttribs.put(CRM, "crm_data");
59                         Data data = new NsDAO.Data();
60                         data.name = ns1;
61                         data.type = NsType.APP.type;
62                         data.attrib(true).putAll(oAttribs);
63                         
64
65                         Result<List<Data>> rdrr;
66
67                         // CREATE
68                         Result<Data> rdc = nsd.create(trans, data);
69                         assertTrue(rdc.isOK());
70                         
71                         try {
72 //                      Bytification
73                         ByteBuffer bb = data.bytify();
74                         Data bdata = new NsDAO.Data();
75                         bdata.reconstitute(bb);
76                         compare(data, bdata);
77
78                                 // Test READ by Object
79                                 rdrr = nsd.read(trans, data);
80                                 assertTrue(rdrr.isOKhasData());
81                                 assertEquals(rdrr.value.size(),1);
82                                 Data d = rdrr.value.get(0);
83                                 assertEquals(d.name,data.name);
84                                 assertEquals(d.type,data.type);
85                                 attribsEqual(d.attrib(false),data.attrib(false));
86                                 attribsEqual(oAttribs,data.attrib(false));
87                                 
88                                 // Test Read by Key
89                                 rdrr = nsd.read(trans, data.name);
90                                 assertTrue(rdrr.isOKhasData());
91                                 assertEquals(rdrr.value.size(),1);
92                                 d = rdrr.value.get(0);
93                                 assertEquals(d.name,data.name);
94                                 assertEquals(d.type,data.type);
95                                 attribsEqual(d.attrib(false),data.attrib(false));
96                                 attribsEqual(oAttribs,data.attrib(false));
97                                 
98                                 // Read NS by Type
99                                 Result<Set<String>> rtypes = nsd.readNsByAttrib(trans, SWM);
100                                 Set<String> types;
101                                 if(rtypes.notOK()) {
102                                         throw new IOException(rtypes.errorString());
103                                 } else {
104                                         types = rtypes.value;
105                                 }
106                                 assertEquals(1,types.size());
107                                 assertEquals(true,types.contains(ns1));
108                                 
109                                 // Add second NS to test list of data returned
110                                 Data data2 = new NsDAO.Data();
111                                 data2.name = ns2;
112                                 data2.type = 3; // app
113                                 Result<Data> rdc2 = nsd.create(trans, data2);
114                                 assertTrue(rdc2.isOK());
115                                 
116                                         // Interrupt - test PARENT
117                                         Result<List<Data>> rdchildren = nsd.getChildren(trans, "com.test");
118                                         assertTrue(rdchildren.isOKhasData());
119                                         boolean child1 = false;
120                                         boolean child2 = false;
121                                         for(Data dchild : rdchildren.value) {
122                                                 if(ns1.equals(dchild.name))child1=true;
123                                                 if(ns2.equals(dchild.name))child2=true;
124                                         }
125                                         assertTrue(child1);
126                                         assertTrue(child2);
127
128                                 // FINISH DATA 2 by deleting
129                                 Result<Void> rddr = nsd.delete(trans, data2, true);
130                                 assertTrue(rddr.isOK());
131
132                                 // ADD DESCRIPTION
133                                 String description = "This is my test Namespace";
134                                 assertFalse(description.equalsIgnoreCase(data.description));
135                                 
136                                 Result<Void> addDesc = nsd.addDescription(trans, data.name, description);
137                                 assertTrue(addDesc.isOK());
138                                 rdrr = nsd.read(trans, data);
139                                 assertTrue(rdrr.isOKhasData());
140                                 assertEquals(rdrr.value.size(),1);
141                                 assertEquals(rdrr.value.get(0).description,description);
142                                 
143                                 // UPDATE
144                                 String newDescription = "zz1234 Owns This Namespace Now";
145                                 oAttribs.put("mso", "mso_data");
146                                 data.attrib(true).put("mso", "mso_data");
147                                 data.description = newDescription;
148                                 Result<Void> update = nsd.update(trans, data);
149                                 assertTrue(update.isOK());
150                                 rdrr = nsd.read(trans, data);
151                                 assertTrue(rdrr.isOKhasData());
152                                 assertEquals(rdrr.value.size(),1);
153                                 assertEquals(rdrr.value.get(0).description,newDescription);
154                                 attribsEqual(oAttribs, rdrr.value.get(0).attrib);
155                                 
156                                 
157                         } catch (IOException e) {
158                                 e.printStackTrace();
159                         } finally {
160                                 // DELETE
161                                 Result<Void> rddr = nsd.delete(trans, data, true);
162                                 assertTrue(rddr.isOK());
163                                 rdrr = nsd.read(trans, data);
164                                 assertTrue(rdrr.isOK() && rdrr.isEmpty());
165                                 assertEquals(rdrr.value.size(),0);
166                         }
167                 } finally {
168                         nsd.close(trans);
169                 }
170         }
171
172         private void compare(NsDAO.Data d, NsDAO.Data data) {
173                 assertEquals(d.name,data.name);
174                 assertEquals(d.type,data.type);
175                 attribsEqual(d.attrib(false),data.attrib(false));
176                 attribsEqual(d.attrib(false),data.attrib(false));
177         }
178         
179         private void attribsEqual(Map<String,String> aa, Map<String,String> ba) {
180                 assertEquals(aa.size(),ba.size());
181                 for(Entry<String, String> es : aa.entrySet()) {
182                         assertEquals(es.getValue(),ba.get(es.getKey()));
183                 }
184         }
185 }