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