aef5d22c4de618fb40b0fd6881c99d217b523513
[aaf/authz.git] / auth / auth-batch / src / test / java / org / onap / aaf / auth / batch / helpers / JU_CQLBatchLoopTest.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 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.auth.batch.helpers;
23
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.MockitoAnnotations.initMocks;
26
27 import java.io.IOException;
28 import java.lang.reflect.Field;
29 import java.lang.reflect.Modifier;
30
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.onap.aaf.auth.env.AuthzTrans;
36 import org.onap.aaf.misc.env.APIException;
37 import org.onap.aaf.misc.env.LogTarget;
38
39 import com.datastax.driver.core.Session;
40
41 public class JU_CQLBatchLoopTest {
42
43     @Mock
44     AuthzTrans trans;
45
46     @Mock
47     Session session;
48
49     @Mock
50     LogTarget lg;
51
52     @Mock
53     CQLBatch cqlBatchObj;
54
55     CQLBatchLoop cqlBatchLoopObj;
56
57     @Before
58     public void setUp() throws APIException, IOException {
59         initMocks(this);
60         Mockito.doReturn(lg).when(trans).info();
61         Mockito.doReturn(new StringBuilder()).when(cqlBatchObj).begin();
62         cqlBatchLoopObj = new CQLBatchLoop(cqlBatchObj, 0, false);
63     }
64
65     @Test
66     public void testShowProgress() {
67         CQLBatchLoop tempLoopObj = cqlBatchLoopObj.showProgress();
68         Field f;
69         try {
70             f = CQLBatchLoop.class.getDeclaredField("showProgress");
71             f.setAccessible(true);
72             assertTrue(cqlBatchLoopObj.total() == 0);
73             assertTrue(f.getBoolean(tempLoopObj));
74         } catch (NoSuchFieldException e) {
75             // TODO Auto-generated catch block
76             e.printStackTrace();
77         } catch (SecurityException e) {
78             // TODO Auto-generated catch block
79             e.printStackTrace();
80         } catch (IllegalArgumentException e) {
81             // TODO Auto-generated catch block
82             e.printStackTrace();
83         } catch (IllegalAccessException e) {
84             // TODO Auto-generated catch block
85             e.printStackTrace();
86         }
87     }
88
89     @Test
90     public void testInc() {
91         StringBuilder sb = cqlBatchLoopObj.inc();
92         sb = cqlBatchLoopObj.inc();
93         assertTrue(cqlBatchLoopObj.batches() == 1);
94         Field f;
95         try {
96             f = CQLBatchLoop.class.getDeclaredField("showProgress");
97             f.setAccessible(true);
98             f.set(cqlBatchLoopObj, true);
99         } catch (NoSuchFieldException e) {
100             // TODO Auto-generated catch block
101             e.printStackTrace();
102         } catch (SecurityException e) {
103             // TODO Auto-generated catch block
104             e.printStackTrace();
105         } catch (IllegalArgumentException e) {
106             // TODO Auto-generated catch block
107             e.printStackTrace();
108         } catch (IllegalAccessException e) {
109             // TODO Auto-generated catch block
110             e.printStackTrace();
111         }
112         cqlBatchLoopObj.inc();
113         assertTrue(cqlBatchLoopObj.batches() == 2);
114         System.out.println(sb.toString());
115     }
116
117     @Test
118     public void testFlush() {
119         Field f, f1;
120         try {
121             f1 = CQLBatchLoop.class.getDeclaredField("i");
122             f1.setAccessible(true);
123
124             f1.set(cqlBatchLoopObj, -1);
125         } catch (NoSuchFieldException e) {
126             // TODO Auto-generated catch block
127             e.printStackTrace();
128         } catch (SecurityException e) {
129             // TODO Auto-generated catch block
130             e.printStackTrace();
131         } catch (IllegalArgumentException e) {
132             // TODO Auto-generated catch block
133             e.printStackTrace();
134         } catch (IllegalAccessException e) {
135             // TODO Auto-generated catch block
136             e.printStackTrace();
137         }
138         cqlBatchLoopObj.flush();
139         try {
140             f = CQLBatchLoop.class.getDeclaredField("current");
141             f1 = CQLBatchLoop.class.getDeclaredField("i");
142             f.setAccessible(true);
143             f1.setAccessible(true);
144
145             Field modifiersField = Field.class.getDeclaredField("modifiers");
146             modifiersField.setAccessible(true);
147             modifiersField.setInt(f, f.getModifiers() & ~Modifier.FINAL);
148
149             StringBuilder sb = new StringBuilder("tets");
150             for (int i = 1; i < 25600; i++) {
151                 sb = sb.append("test");
152             }
153             f.set(cqlBatchLoopObj, sb);
154             f1.set(cqlBatchLoopObj, 10);
155         } catch (NoSuchFieldException e) {
156             // TODO Auto-generated catch block
157             e.printStackTrace();
158         } catch (SecurityException e) {
159             // TODO Auto-generated catch block
160             e.printStackTrace();
161         } catch (IllegalArgumentException e) {
162             // TODO Auto-generated catch block
163             e.printStackTrace();
164         } catch (IllegalAccessException e) {
165             // TODO Auto-generated catch block
166             e.printStackTrace();
167         }
168         cqlBatchLoopObj.flush();
169     }
170
171     @Test
172     public void testReset() {
173         cqlBatchLoopObj.reset();
174         assertTrue(cqlBatchLoopObj.batches() == 0);
175         System.out.println(cqlBatchLoopObj.toString());
176     }
177 }