Add a MassMail Batch Program
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / util / test / JU_CSV.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.util.test;
22
23 import java.io.File;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.PrintStream;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.junit.After;
31 import org.junit.Assert;
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.util.CSV;
39 import org.onap.aaf.cadi.util.CSV.Visitor;
40 import org.onap.aaf.cadi.util.CSV.Writer;
41 import org.onap.aaf.cadi.util.Holder;
42
43 public class JU_CSV {
44
45     private String filename;
46     private File file;
47     private static ArrayList<Object> expected;
48
49     @Before
50     public void start() {
51         filename = "Sample.csv";
52         file = new File(filename);
53     }
54
55     @After
56     public void end() {
57         if(file!=null) {
58             file.delete();
59         }
60     }
61
62     @BeforeClass
63     public static void before() {
64         expected = new ArrayList<>();
65     }
66
67     @Test
68     public void test() throws IOException, CadiException {
69         Access access = new PropAccess();
70         CSV csv = new CSV(access,file);
71         // Can't visit for file that doesn't exist
72         try {
73             csv.visit(new Visitor() {
74                 @Override
75                 public void visit(List<String> row) {
76                 }});
77         } catch(IOException e) {
78             Assert.assertTrue("CSV correctly created exception",true);
79         }
80
81         Writer writer = csv.writer();
82         try {
83             writer.row(add("\"hello\""));
84             writer.comment("Ignore Comments");
85             writer.row(add("dXNlcjpwYXNzd29yZA=="),add("dXNlckBzb21ldGhpbmcub3JnOm90aGVyUGFzc3dvcmQ="));
86             writer.row(); // no output
87             writer.row(add("There is, but one thing to say"), add(" and that is"), add("\"All the best\""));
88         } finally {
89             writer.close();
90         }
91
92         PrintStream garbage = new PrintStream(new FileOutputStream(file, true));
93         try {
94             garbage.println("# Ignore empty spaces, etc");
95             garbage.println("   ");
96             garbage.println("# Ignore blank lines");
97             garbage.println();
98         } finally {
99             garbage.close();
100         }
101
102
103     ////////////
104     // Tests
105     ////////////
106         final Holder<Integer> hi = new Holder<>(0);
107         csv.visit(new CSV.Visitor() {
108             @Override
109             public void visit(List<String> row) {
110                 for(String s: row) {
111 //                    System.out.println(hi.value + ") " + s);
112                     Assert.assertEquals(expected.get(hi.get()),s);
113                     hi.set(hi.get()+1); // increment
114                 }
115             }
116         });
117
118     }
119
120     private String add(String s) {
121         expected.add(s);
122         return s;
123     }
124
125 }