Remove spaces from dockerbuild script
[aaf/authz.git] / auth / auth-cass / src / test / java / com / att / dao / aaf / test / JU_NsDAO.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 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 com.att.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.parent = nsparent;
63                         data.attrib(true).putAll(oAttribs);
64                         
65
66                         Result<List<Data>> rdrr;
67
68                         // CREATE
69                         Result<Data> rdc = nsd.create(trans, data);
70                         assertTrue(rdc.isOK());
71                         
72                         try {
73 //                      Bytification
74                         ByteBuffer bb = data.bytify();
75                         Data bdata = new NsDAO.Data();
76                         bdata.reconstitute(bb);
77                         compare(data, bdata);
78
79                                 // Test READ by Object
80                                 rdrr = nsd.read(trans, data);
81                                 assertTrue(rdrr.isOKhasData());
82                                 assertEquals(rdrr.value.size(),1);
83                                 Data d = rdrr.value.get(0);
84                                 assertEquals(d.name,data.name);
85                                 assertEquals(d.type,data.type);
86                                 attribsEqual(d.attrib(false),data.attrib(false));
87                                 attribsEqual(oAttribs,data.attrib(false));
88                                 
89                                 // Test Read by Key
90                                 rdrr = nsd.read(trans, data.name);
91                                 assertTrue(rdrr.isOKhasData());
92                                 assertEquals(rdrr.value.size(),1);
93                                 d = rdrr.value.get(0);
94                                 assertEquals(d.name,data.name);
95                                 assertEquals(d.type,data.type);
96                                 attribsEqual(d.attrib(false),data.attrib(false));
97                                 attribsEqual(oAttribs,data.attrib(false));
98                                 
99                                 // Read NS by Type
100                                 Result<Set<String>> rtypes = nsd.readNsByAttrib(trans, SWM);
101                                 Set<String> types;
102                                 if(rtypes.notOK()) {
103                                         throw new IOException(rtypes.errorString());
104                                 } else {
105                                         types = rtypes.value;
106                                 }
107                                 assertEquals(1,types.size());
108                                 assertEquals(true,types.contains(ns1));
109                                 
110                                 // Add second NS to test list of data returned
111                                 Data data2 = new NsDAO.Data();
112                                 data2.name = ns2;
113                                 data2.type = 3; // app
114                                 data2.parent = nsparent;
115                                 Result<Data> rdc2 = nsd.create(trans, data2);
116                                 assertTrue(rdc2.isOK());
117                                 
118                                         // Interrupt - test PARENT
119                                         Result<List<Data>> rdchildren = nsd.getChildren(trans, "com.test");
120                                         assertTrue(rdchildren.isOKhasData());
121                                         boolean child1 = false;
122                                         boolean child2 = false;
123                                         for(Data dchild : rdchildren.value) {
124                                                 if(ns1.equals(dchild.name))child1=true;
125                                                 if(ns2.equals(dchild.name))child2=true;
126                                         }
127                                         assertTrue(child1);
128                                         assertTrue(child2);
129
130                                 // FINISH DATA 2 by deleting
131                                 Result<Void> rddr = nsd.delete(trans, data2, true);
132                                 assertTrue(rddr.isOK());
133
134                                 // ADD DESCRIPTION
135                                 String description = "This is my test Namespace";
136                                 assertFalse(description.equalsIgnoreCase(data.description));
137                                 
138                                 Result<Void> addDesc = nsd.addDescription(trans, data.name, description);
139                                 assertTrue(addDesc.isOK());
140                                 rdrr = nsd.read(trans, data);
141                                 assertTrue(rdrr.isOKhasData());
142                                 assertEquals(rdrr.value.size(),1);
143                                 assertEquals(rdrr.value.get(0).description,description);
144                                 
145                                 // UPDATE
146                                 String newDescription = "zz1234 Owns This Namespace Now";
147                                 oAttribs.put("mso", "mso_data");
148                                 data.attrib(true).put("mso", "mso_data");
149                                 data.description = newDescription;
150                                 Result<Void> update = nsd.update(trans, data);
151                                 assertTrue(update.isOK());
152                                 rdrr = nsd.read(trans, data);
153                                 assertTrue(rdrr.isOKhasData());
154                                 assertEquals(rdrr.value.size(),1);
155                                 assertEquals(rdrr.value.get(0).description,newDescription);
156                                 attribsEqual(oAttribs, rdrr.value.get(0).attrib);
157                                 
158                                 
159                         } catch (IOException e) {
160                                 e.printStackTrace();
161                         } finally {
162                                 // DELETE
163                                 Result<Void> rddr = nsd.delete(trans, data, true);
164                                 assertTrue(rddr.isOK());
165                                 rdrr = nsd.read(trans, data);
166                                 assertTrue(rdrr.isOK() && rdrr.isEmpty());
167                                 assertEquals(rdrr.value.size(),0);
168                         }
169                 } finally {
170                         nsd.close(trans);
171                 }
172         }
173
174         private void compare(NsDAO.Data d, NsDAO.Data data) {
175                 assertEquals(d.name,data.name);
176                 assertEquals(d.type,data.type);
177                 attribsEqual(d.attrib(false),data.attrib(false));
178                 attribsEqual(d.attrib(false),data.attrib(false));
179         }
180         
181         private void attribsEqual(Map<String,String> aa, Map<String,String> ba) {
182                 assertEquals(aa.size(),ba.size());
183                 for(Entry<String, String> es : aa.entrySet()) {
184                         assertEquals(es.getValue(),ba.get(es.getKey()));
185                 }
186         }
187 }