Merge "AAFcli.java -Declare "value" on a separate line"
[aaf/authz.git] / auth / auth-core / src / test / java / org / onap / aaf / auth / local / test / JU_TextIndex.java
index 613d2a8..30149cf 100644 (file)
@@ -7,9 +7,9 @@
  * * Licensed under the Apache License, Version 2.0 (the "License");
  * * you may not use this file except in compliance with the License.
  * * You may obtain a copy of the License at
- * * 
+ * *
  *  *      http://www.apache.org/licenses/LICENSE-2.0
- * * 
+ * *
  *  * Unless required by applicable law or agreed to in writing, software
  * * distributed under the License is distributed on an "AS IS" BASIS,
  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -19,6 +19,7 @@
  * *
  * *
  ******************************************************************************/
+
 package org.onap.aaf.auth.local.test;
 
 import static org.junit.Assert.*;
@@ -31,6 +32,10 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStreamWriter;
 import java.io.Writer;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.security.Principal;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -38,39 +43,102 @@ import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.runners.MockitoJUnitRunner;
+import org.onap.aaf.auth.env.AuthzTrans;
+import org.onap.aaf.auth.env.AuthzTransFilter;
+import org.onap.aaf.auth.local.AbsData.Reuse;
+import org.onap.aaf.auth.local.AbsData;
 import org.onap.aaf.auth.local.DataFile;
 import org.onap.aaf.auth.local.TextIndex;
+import org.onap.aaf.auth.local.TextIndex.Iter;
+import org.onap.aaf.auth.local.test.JU_AbsData;
+import org.onap.aaf.misc.env.Env;
 import org.onap.aaf.misc.env.TimeTaken;
 import org.onap.aaf.misc.env.Trans;
 
 @RunWith(MockitoJUnitRunner.class)
 public class JU_TextIndex {
-       TextIndex textIndex;
-       Trans trans;
-       DataFile datefile;
-       @Mock
-       File file;
-       
-       @Before
-       public void setUp() throws IOException{                 //TODO: AAF-111 fix once actual input is known.
-               char character = 'a';
-               String filePath = "test/output_key";
-               File keyfile = new File(filePath);
-               FileOutputStream is = new FileOutputStream(keyfile);
-        OutputStreamWriter osw = new OutputStreamWriter(is);    
-        Writer w = new BufferedWriter(osw);
-        w.write("asdfasdfasdf");
+    TextIndex textIndex;
+    Iter iter;
+    Trans trans;
+    DataFile datafile;
+    @Mock
+    File file;
+
+    private class AbsDataStub extends AbsData {
+
+
+        public AbsDataStub(File dataf, char sepChar, int maxLineSize, int fieldOffset) {
+            super(dataf, sepChar, maxLineSize, fieldOffset);
+            // TODO Auto-generated constructor stub
+
+        }
+
+    }
+
+    @Before
+    public void setUp() throws IOException{
+        char character = 'x';
+        String filePath = "test/output_key";
+        File keyfile = new File(filePath);
+        FileOutputStream is = new FileOutputStream(keyfile);
+        OutputStreamWriter osw = new OutputStreamWriter(is);
+        BufferedWriter  w = new BufferedWriter(osw);
+        for (int i = 0; i< 10; i++) {        //Write lines to file
+            w.write("a\nsdfasdfxasdf" + i + "\n");
+        }
         w.close();
-               datefile = new DataFile(keyfile, "test");
-               trans = mock(Trans.class);
-               textIndex = new TextIndex(keyfile);
-               //textIndex.create(trans, datefile, 20, character, 2, 2);
-               keyfile.delete();
-       }
-
-       @Test
-       public void testOpen() throws IOException {
-               //textIndex.open();
-       }
+
+        datafile = new DataFile(keyfile, "r");
+        datafile.open();
+        datafile = new DataFile(keyfile, "rws");// "S" for synchronized
+        datafile.open();
+
+        trans = mock(Trans.class);
+        TimeTaken ttMock = mock(TimeTaken.class);
+        TimeTaken ttMock1 = mock(TimeTaken.class);
+        when(trans.start("Open Files", Env.SUB)).thenReturn(ttMock);
+        when(trans.start("Read", Env.SUB)).thenReturn(ttMock);
+        textIndex = new TextIndex(keyfile);
+        textIndex.close();
+        textIndex.open();
+        //textIndex.create(trans, datafile, 4, character, 2, 0);    //TODO: AAF-111 once actual input is aquired
+        keyfile.delete();
+
+        iter = textIndex.new Iter();
+    }
+
+    @Test
+    public void testClose() throws IOException {
+        textIndex.close();
+    }
+
+    @Test
+    public void testFind() throws IOException {
+        char character = 'x';
+        String filePath = "test/output_.key";
+        File keyfile = new File(filePath);
+        AbsDataStub ads = new AbsDataStub(keyfile, character, 0, 0);
+        Reuse reuse = ads.reuse();
+        textIndex.find("a", reuse , 0);
+    }
+
+    @Test
+    public void testIterNext() {
+        iter.next();
+        iter.hasNext();
+    }
+
+    @Test
+    public void testIdx() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+        TextIndex outerObject = new TextIndex(file);
+        Class<?> idxClass = TextIndex.class.getDeclaredClasses()[0];
+        Constructor<?> idxConstructor = idxClass.getDeclaredConstructors()[0];
+        Class[] cArg = new Class[2];
+        cArg[0] = Object.class;
+        cArg[1] = Integer.class;
+        idxConstructor.setAccessible(true);
+        //Object innerObject = idxConstructor.newInstance(outerObject,cArg);
+        //idxConstructor.hashCode();                                            //TODO: AAF-111 access inner private class
+    }
 
 }