2 * ============LICENSE_START====================================================
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
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 package org.onap.aaf.cadi.config.test;
24 import java.io.IOException;
26 import java.text.SimpleDateFormat;
27 import java.util.ArrayList;
28 import java.util.GregorianCalendar;
29 import java.util.Iterator;
30 import java.util.List;
32 import org.junit.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.aaf.cadi.Access;
36 import org.onap.aaf.cadi.CadiException;
37 import org.onap.aaf.cadi.PropAccess;
38 import org.onap.aaf.cadi.Symm;
39 import org.onap.aaf.cadi.filter.MapBathConverter;
40 import org.onap.aaf.cadi.util.CSV;
41 import org.onap.aaf.cadi.util.CSV.Visitor;
42 import org.onap.aaf.cadi.util.CSV.Writer;
44 import junit.framework.Assert;
47 * Test a simple Migration conversion tool for CADI
49 * @author Instrumental(Jonathan)
52 public class JU_MapBathConverter {
53 private static final String NEW_USER_SOMETHING_ORG = "NEW_USER@Something.org";
54 private static final String OLD_ID = "OLD_ID";
55 private static final String SHARED_PASS = "SHARED_PASS";
56 private static CSV csv;
57 private static ArrayList<String> expected;
58 private static final Access access = new PropAccess();
59 private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
62 public static void createFile() throws IOException {
63 // Note, you cate a "MapBathConverter" by access to a File.
64 // We will create that file now. Local is fine.
65 csv = new CSV(access,"JU_MapBathConverter.csv");
69 public static void beforeClass() {
70 expected = new ArrayList<>();
74 public void before() {
79 public void test() throws IOException, CadiException {
80 CSV.Writer cw = csv.writer();
81 GregorianCalendar gc = new GregorianCalendar();
82 gc.add(GregorianCalendar.MONTH, 6);
85 // CSV can simply be OLD ID and NEW, no passwords
86 cw.row(exp(OLD_ID), exp(NEW_USER_SOMETHING_ORG),sdf.format(gc.getTime()));
88 // Style 1 - Incoming ID/pass, create new cred with NweID and same Pass
89 cw.row(exp(bath(OLD_ID,SHARED_PASS)), exp(NEW_USER_SOMETHING_ORG),sdf.format(gc.getTime()));
90 // the response should be Basic with NEW_ID and OLD_PASS
93 cw.row(exp(bath(OLD_ID,"OLD_PASS")), exp(bath(NEW_USER_SOMETHING_ORG,"NEW_PASS")),sdf.format(gc.getTime()));
99 final Iterator<String> exp = expected.iterator();
100 csv.visit(new Visitor() {
102 public void visit(List<String> row) {
104 for(String s : row) {
108 Assert.assertEquals(exp.next(), s);
113 } catch (Exception e) {
114 Assert.assertTrue("Last entry should be a date",false);
118 Assert.fail("There should only be 3 columns in this test case.");
124 MapBathConverter mbc = new MapBathConverter(access, csv);
126 // Check no lookup just returns the same
127 Assert.assertEquals("NonKey", "NonKey"); // if not in map, expect same value
129 Iterator<String> exp1 = expected.iterator();
130 // there's no passwords in CSV
131 String old = exp1.next();
132 String nw = exp1.next();
133 Assert.assertEquals(nw, mbc.convert(access,old));
135 Assert.assertEquals(bath(NEW_USER_SOMETHING_ORG,SHARED_PASS), mbc.convert(access,bath(OLD_ID,SHARED_PASS)));
137 // Style 1 (new cred, old password)
139 nw = bath(exp1.next(),SHARED_PASS);
140 Assert.assertEquals(nw, mbc.convert(access,old));
145 Assert.assertEquals(nw, mbc.convert(access,old));
153 public void testInsecureRole() throws IOException {
154 CSV.Writer cw = csv.writer();
155 GregorianCalendar gc = new GregorianCalendar();
156 gc.add(GregorianCalendar.MONTH, 6);
159 // Invalid Scenario - Non Authenticated ID to authenticated User
160 cw.row(exp(OLD_ID), exp(bath(NEW_USER_SOMETHING_ORG,"NEW_PASS")),sdf.format(gc.getTime()));
167 new MapBathConverter(access, csv);
168 Assert.fail("Invalid Data should throw Exception");
169 } catch (CadiException e) {
170 Assert.assertTrue("Invalid Data should throw Exception",true);
179 public void testTooFewColumns() throws IOException, CadiException {
180 CSV.Writer cw = csv.writer();
183 cw.row(exp(bath(OLD_ID,"OLD_PASS")), exp(bath(NEW_USER_SOMETHING_ORG,"NEW_PASS")));
189 new MapBathConverter(access, csv);
190 Assert.fail("file with too few rows should throw exception");
191 } catch(CadiException | IOException e) {
192 Assert.assertTrue("Correctly thrown Exception",true);
200 public void testNoFile() {
202 new MapBathConverter(access, new CSV(access,"Bogus"));
203 Assert.fail("Non Existent File should throw exception");
204 } catch(CadiException | IOException e) {
205 Assert.assertTrue("Correctly thrown Exception",true);
210 public void testBadRows() throws IOException {
212 Writer cw = csv.writer();
214 cw.row("Single Column");
220 new MapBathConverter(access,csv);
221 Assert.fail("Non Existent File should throw exception");
222 } catch(CadiException | IOException e) {
223 Assert.assertTrue("Correctly thrown Exception",true);
229 // Check for deletion
230 Assert.assertFalse(csv.toString() + "should have been deleted",new File(csv.toString()).exists());
233 private String bath(String user, String password) throws IOException {
234 StringBuilder sb = new StringBuilder(user);
237 byte[] encoded = Symm.base64noSplit.encode(sb.toString().getBytes());
240 sb.append(new String(encoded));
241 return sb.toString();
244 private String exp(String s) {