2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2023 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.ophistory;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.awaitility.Awaitility.await;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Mockito.doAnswer;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.never;
31 import static org.mockito.Mockito.spy;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
35 import jakarta.persistence.EntityManagerFactory;
36 import java.time.Instant;
37 import java.util.Properties;
38 import java.util.UUID;
39 import java.util.concurrent.CountDownLatch;
40 import java.util.concurrent.TimeUnit;
41 import java.util.concurrent.atomic.AtomicInteger;
42 import java.util.function.Consumer;
43 import org.junit.After;
44 import org.junit.AfterClass;
45 import org.junit.Before;
46 import org.junit.BeforeClass;
47 import org.junit.Test;
48 import org.mockito.Mock;
49 import org.onap.policy.controlloop.ControlLoopOperation;
50 import org.onap.policy.controlloop.VirtualControlLoopEvent;
51 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManagerParams.OperationHistoryDataManagerParamsBuilder;
53 public class OperationHistoryDataManagerImplTest {
55 private static final IllegalStateException EXPECTED_EXCEPTION = new IllegalStateException("expected exception");
56 private static final String MY_LOOP_NAME = "my-loop-name";
57 private static final String MY_ACTOR = "my-actor";
58 private static final String MY_OPERATION = "my-operation";
59 private static final String MY_TARGET = "my-target";
60 private static final String MY_ENTITY = "my-entity";
61 private static final String REQ_ID = "my-request-id";
62 private static final int BATCH_SIZE = 5;
63 private static final int MAX_QUEUE_LENGTH = 23;
65 private static EntityManagerFactory emf;
67 private Thread thread = mock(Thread.class);
69 private OperationHistoryDataManagerParams params;
70 private Consumer<EntityManagerFactory> threadFunction;
71 private VirtualControlLoopEvent event;
72 private ControlLoopOperation operation;
73 private EntityManagerFactory emfSpy;
75 // decremented when the thread function completes
76 private CountDownLatch finished;
78 private OperationHistoryDataManagerImpl mgr;
82 * Sets up for all tests.
85 public static void setUpBeforeClass() {
86 OperationHistoryDataManagerParams params = makeBuilder().build();
88 // capture the entity manager factory for re-use
89 new OperationHistoryDataManagerImpl(params) {
91 protected EntityManagerFactory makeEntityManagerFactory(String opsHistPu, Properties props) {
92 emf = super.makeEntityManagerFactory(opsHistPu, props);
99 * Restores the environment after all tests.
102 public static void tearDownAfterClass() {
107 * Sets up for an individual test.
110 public void setUp() {
111 event = new VirtualControlLoopEvent();
112 event.setClosedLoopControlName(MY_LOOP_NAME);
113 event.setRequestId(UUID.randomUUID());
115 operation = new ControlLoopOperation();
116 operation.setActor(MY_ACTOR);
117 operation.setOperation(MY_OPERATION);
118 operation.setTarget(MY_TARGET);
119 operation.setSubRequestId(UUID.randomUUID().toString());
121 threadFunction = null;
122 finished = new CountDownLatch(1);
124 // prevent the "real" emf from being closed
126 doAnswer(ans -> null).when(emfSpy).close();
128 params = makeBuilder().build();
130 mgr = new PseudoThread();
135 public void tearDown() {
140 public void testConstructor() {
141 // use a thread and manager that haven't been started yet
142 thread = mock(Thread.class);
143 mgr = new PseudoThread();
145 // should not start the thread before start() is called
146 verify(thread, never()).start();
150 // should have started the thread
151 verify(thread).start();
153 // invalid properties
155 assertThatCode(() -> new PseudoThread()).isInstanceOf(IllegalArgumentException.class)
156 .hasMessageContaining("data-manager-properties");
160 public void testStart() {
161 // this should have no effect
166 // this should also have no effect
167 assertThatCode(() -> mgr.start()).doesNotThrowAnyException();
171 public void testStore_testStop() throws InterruptedException {
173 mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
177 assertEquals(1, mgr.getRecordsCommitted());
181 * Tests stop() when the manager isn't running.
184 public void testStopNotRunning() {
185 // use a manager that hasn't been started yet
186 mgr = new PseudoThread();
189 verify(emfSpy).close();
193 * Tests store() when it is already stopped.
196 public void testStoreAlreadyStopped() throws InterruptedException {
200 mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
202 assertEquals(0, mgr.getRecordsCommitted());
206 * Tests store() when when the queue is full.
209 public void testStoreTooManyItems() throws InterruptedException {
210 final int nextra = 5;
211 for (int nitems = 0; nitems < MAX_QUEUE_LENGTH + nextra; ++nitems) {
212 mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
217 assertEquals(MAX_QUEUE_LENGTH, mgr.getRecordsCommitted());
221 public void testRun() throws InterruptedException {
223 // trigger thread shutdown when it completes this batch
224 when(emfSpy.createEntityManager()).thenAnswer(ans -> {
226 return emf.createEntityManager();
230 mgr = new RealThread();
233 mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
234 mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
235 mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
239 verify(emfSpy).close();
241 assertEquals(3, mgr.getRecordsCommitted());
244 private void waitForThread() {
245 await().atMost(5, TimeUnit.SECONDS).until(() -> !thread.isAlive());
249 * Tests run() when the entity manager throws an exception.
252 public void testRunException() throws InterruptedException {
253 AtomicInteger count = new AtomicInteger(0);
255 when(emfSpy.createEntityManager()).thenAnswer(ans -> {
256 if (count.incrementAndGet() == 2) {
257 // interrupt during one of the attempts
261 // throw an exception for each record
262 throw EXPECTED_EXCEPTION;
266 mgr = new RealThread();
269 mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
270 mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
271 mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
275 verify(emfSpy).close();
279 * Tests storeRemainingRecords() when the entity manager throws an exception.
282 public void testStoreRemainingRecordsException() throws InterruptedException {
283 // arrange to throw an exception
284 when(emfSpy.createEntityManager()).thenThrow(EXPECTED_EXCEPTION);
286 mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
292 public void testStoreRecord() throws InterruptedException {
294 * Note: we change sub-request ID each time to guarantee that the records are
299 mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
302 operation = new ControlLoopOperation(operation);
303 operation.setSubRequestId(UUID.randomUUID().toString());
304 operation.setStart(Instant.now());
305 mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
307 // both start and end times
308 operation = new ControlLoopOperation(operation);
309 operation.setSubRequestId(UUID.randomUUID().toString());
310 operation.setEnd(Instant.now());
311 mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
314 operation = new ControlLoopOperation(operation);
315 operation.setSubRequestId(UUID.randomUUID().toString());
316 operation.setStart(null);
317 mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
321 // all of them should have been stored
322 assertEquals(4, mgr.getRecordsCommitted());
325 assertEquals(4, mgr.getRecordsInserted());
326 assertEquals(0, mgr.getRecordsUpdated());
330 * Tests storeRecord() when records are updated.
333 public void testStoreRecordUpdate() throws InterruptedException {
335 * Note: we do NOT change sub-request ID, so that records all refer to the same DB
340 operation.setStart(null);
341 mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
344 operation = new ControlLoopOperation(operation);
345 operation.setStart(Instant.now());
346 mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
348 // both start and end times
349 operation = new ControlLoopOperation(operation);
350 operation.setEnd(Instant.now());
351 mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
354 operation = new ControlLoopOperation(operation);
355 operation.setStart(null);
356 mgr.store(REQ_ID, event.getClosedLoopControlName(), event, MY_ENTITY, operation);
360 // all of them should have been stored
361 assertEquals(4, mgr.getRecordsCommitted());
363 // only one new record
364 assertEquals(1, mgr.getRecordsInserted());
366 // remainder were updates
367 assertEquals(3, mgr.getRecordsUpdated());
370 private void runThread() throws InterruptedException {
371 if (threadFunction == null) {
375 Thread thread2 = new Thread(() -> {
376 threadFunction.accept(emfSpy);
377 finished.countDown();
380 thread2.setDaemon(true);
385 assertTrue(finished.await(5, TimeUnit.SECONDS));
388 private static OperationHistoryDataManagerParamsBuilder makeBuilder() {
390 return OperationHistoryDataManagerParams.builder()
391 .url("jdbc:h2:mem:" + OperationHistoryDataManagerImplTest.class.getSimpleName())
393 .driver("org.h2.Driver")
396 .batchSize(BATCH_SIZE)
397 .maxQueueLength(MAX_QUEUE_LENGTH);
402 * Manager that uses the shared DB.
404 private class SharedDb extends OperationHistoryDataManagerImpl {
410 protected EntityManagerFactory makeEntityManagerFactory(String opsHistPu, Properties props) {
411 // re-use the same factory to avoid re-creating the DB for each test
417 * Manager that uses the shared DB and a pseudo thread.
419 private class PseudoThread extends SharedDb {
422 protected Thread makeThread(EntityManagerFactory emfactory, Consumer<EntityManagerFactory> command) {
423 threadFunction = command;
429 * Manager that uses the shared DB and catches the thread.
431 private class RealThread extends SharedDb {
434 protected Thread makeThread(EntityManagerFactory emfactory, Consumer<EntityManagerFactory> command) {
435 thread = super.makeThread(emfactory, command);