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