4af45e56c15bfd252caa657ed51bde07c13661d8
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.controlloop.eventmanager;
23
24 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.when;
31
32 import org.junit.jupiter.api.AfterAll;
33 import org.junit.jupiter.api.AfterEach;
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
37 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
38 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManagerImpl;
39 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManagerStub;
40 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
41
42 class EventManagerServicesTest {
43     private static final String FILEPFX = "eventService/";
44     private static final IllegalArgumentException EXPECTED_EXCEPTION =
45                     new IllegalArgumentException("expected exception");
46
47     private EventManagerServices services;
48
49     /**
50      * Configures HTTP clients.
51      */
52     @BeforeAll
53     public static void setUpBeforeClass() throws Exception {
54         // start with a clean slate
55         HttpClientFactoryInstance.getClientFactory().destroy();
56
57         SystemPersistenceConstants.getManager().setConfigurationDir("src/test/resources");
58
59         var props = SystemPersistenceConstants.getManager().getProperties("eventService/event-svc-http-client");
60         HttpClientFactoryInstance.getClientFactory().build(props);
61     }
62
63     @AfterAll
64     public static void teatDownBeforeClass() {
65         HttpClientFactoryInstance.getClientFactory().destroy();
66     }
67
68     @AfterEach
69     public void tearDown() {
70         closeDb();
71     }
72
73     @Test
74     void testEventManagerServices_testGetActorService() {
75         // try with guard disabled - should use DB stub
76         services = new EventManagerServices(FILEPFX + "event-svc-guard-disabled");
77         assertTrue(services.getDataManager() instanceof OperationHistoryDataManagerStub);
78         assertNotNull(services.getActorService());
79
80         // try with guard enabled - should create a DB connection
81         services = new EventManagerServices(FILEPFX + "event-svc-with-db");
82         assertTrue(services.getDataManager() instanceof OperationHistoryDataManagerImpl);
83         assertNotNull(services.getActorService());
84     }
85
86     @Test
87     void testStartActorService() {
88         // config file not found
89         assertThatIllegalStateException().isThrownBy(() -> new EventManagerServices("missing-config-file"));
90     }
91
92     @Test
93     void testIsGuardEnabled() {
94         // cannot check guard
95         services = new EventManagerServices(FILEPFX + "event-svc-no-guard-actor");
96         assertTrue(services.getDataManager() instanceof OperationHistoryDataManagerStub);
97
98         // force exception when checking for guard operator
99         services = new EventManagerServices(FILEPFX + "event-svc-with-db") {
100             @Override
101             public ActorService getActorService() {
102                 var svc = mock(ActorService.class);
103                 when(svc.getActor(any())).thenThrow(EXPECTED_EXCEPTION);
104                 return svc;
105             }
106         };
107         assertTrue(services.getDataManager() instanceof OperationHistoryDataManagerStub);
108     }
109
110     @Test
111     void testMakeDataManager() {
112         assertThatThrownBy(() -> new EventManagerServices(FILEPFX + "event-svc-invalid-db"))
113                         .isInstanceOf(IllegalArgumentException.class);
114     }
115
116
117     private void closeDb() {
118         if (services != null) {
119             services.getDataManager().stop();
120         }
121     }
122 }