aboutsummaryrefslogtreecommitdiffstats
path: root/cadi/core/src/test/java/org/onap/ccsdk/apps/cadi/test/JU_Capacitor.java
blob: b0ba781537545cf70bf8c40dfb25a6a3a47cf3ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.ccsdk
 * * ===========================================================================
 * * Copyright © 2023 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * 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.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * *
 ******************************************************************************/

package org.onap.ccsdk.apps.cadi.test;

import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import org.junit.Before;
import org.junit.Test;
import org.onap.ccsdk.apps.cadi.Capacitor;

import java.lang.reflect.*;

public class JU_Capacitor {
    private Capacitor cap;
    public final static String TEST_DATA =
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
            "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" +
            "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" +
            "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" +
            "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" +
            "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";

    @Before
    public void setup() {
        cap = new Capacitor();
    }

    @Test
    public void singleByteTest() throws Exception {
        assertEquals(cap.read(), -1);
        cap.setForRead();
        Field curr_field = Capacitor.class.getDeclaredField("curr");
        curr_field.setAccessible(true);
        Field idx_field = Capacitor.class.getDeclaredField("idx");
        idx_field.setAccessible(true);
        assertNull(curr_field.get(cap));
        assertEquals(idx_field.get(cap), 0);

        for (int iter = 0; iter < 20; ++iter) {
            for (int i = 0; i < 20; ++i) {
                cap.put((byte)('a' + i));
            }
            cap.setForRead();
            byte[] array = new byte[20];
            for (int i = 0; i < 20; ++i) {
                array[i]=(byte)cap.read();
            }
            assertEquals("abcdefghijklmnopqrst", new String(array));
            assertEquals(-1, cap.read());

            cap.done();
        }

        for (int i = 0; i < 500; i++) {
            cap.put((byte)'a');
        }
        cap.setForRead();
        byte[] array = new byte[500];
        for (int i = 0; i < 500; ++i) {
            array[i]=(byte)cap.read();
        }
        assertEquals((new String(array)).length(), 500);
        assertEquals(-1, cap.read());
    }

    @Test
    public void availableTest() {
        assertEquals(cap.available(), 0);
        for (int i = 0; i < 100; ++i) {
            cap.put((byte)'a');
        }
        // The Capacitor can hold 256 bytes. After reading 100 bytes,
        // it should have 156 available
        assertEquals(cap.available(), 156);
    }

    @Test
    public void byteArrayTest() {
        byte[] arrayA = TEST_DATA.getBytes();
        assertEquals(cap.read(arrayA, 0, arrayA.length), -1);

        cap.put(arrayA, 0, arrayA.length);

        byte[] arrayB = new byte[arrayA.length];
        cap.setForRead();
        assertEquals(arrayA.length, cap.read(arrayB, 0, arrayB.length));
        assertEquals(TEST_DATA, new String(arrayB));
        assertEquals(-1, cap.read());
        cap.done();

        String b = "This is some content that we want to read";
        byte[] a = b.getBytes();
        byte[] c = new byte[b.length()]; // we want to use this to test reading offsets, etc

        for (int i = 0; i < a.length; i += 11) {
            cap.put(a, i, Math.min(11, a.length-i));
        }
        cap.reset();
        int read;
        for (int i = 0; i < c.length; i += read) {
            read = cap.read(c, i, Math.min(3, c.length-i));
        }
        assertEquals(b, new String(c));
    }

    @Test
    public void resetTest() throws Exception {
        cap.reset();
        Field curr_field = Capacitor.class.getDeclaredField("curr");
        curr_field.setAccessible(true);
        Field idx_field = Capacitor.class.getDeclaredField("idx");
        idx_field.setAccessible(true);
        assertNull(curr_field.get(cap));
        assertEquals(idx_field.get(cap), 0);

        cap.put((byte)'a');
        cap.reset();
        assertNotNull(curr_field.get(cap));
        assertEquals(idx_field.get(cap), 1);
    }

    @Test
    public void skipTest() throws Exception {
        // capacitor can't skip if nothing has been put into it
        assertEquals(cap.skip(10), 0);
        cap.put((byte)'a');
        // The Capacitor can hold 256 bytes. If we try  to skip 100 bytes,
        // it should only skip 1 byte, leaving 255 remaining
        assertEquals(cap.skip(100), 255);

        // Skipping 200 bytes leaves 0 remaining
        assertEquals(cap.skip(200), 0);
    }
}