2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.batch.helpers;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.MockitoAnnotations.initMocks;
27 import java.io.IOException;
28 import java.lang.reflect.Field;
29 import java.lang.reflect.Modifier;
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;
39 import com.datastax.driver.core.Session;
41 public class JU_CQLBatchLoopTest {
55 CQLBatchLoop cqlBatchLoopObj;
58 public void setUp() throws APIException, IOException {
60 Mockito.doReturn(lg).when(trans).info();
61 Mockito.doReturn(new StringBuilder()).when(cqlBatchObj).begin();
62 cqlBatchLoopObj = new CQLBatchLoop(cqlBatchObj, 0, false);
66 public void testShowProgress() {
67 CQLBatchLoop tempLoopObj = cqlBatchLoopObj.showProgress();
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
77 } catch (SecurityException e) {
78 // TODO Auto-generated catch block
80 } catch (IllegalArgumentException e) {
81 // TODO Auto-generated catch block
83 } catch (IllegalAccessException e) {
84 // TODO Auto-generated catch block
90 public void testInc() {
91 StringBuilder sb = cqlBatchLoopObj.inc();
92 sb = cqlBatchLoopObj.inc();
93 assertTrue(cqlBatchLoopObj.batches() == 1);
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
102 } catch (SecurityException e) {
103 // TODO Auto-generated catch block
105 } catch (IllegalArgumentException e) {
106 // TODO Auto-generated catch block
108 } catch (IllegalAccessException e) {
109 // TODO Auto-generated catch block
112 cqlBatchLoopObj.inc();
113 assertTrue(cqlBatchLoopObj.batches() == 2);
114 System.out.println(sb.toString());
118 public void testFlush() {
121 f1 = CQLBatchLoop.class.getDeclaredField("i");
122 f1.setAccessible(true);
124 f1.set(cqlBatchLoopObj, -1);
125 } catch (NoSuchFieldException e) {
126 // TODO Auto-generated catch block
128 } catch (SecurityException e) {
129 // TODO Auto-generated catch block
131 } catch (IllegalArgumentException e) {
132 // TODO Auto-generated catch block
134 } catch (IllegalAccessException e) {
135 // TODO Auto-generated catch block
138 cqlBatchLoopObj.flush();
140 f = CQLBatchLoop.class.getDeclaredField("current");
141 f1 = CQLBatchLoop.class.getDeclaredField("i");
142 f.setAccessible(true);
143 f1.setAccessible(true);
145 Field modifiersField = Field.class.getDeclaredField("modifiers");
146 modifiersField.setAccessible(true);
147 modifiersField.setInt(f, f.getModifiers() & ~Modifier.FINAL);
149 StringBuilder sb = new StringBuilder("tets");
150 for (int i = 1; i < 25600; i++) {
151 sb = sb.append("test");
153 f.set(cqlBatchLoopObj, sb);
154 f1.set(cqlBatchLoopObj, 10);
155 } catch (NoSuchFieldException e) {
156 // TODO Auto-generated catch block
158 } catch (SecurityException e) {
159 // TODO Auto-generated catch block
161 } catch (IllegalArgumentException e) {
162 // TODO Auto-generated catch block
164 } catch (IllegalAccessException e) {
165 // TODO Auto-generated catch block
168 cqlBatchLoopObj.flush();
172 public void testReset() {
173 cqlBatchLoopObj.reset();
174 assertTrue(cqlBatchLoopObj.batches() == 0);
175 System.out.println(cqlBatchLoopObj.toString());