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