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;
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;
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;
43 import junit.framework.Assert;
46 * Test a simple Migration conversion tool for CADI
48 * @author Instrumental(Jonathan)
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");
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");
68 public static void beforeClass() {
69 expected = new ArrayList<>();
73 public void before() {
78 public void test() throws IOException, CadiException {
79 CSV.Writer cw = csv.writer();
80 GregorianCalendar gc = new GregorianCalendar();
81 gc.add(GregorianCalendar.MONTH, 6);
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()));
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
92 cw.row(exp(bath(OLD_ID,"OLD_PASS")), exp(bath(NEW_USER_SOMETHING_ORG,"NEW_PASS")),sdf.format(gc.getTime()));
98 final Iterator<String> exp = expected.iterator();
99 csv.visit(new Visitor() {
101 public void visit(List<String> row) {
103 for(String s : row) {
107 Assert.assertEquals(exp.next(), s);
110 System.out.println(s);
113 Assert.fail("There should only be 3 columns in this test case.");
119 MapBathConverter mbc = new MapBathConverter(access, csv);
121 // Check no lookup just returns the same
122 Assert.assertEquals("NonKey", "NonKey"); // if not in map, expect same value
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));
130 Assert.assertEquals(bath(NEW_USER_SOMETHING_ORG,SHARED_PASS), mbc.convert(access,bath(OLD_ID,SHARED_PASS)));
132 // Style 1 (new cred, old password)
134 nw = bath(exp1.next(),SHARED_PASS);
135 Assert.assertEquals(nw, mbc.convert(access,old));
140 Assert.assertEquals(nw, mbc.convert(access,old));
148 public void testTooFewColumns() throws IOException, CadiException {
149 CSV.Writer cw = csv.writer();
152 cw.row(exp(bath(OLD_ID,"OLD_PASS")), exp(bath(NEW_USER_SOMETHING_ORG,"NEW_PASS")));
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);
169 public void testNoFile() {
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);
179 public void testBadRows() throws IOException {
181 Writer cw = csv.writer();
183 cw.row("Single Column");
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);
198 // Check for deletion
199 Assert.assertFalse(csv.toString() + "should have been deleted",new File(csv.toString()).exists());
202 private String bath(String user, String password) throws IOException {
203 StringBuilder sb = new StringBuilder(user);
206 byte[] encoded = Symm.base64noSplit.encode(sb.toString().getBytes());
209 sb.append(new String(encoded));
210 return sb.toString();
213 private String exp(String s) {