Fix request handler class error
[appc.git] / appc-common / src / test / java / org / onap / appc / pool / CachedElementTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24
25
26 package org.onap.appc.pool;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertNotNull;
31 import static org.junit.Assert.assertNull;
32 import static org.junit.Assert.assertTrue;
33
34 import java.io.IOException;
35 import java.lang.reflect.InvocationTargetException;
36 import java.lang.reflect.Method;
37
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.appc.pool.Allocator;
41 import org.onap.appc.pool.CachedElement;
42 import org.onap.appc.pool.Destructor;
43 import org.onap.appc.pool.Pool;
44 import org.onap.appc.pool.PoolDrainedException;
45 import org.onap.appc.pool.PoolExtensionException;
46 import org.onap.appc.pool.PoolSpecificationException;
47 import org.onap.appc.pool.*;
48
49
50 public class CachedElementTest implements Allocator<Testable>, Destructor<Testable> {
51     private static final int MIN = 10;
52     private static final int MAX = 100;
53     private Pool<Testable> pool;
54     private int index = 0;
55     private int destroyCount = 0;
56
57     /**
58      * setup
59      *
60      * @throws PoolSpecificationException
61      *             If the minimum size is less than 0, or if the max size is non-zero and less than the min size.
62      */
63     @Before
64     public void setup() throws PoolSpecificationException {
65         pool = new Pool<>(MIN, MAX);
66     }
67
68     /**
69      * Test state
70      */
71     @Test
72     public void testAllocator() {
73         assertNull(pool.getAllocator());
74         pool.setAllocator(this);
75         assertNotNull(pool.getAllocator());
76     }
77
78     /**
79      * Test state
80      */
81     @Test
82     public void testDestructor() {
83         assertNull(pool.getDestructor());
84         pool.setDestructor(this);
85         assertNotNull(pool.getDestructor());
86     }
87
88     /**
89      * Test that we can allocate and release elements and that the pool maintains them in MRU order
90      *
91      * @throws PoolExtensionException
92      *             If the pool cannot be extended
93      * @throws PoolDrainedException
94      *             If the caller is trying to reserve an element from a drained pool
95      */
96     @Test
97     public void testAllocateAndRelease() throws PoolExtensionException, PoolDrainedException {
98         pool.setAllocator(this);
99
100         assertFalse(pool.isDrained());
101
102         /*
103          * Allocate three elements
104          */
105         Testable value1 = pool.reserve();
106         assertNotNull(value1);
107         assertEquals(Integer.valueOf(MIN - 1), Integer.valueOf(value1.getId()));
108         assertEquals(1, pool.getAllocatedSize());
109         assertEquals(MIN - 1, pool.getFreeSize());
110         assertEquals(1, pool.getAllocatedSize());
111
112         Testable value2 = pool.reserve();
113         assertNotNull(value2);
114         assertEquals(Integer.valueOf(MIN - 2), Integer.valueOf(value2.getId()));
115         assertEquals(2, pool.getAllocatedSize());
116         assertEquals(MIN - 2, pool.getFreeSize());
117         assertEquals(2, pool.getAllocatedSize());
118
119         Testable value3 = pool.reserve();
120         assertNotNull(value3);
121         assertEquals(Integer.valueOf(MIN - 3), Integer.valueOf(value3.getId()));
122         assertEquals(3, pool.getAllocatedSize());
123         assertEquals(MIN - 3, pool.getFreeSize());
124         assertEquals(3, pool.getAllocatedSize());
125
126         /*
127          * Now, release them in the order obtained
128          */
129         pool.release(value1);
130         pool.release(value2);
131         pool.release(value3);
132
133         assertEquals(0, pool.getAllocatedSize());
134         assertEquals(MIN, pool.getFreeSize());
135
136         /*
137          * Now, allocate them again, but their values should be reversed (3, 2, 1) representing the most recently used
138          * to the least recently used.
139          */
140         value1 = pool.reserve();
141         assertNotNull(value1);
142         assertEquals(Integer.valueOf(MIN - 3), Integer.valueOf(value1.getId()));
143
144         value2 = pool.reserve();
145         assertNotNull(value2);
146         assertEquals(Integer.valueOf(MIN - 2), Integer.valueOf(value2.getId()));
147
148         value3 = pool.reserve();
149         assertNotNull(value3);
150         assertEquals(Integer.valueOf(MIN - 1), Integer.valueOf(value3.getId()));
151     }
152
153     /**
154      * Test that we can trim the pool to a desired size
155      *
156      * @throws PoolDrainedException
157      *             If the caller is trying to release or reserve an element from a drained pool
158      * @throws PoolExtensionException
159      *             If the pool cannot be extended
160      * @throws IllegalAccessException
161      *             if this Method object is enforcing Java language access control and the underlying method is
162      *             inaccessible.
163      * @throws IllegalArgumentException
164      *             if the method is an instance method and the specified object argument is not an instance of the class
165      *             or interface declaring the underlying method (or of a subclass or implementor thereof); if the number
166      *             of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or
167      *             if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal
168      *             parameter type by a method invocation conversion.
169      * @throws InvocationTargetException
170      *             if the underlying method throws an exception.
171      * @throws SecurityException
172      *             If a security manager, s, is present and any of the following conditions is met:
173      *             <ul>
174      *             <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared method</li>
175      *             <li>the caller's class loader is not the same as or an ancestor of the class loader for the current
176      *             class and invocation of s.checkPackageAccess() denies access to the package of this class</li>
177      *             </ul>
178      * @throws NoSuchMethodException
179      *             if a matching method is not found.
180      */
181     @SuppressWarnings("nls")
182     @Test
183     public void testTrim() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException,
184         PoolDrainedException, PoolExtensionException, NoSuchMethodException, SecurityException {
185
186         pool.setAllocator(this);
187         int SIZE = 50;
188         Testable[] array = new Testable[SIZE];
189
190         assertEquals(0, pool.getAllocatedSize());
191         for (int i = 0; i < SIZE; i++) {
192             array[i] = pool.reserve();
193         }
194         assertEquals(SIZE, pool.getAllocatedSize());
195
196         for (int i = 0; i < SIZE; i++) {
197             pool.release(array[i]);
198         }
199         assertEquals(0, pool.getAllocatedSize());
200
201         assertEquals(SIZE, pool.getFreeSize());
202
203         Method trimMethod = Pool.class.getDeclaredMethod("trim", new Class[] {
204             Integer.TYPE
205         });
206         trimMethod.setAccessible(true);
207         trimMethod.invoke(pool, new Object[] {
208             SIZE - MIN
209         });
210
211         assertEquals(MIN, pool.getFreeSize());
212     }
213
214     /**
215      * Test that we can drain a pool containing a mix of free and allocated elements
216      *
217      * @throws PoolDrainedException
218      *             If the caller is trying to release or reserve an element from a drained pool
219      * @throws PoolExtensionException
220      *             If the pool cannot be extended
221      * @throws IOException
222      *             if an I/O error occurs
223      */
224     @Test
225     public void testDrain() throws PoolExtensionException, PoolDrainedException, IOException {
226         int SIZE = 50;
227         int FREE = 20;
228         int ALLOC = SIZE - FREE;
229
230         Testable[] array = new Testable[SIZE];
231         pool.setAllocator(this);
232         pool.setDestructor(this);
233
234         assertFalse(pool.isDrained());
235
236         assertEquals(0, pool.getAllocatedSize());
237         for (int i = 0; i < SIZE; i++) {
238             array[i] = pool.reserve();
239         }
240         assertEquals(SIZE, pool.getAllocatedSize());
241
242         for (int i = 0; i < FREE; i++) {
243             array[i].close();
244         }
245         assertEquals(ALLOC, pool.getAllocatedSize());
246         assertEquals(FREE, pool.getFreeSize());
247
248         pool.drain();
249         assertEquals(0, pool.getFreeSize());
250         assertEquals(0, pool.getAllocatedSize());
251         assertTrue(pool.isDrained());
252
253         assertEquals(SIZE, destroyCount);
254     }
255
256     /**
257      * @see org.onap.appc.pool.Allocator#allocate(org.onap.appc.pool.Pool)
258      */
259     @Override
260     public Testable allocate(Pool<Testable> pool) {
261         Testable element = new Element(index++);
262         Testable ce = CachedElement.newInstance(pool, element, new Class[] {
263             Testable.class
264         });
265         return ce;
266     }
267
268     /**
269      * @see org.onap.appc.pool.Destructor#destroy(java.io.Closeable, org.onap.appc.pool.Pool)
270      */
271     @Override
272     public void destroy(Testable obj, Pool<Testable> pool) {
273         destroyCount++;
274     }
275 }