d433029635323f7fa4e40221daecbaad609ff505
[aaf/authz.git] / auth / auth-core / src / test / java / org / onap / aaf / auth / local / test / JU_TextIndex.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
4  * * ===========================================================================
5  * * Copyright © 2017 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  ******************************************************************************/
22
23 package org.onap.aaf.auth.local.test;
24
25 import static org.junit.Assert.*;
26 import static org.mockito.Matchers.*;
27 import static org.mockito.Mockito.*;
28
29 import java.io.BufferedWriter;
30 import java.io.File;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33 import java.io.OutputStreamWriter;
34 import java.io.Writer;
35 import java.lang.reflect.Constructor;
36 import java.lang.reflect.InvocationTargetException;
37 import java.lang.reflect.Method;
38 import java.security.Principal;
39
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mock;
44 import org.mockito.Mockito;
45 import org.mockito.runners.MockitoJUnitRunner;
46 import org.onap.aaf.auth.env.AuthzTrans;
47 import org.onap.aaf.auth.env.AuthzTransFilter;
48 import org.onap.aaf.auth.local.AbsData.Reuse;
49 import org.onap.aaf.auth.local.AbsData;
50 import org.onap.aaf.auth.local.DataFile;
51 import org.onap.aaf.auth.local.TextIndex;
52 import org.onap.aaf.auth.local.TextIndex.Iter;
53 import org.onap.aaf.auth.local.test.JU_AbsData;
54 import org.onap.aaf.misc.env.Env;
55 import org.onap.aaf.misc.env.TimeTaken;
56 import org.onap.aaf.misc.env.Trans;
57
58 @RunWith(MockitoJUnitRunner.class)
59 public class JU_TextIndex {
60     TextIndex textIndex;
61     Iter iter;
62     Trans trans;
63     DataFile datafile;
64     @Mock
65     File file;
66     
67     private class AbsDataStub extends AbsData {
68
69         
70         public AbsDataStub(File dataf, char sepChar, int maxLineSize, int fieldOffset) {
71             super(dataf, sepChar, maxLineSize, fieldOffset);
72             // TODO Auto-generated constructor stub
73             
74         }
75         
76     }
77     
78     @Before
79     public void setUp() throws IOException{    
80         char character = 'x';
81         String filePath = "test/output_key";
82         File keyfile = new File(filePath);
83         FileOutputStream is = new FileOutputStream(keyfile);
84         OutputStreamWriter osw = new OutputStreamWriter(is);
85         BufferedWriter  w = new BufferedWriter(osw);
86         for (int i = 0; i< 10; i++) {        //Write lines to file
87             w.write("a\nsdfasdfxasdf" + i + "\n");
88         }
89         w.close();
90         
91         datafile = new DataFile(keyfile, "r");
92         datafile.open();
93         datafile = new DataFile(keyfile, "rws");// "S" for synchronized
94         datafile.open();
95         
96         trans = mock(Trans.class);
97         TimeTaken ttMock = mock(TimeTaken.class);
98         TimeTaken ttMock1 = mock(TimeTaken.class);
99         when(trans.start("Open Files", Env.SUB)).thenReturn(ttMock);
100         when(trans.start("Read", Env.SUB)).thenReturn(ttMock);
101         textIndex = new TextIndex(keyfile);    
102         textIndex.close();
103         textIndex.open();
104         //textIndex.create(trans, datafile, 4, character, 2, 0);    //TODO: AAF-111 once actual input is aquired
105         keyfile.delete();
106         
107         iter = textIndex.new Iter();
108     }
109     
110     @Test
111     public void testClose() throws IOException {
112         textIndex.close();
113     }
114     
115     @Test
116     public void testFind() throws IOException {
117         char character = 'x';
118         String filePath = "test/output_.key";
119         File keyfile = new File(filePath);
120         AbsDataStub ads = new AbsDataStub(keyfile, character, 0, 0);
121         Reuse reuse = ads.reuse();
122         textIndex.find("a", reuse , 0);
123     }
124     
125     @Test
126     public void testIterNext() {
127         iter.next();
128         iter.hasNext();
129     }
130     
131     @Test
132     public void testIdx() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
133         TextIndex outerObject = new TextIndex(file);
134         Class<?> idxClass = TextIndex.class.getDeclaredClasses()[0];
135         Constructor<?> idxConstructor = idxClass.getDeclaredConstructors()[0];
136         Class[] cArg = new Class[2];
137         cArg[0] = Object.class;
138         cArg[1] = Integer.class;
139         idxConstructor.setAccessible(true);
140         //Object innerObject = idxConstructor.newInstance(outerObject,cArg);
141         //idxConstructor.hashCode();                                            //TODO: AAF-111 access inner private class
142     }
143
144 }