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