e9bceccde18812aa207dfb06268f1ed8ac89aee9
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / test / JU_Capacitor.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.cadi.test;
23
24 import static junit.framework.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.aaf.cadi.Capacitor;
31
32 import java.lang.reflect.*;
33
34 public class JU_Capacitor {
35         private Capacitor cap;
36         public final static String TEST_DATA = 
37                         "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
38                         "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" +
39                         "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" +
40                         "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" +
41                         "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" +
42                         "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
43
44         @Before
45         public void setup() {
46                 cap = new Capacitor();
47         }
48
49         @Test
50         public void singleByteTest() throws Exception {
51         assertEquals(cap.read(), -1);
52         cap.setForRead();
53         Field curr_field = Capacitor.class.getDeclaredField("curr");
54         curr_field.setAccessible(true);
55         Field idx_field = Capacitor.class.getDeclaredField("idx");
56         idx_field.setAccessible(true);
57         assertNull(curr_field.get(cap));
58         assertEquals(idx_field.get(cap), 0);
59
60                 for(int iter = 0; iter < 20; ++iter) {
61                         for(int i = 0; i < 20; ++i) {
62                                 cap.put((byte)('a' + i));
63                         }
64                         cap.setForRead();
65                         byte[] array = new byte[20];
66                         for(int i = 0; i < 20; ++i) {
67                                 array[i]=(byte)cap.read();
68                         }
69                         assertEquals("abcdefghijklmnopqrst", new String(array));
70                         assertEquals(-1, cap.read());
71
72                         cap.done();
73                 }
74
75                 for(int i = 0; i < 500; i++) {
76                         cap.put((byte)'a');
77                 }
78                 cap.setForRead();
79                 byte[] array = new byte[500];
80                 for(int i = 0; i < 500; ++i) {
81                         array[i]=(byte)cap.read();
82                 }
83         assertEquals((new String(array)).length(), 500);
84                 assertEquals(-1, cap.read());
85         }
86
87         @Test
88         public void availableTest() {
89         assertEquals(cap.available(), 0);
90         for(int i = 0; i < 100; ++i) {
91             cap.put((byte)'a');
92         }
93         // The Capacitor can hold 256 bytes. After reading 100 bytes,
94         // it should have 156 available
95         assertEquals(cap.available(), 156);
96     }
97
98         @Test
99         public void byteArrayTest() {
100                 byte[] arrayA = TEST_DATA.getBytes();
101         assertEquals(cap.read(arrayA, 0, arrayA.length), -1);
102
103         cap.put(arrayA, 0, arrayA.length);
104
105         byte[] arrayB = new byte[arrayA.length];
106         cap.setForRead();
107         assertEquals(arrayA.length, cap.read(arrayB, 0, arrayB.length));
108         assertEquals(TEST_DATA, new String(arrayB));
109         assertEquals(-1, cap.read());
110         cap.done();
111
112                 String b = "This is some content that we want to read";
113                 byte[] a = b.getBytes();
114                 byte[] c = new byte[b.length()]; // we want to use this to test reading offsets, etc
115
116                 for(int i = 0; i < a.length; i += 11) {
117                         cap.put(a, i, Math.min(11, a.length-i));
118                 }
119                 cap.reset();
120                 int read;
121                 for(int i = 0; i < c.length; i += read) {
122                         read = cap.read(c, i, Math.min(3, c.length-i));
123                 }
124                 assertEquals(b, new String(c)); 
125         }
126
127         @Test
128         public void resetTest() throws Exception {
129                 cap.reset();
130         Field curr_field = Capacitor.class.getDeclaredField("curr");
131         curr_field.setAccessible(true);
132         Field idx_field = Capacitor.class.getDeclaredField("idx");
133         idx_field.setAccessible(true);
134         assertNull(curr_field.get(cap));
135         assertEquals(idx_field.get(cap), 0);
136
137                 cap.put((byte)'a');
138                 cap.reset();
139         assertNotNull(curr_field.get(cap));
140         assertEquals(idx_field.get(cap), 1);
141         }
142
143         @Test
144         public void skipTest() throws Exception {
145                 // capacitor can't skip if nothing has been put into it
146                 assertEquals(cap.skip(10), 0);
147                 cap.put((byte)'a');
148                 // The Capacitor can hold 256 bytes. If we try  to skip 100 bytes,
149                 // it should only skip 1 byte, leaving 255 remaining
150                 assertEquals(cap.skip(100), 255);
151
152                 // Skipping 200 bytes leaves 0 remaining
153                 assertEquals(cap.skip(200), 0);
154         }
155 }