CADI ID Translate
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / config / test / JU_MapBathConverter.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 package org.onap.aaf.cadi.config.test;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.text.SimpleDateFormat;
26 import java.util.ArrayList;
27 import java.util.GregorianCalendar;
28 import java.util.Iterator;
29 import java.util.List;
30
31 import org.junit.Before;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.onap.aaf.cadi.Access;
35 import org.onap.aaf.cadi.CadiException;
36 import org.onap.aaf.cadi.PropAccess;
37 import org.onap.aaf.cadi.Symm;
38 import org.onap.aaf.cadi.filter.MapBathConverter;
39 import org.onap.aaf.cadi.util.CSV;
40 import org.onap.aaf.cadi.util.CSV.Visitor;
41 import org.onap.aaf.cadi.util.CSV.Writer;
42
43 import junit.framework.Assert;
44
45 /**
46  * Test a simple Migration conversion tool for CADI
47  * 
48  * @author Instrumental(Jonathan)
49  *
50  */
51 public class JU_MapBathConverter {
52         private static final String NEW_USER_SOMETHING_ORG = "NEW_USER@Something.org";
53         private static final String OLD_ID = "OLD_ID";
54         private static final String SHARED_PASS = "SHARED_PASS";
55         private static CSV csv;
56         private static ArrayList<String> expected;
57         private static final Access access = new PropAccess();
58     private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
59
60         @BeforeClass
61         public static void createFile() throws IOException {
62                 // Note, you cate a "MapBathConverter" by access to a File.
63                 // We will create that file now.  Local is fine.
64                 csv = new CSV("JU_MapBathConverter.csv"); 
65         }
66         
67         @BeforeClass
68         public static void beforeClass() {
69                 expected = new ArrayList<>();
70         }
71         
72         @Before
73         public void before() {
74                 expected.clear();
75         }
76         
77         @Test
78         public void test() throws IOException, CadiException {
79                 CSV.Writer cw = csv.writer();
80                 GregorianCalendar gc = new GregorianCalendar();
81                 gc.add(GregorianCalendar.MONTH, 6);
82                 try {
83                         try {
84                                 // CSV can simply be OLD ID and NEW,  no passwords
85                                 cw.row(exp(OLD_ID), exp(NEW_USER_SOMETHING_ORG),sdf.format(gc.getTime()));
86
87                                 // Style 1 - Incoming ID/pass, create new cred with NweID and same Pass
88                                 cw.row(exp(bath(OLD_ID,SHARED_PASS)), exp(NEW_USER_SOMETHING_ORG),sdf.format(gc.getTime()));
89                                 // the response should be Basic with NEW_ID and OLD_PASS
90                                 
91                                 // Style 2
92                                 cw.row(exp(bath(OLD_ID,"OLD_PASS")), exp(bath(NEW_USER_SOMETHING_ORG,"NEW_PASS")),sdf.format(gc.getTime()));
93                                 
94                         } finally {
95                                 cw.close();
96                         }
97                         
98                         final Iterator<String> exp = expected.iterator();
99                         csv.visit(new Visitor() {
100                                 @Override
101                                 public void visit(List<String> row) {
102                                         int i=0;
103                                         for(String s : row) {
104                                                 switch(i++) {
105                                                         case 0:
106                                                         case 1:
107                                                                 Assert.assertEquals(exp.next(), s);
108                                                                 break;
109                                                         case 2:
110                                                                 System.out.println(s);
111                                                                 break;
112                                                         default:
113                                                                 Assert.fail("There should only be 3 columns in this test case.");
114                                                 }
115                                         }
116                                 }
117                         });
118                         
119                         MapBathConverter mbc = new MapBathConverter(access, csv);
120
121                         // Check no lookup just returns the same
122                         Assert.assertEquals("NonKey", "NonKey"); // if not in map, expect same value
123
124                         Iterator<String> exp1 = expected.iterator();
125                         // there's no passwords in CSV
126                         String old = exp1.next(); 
127                         String nw = exp1.next();
128                         Assert.assertEquals(nw, mbc.convert(access,old));
129                         
130                         Assert.assertEquals(bath(NEW_USER_SOMETHING_ORG,SHARED_PASS), mbc.convert(access,bath(OLD_ID,SHARED_PASS)));
131                         
132                         // Style 1 (new cred, old password)
133                         old = exp1.next();
134                         nw = bath(exp1.next(),SHARED_PASS);
135                         Assert.assertEquals(nw, mbc.convert(access,old));
136
137                         // Style 2
138                         old = exp1.next();
139                         nw = exp1.next();
140                         Assert.assertEquals(nw, mbc.convert(access,old));
141
142                 } finally {
143                         csv.delete();
144                 }
145         }
146
147         @Test
148         public void testTooFewColumns() throws IOException, CadiException {
149                 CSV.Writer cw = csv.writer();
150                 try {
151                         try {
152                                 cw.row(exp(bath(OLD_ID,"OLD_PASS")), exp(bath(NEW_USER_SOMETHING_ORG,"NEW_PASS")));
153                         } finally {
154                                 cw.close();
155                         }
156                         
157                         try {
158                                 new MapBathConverter(access, csv);
159                                 Assert.fail("file with too few rows should throw exception");
160                         } catch(CadiException | IOException e) {
161                                 Assert.assertTrue("Correctly thrown Exception",true);
162                         }
163                 } finally {
164                         csv.delete();
165                 }
166         }
167
168         @Test
169         public void testNoFile() {
170                 try {
171                         new MapBathConverter(access, new CSV("Bogus"));
172                         Assert.fail("Non Existent File should throw exception");
173                 } catch(CadiException | IOException e) {
174                         Assert.assertTrue("Correctly thrown Exception",true);
175                 }
176         }
177         
178         @Test
179         public void testBadRows() throws IOException {
180                 try {
181                         Writer cw = csv.writer();
182                         try {
183                                 cw.row("Single Column");
184                         } finally {
185                                 cw.close();
186                         }
187                         
188                         try {
189                                 new MapBathConverter(access,csv);
190                                 Assert.fail("Non Existent File should throw exception");
191                         } catch(CadiException | IOException e) {
192                                 Assert.assertTrue("Correctly thrown Exception",true);
193                         }
194                 } finally {
195                         csv.delete();
196                 }
197                 
198                 // Check for deletion 
199                 Assert.assertFalse(csv.toString() + "should have been deleted",new File(csv.toString()).exists());
200         }
201         
202         private String bath(String user, String password) throws IOException {
203                 StringBuilder sb = new StringBuilder(user);
204                 sb.append(':');
205                 sb.append(password);
206                 byte[] encoded = Symm.base64noSplit.encode(sb.toString().getBytes());
207                 sb.setLength(0);
208                 sb.append("Basic ");
209                 sb.append(new String(encoded));
210                 return sb.toString();
211         }
212
213         private String exp(String s) {
214                 expected.add(s);
215                 return s;
216         }
217 }