Fix request handler class error
[appc.git] / appc-common / src / test / java / org / onap / appc / pool / PoolTest.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  * Modifications Copyright (C) 2018 IBM.
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26
27
28 package org.onap.appc.pool;
29
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.assertFalse;
32 import static org.junit.Assert.assertNotNull;
33 import static org.junit.Assert.assertNull;
34 import static org.junit.Assert.assertTrue;
35
36 import java.io.IOException;
37 import java.lang.reflect.InvocationTargetException;
38 import java.lang.reflect.Method;
39 import java.lang.reflect.Proxy;
40
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.onap.appc.pool.Allocator;
44 import org.onap.appc.pool.Destructor;
45 import org.onap.appc.pool.Pool;
46 import org.onap.appc.pool.PoolDrainedException;
47 import org.onap.appc.pool.PoolExtensionException;
48 import org.onap.appc.pool.PoolSpecificationException;
49 import org.onap.appc.pool.*;
50
51
52 public class PoolTest implements Allocator<Testable>, Destructor<Testable> {
53
54     private Pool<Testable> pool;
55     private static final int MIN = 10;
56     private static final int MAX = 100;
57     private int index = 0;
58     private int destroyCount = 0;
59
60     /**
61      * Set up the test by allocating a pool with MIN-MAX size (bounded pool)
62      *
63      * @throws PoolSpecificationException
64      *             If the minimum size is less than 0, or if the max size is non-zero and less than the min size.
65      */
66     @Before
67     public void setup() throws PoolSpecificationException {
68         pool = new Pool<>(MIN, MAX);
69         index = 0;
70         destroyCount = 0;
71     }
72
73     /**
74      * Test that trying to construct a pool with a bad minimum throws an exception
75      *
76      * @throws PoolSpecificationException
77      *             If the minimum size is less than 0, or if the max size is non-zero and less than the min size.
78      */
79     @Test(expected = PoolSpecificationException.class)
80     public void testInvalidMinSize() throws PoolSpecificationException {
81         pool = new Pool<>(-1, MAX);
82     }
83
84     /**
85      * Test that trying to construct a pool with a bad maximum throws an exception
86      *
87      * @throws PoolSpecificationException
88      *             If the minimum size is less than 0, or if the max size is non-zero and less than the min size.
89      */
90     @Test(expected = PoolSpecificationException.class)
91     public void testInvalidMaxSize() throws PoolSpecificationException {
92         pool = new Pool<>(MIN, -1);
93     }
94
95     /**
96      * Test creation of a pool where max is less than min fails
97      *
98      * @throws PoolSpecificationException
99      *             If the minimum size is less than 0, or if the max size is non-zero and less than the min size.
100      */
101     @Test(expected = PoolSpecificationException.class)
102     public void testInvalidSizeRange() throws PoolSpecificationException {
103         pool = new Pool<>(MAX, MIN);
104     }
105
106     /**
107      * Test state
108      */
109     @Test
110     public void testMinPool() {
111         assertEquals(MIN, pool.getMinPool());
112     }
113
114     /**
115      * Test state
116      */
117     @Test
118     public void testMaxPool() {
119         assertEquals(MAX, pool.getMaxPool());
120     }
121
122     /**
123      * Test state
124      */
125     @Test
126     public void testAllocator() {
127         assertNull(pool.getAllocator());
128         pool.setAllocator(this);
129         assertNotNull(pool.getAllocator());
130     }
131
132     /**
133      * Test state
134      */
135     @Test
136     public void testDestructor() {
137         assertNull(pool.getDestructor());
138         pool.setDestructor(this);
139         assertNotNull(pool.getDestructor());
140     }
141
142     /**
143      * Test that we can allocate and release elements and that the pool maintains them in MRU order
144      *
145      * @throws PoolExtensionException
146      *             If the pool cannot be extended
147      * @throws PoolDrainedException
148      *             If the caller is trying to reserve an element from a drained pool
149      */
150     @Test
151     public void testAllocateAndRelease() throws PoolExtensionException, PoolDrainedException {
152         pool.setAllocator(this);
153
154         assertFalse(pool.isDrained());
155
156         /*
157          * Allocate three elements
158          */
159         Testable value1 = pool.reserve();
160         assertNotNull(value1);
161         assertEquals(Integer.valueOf(MIN - 1), value1.getId());
162         assertEquals(1, pool.getAllocatedSize());
163         assertEquals(MIN - 1, pool.getFreeSize());
164         assertEquals(1, pool.getAllocatedSize());
165
166         Testable value2 = pool.reserve();
167         assertNotNull(value2);
168         assertEquals(Integer.valueOf(MIN - 2), value2.getId());
169         assertEquals(2, pool.getAllocatedSize());
170         assertEquals(MIN - 2, pool.getFreeSize());
171         assertEquals(2, pool.getAllocatedSize());
172
173         Testable value3 = pool.reserve();
174         assertNotNull(value3);
175         assertEquals(Integer.valueOf(MIN - 3), value3.getId());
176         assertEquals(3, pool.getAllocatedSize());
177         assertEquals(MIN - 3, pool.getFreeSize());
178         assertEquals(3, pool.getAllocatedSize());
179
180         /*
181          * Now, release them in the order obtained
182          */
183         pool.release(value1);
184         pool.release(value2);
185         pool.release(value3);
186
187         assertEquals(0, pool.getAllocatedSize());
188         assertEquals(MIN, pool.getFreeSize());
189
190         /*
191          * Now, allocate them again, but their values should be reversed (3, 2, 1) representing the most recently used
192          * to the least recently used.
193          */
194         value1 = pool.reserve();
195         assertNotNull(value1);
196         assertEquals(Integer.valueOf(MIN - 3), value1.getId());
197
198         value2 = pool.reserve();
199         assertNotNull(value2);
200         assertEquals(Integer.valueOf(MIN - 2), value2.getId());
201
202         value3 = pool.reserve();
203         assertNotNull(value3);
204         assertEquals(Integer.valueOf(MIN - 1), value3.getId());
205     }
206
207     /**
208      * Test that we can trim the pool to a desired size
209      *
210      * @throws PoolExtensionException
211      *             If the pool cannot be extended
212      * @throws NoSuchMethodException
213      *             if a matching method is not found.
214      * @throws SecurityException
215      *             if the request is denied.
216      * @throws IllegalAccessException
217      *             if this Method object is enforcing Java language access control and the underlying method is
218      *             inaccessible.
219      * @throws IllegalArgumentException
220      *             if the method is an instance method and the specified object argument is not an instance of the class
221      *             or interface declaring the underlying method (or of a subclass or implementor thereof); if the number
222      *             of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or
223      *             if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal
224      *             parameter type by a method invocation conversion.
225      * @throws InvocationTargetException
226      *             if the underlying method throws an exception.
227      * @throws PoolDrainedException
228      *             If the caller is trying to reserve an element from a drained pool
229      */
230     @SuppressWarnings("nls")
231     @Test
232     public void testTrim() throws PoolExtensionException, NoSuchMethodException, SecurityException,
233         IllegalAccessException, IllegalArgumentException, InvocationTargetException, PoolDrainedException {
234         pool.setAllocator(this);
235         int SIZE = 50;
236         Proxy[] array = new Proxy[SIZE];
237
238         assertEquals(0, pool.getAllocatedSize());
239         for (int i = 0; i < SIZE; i++) {
240             array[i] = (Proxy) pool.reserve();
241         }
242         assertEquals(SIZE, pool.getAllocatedSize());
243
244         for (int i = 0; i < SIZE; i++) {
245             pool.release((Testable) array[i]);
246         }
247         assertEquals(0, pool.getAllocatedSize());
248
249         assertEquals(SIZE, pool.getFreeSize());
250
251         Method trimMethod = Pool.class.getDeclaredMethod("trim", new Class[] {
252             Integer.TYPE
253         });
254         trimMethod.setAccessible(true);
255         trimMethod.invoke(pool, new Object[] {
256             SIZE - MIN
257         });
258
259         assertEquals(MIN, pool.getFreeSize());
260     }
261
262     /**
263      * Test that we can drain a pool containing a mix of free and allocated elements
264      *
265      * @throws PoolExtensionException
266      *             If the pool cannot be extended
267      * @throws PoolDrainedException
268      *             If the caller is trying to reserve an element from a drained pool
269      */
270     @Test
271     public void testDrain() throws PoolExtensionException, PoolDrainedException {
272         int SIZE = 50;
273         int FREE = 20;
274         int ALLOC = SIZE - FREE;
275
276         Proxy[] array = new Proxy[SIZE];
277         pool.setAllocator(this);
278         pool.setDestructor(this);
279
280         assertFalse(pool.isDrained());
281
282         assertEquals(0, pool.getAllocatedSize());
283         for (int i = 0; i < SIZE; i++) {
284             array[i] = (Proxy) pool.reserve();
285         }
286         assertEquals(SIZE, pool.getAllocatedSize());
287
288         for (int i = 0; i < FREE; i++) {
289             pool.release((Testable) array[i]);
290         }
291         assertEquals(ALLOC, pool.getAllocatedSize());
292         assertEquals(FREE, pool.getFreeSize());
293
294         pool.drain();
295         assertEquals(0, pool.getFreeSize());
296         assertEquals(0, pool.getAllocatedSize());
297         assertTrue(pool.isDrained());
298
299         assertEquals(SIZE, destroyCount);
300     }
301
302     /**
303      * @see org.onap.appc.pool.Destructor#destroy(java.io.Closeable, org.onap.appc.pool.Pool)
304      */
305     @Override
306     public void destroy(Testable obj, Pool<Testable> pool) {
307         destroyCount++;
308         try {
309             obj.close();
310         } catch (IOException e) {
311             e.printStackTrace();
312         }
313     }
314
315     /**
316      * @see org.onap.appc.pool.Allocator#allocate(org.onap.appc.pool.Pool)
317      */
318     @Override
319     public Testable allocate(Pool<Testable> pool) {
320         Testable e = new Element(index++);
321
322         return e;
323     }
324     
325     @Test
326     public void testGetAndSetProperties() throws PoolSpecificationException
327     {
328         pool= new Pool<Testable>(3, 5);
329         pool.setProperty("key1", "value1");
330         assertEquals("value1", pool.getProperty("key1"));
331     }
332 }