b32fb4438fee196317172336d9abe068f37dd75d
[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.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.utils.PropertyUtil;
41
42 public 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     @BeforeClass
53     public static void setUpBeforeClass() throws Exception {
54         // start with a clean slate
55         HttpClientFactoryInstance.getClientFactory().destroy();
56
57         Properties props =
58                         PropertyUtil.getProperties("src/test/resources/eventService/event-svc-http-client.properties");
59         HttpClientFactoryInstance.getClientFactory().build(props);
60     }
61
62     @AfterClass
63     public static void teatDownBeforeClass() {
64         HttpClientFactoryInstance.getClientFactory().destroy();
65     }
66
67     @After
68     public void tearDown() {
69         closeDb();
70     }
71
72     @Test
73     public void testEventManagerServices_testGetActorService() {
74         // try with guard disabled - should use DB stub
75         services = new EventManagerServices(FILEPFX + "event-svc-guard-disabled.properties");
76         assertTrue(services.getDataManager() instanceof OperationHistoryDataManagerStub);
77         assertNotNull(services.getActorService());
78
79         // try with guard enabled - should create a DB connection
80         services = new EventManagerServices(FILEPFX + "event-svc-with-db.properties");
81         assertTrue(services.getDataManager() instanceof OperationHistoryDataManagerImpl);
82         assertNotNull(services.getActorService());
83     }
84
85     @Test
86     public void testStartActorService() {
87         // config file not found
88         assertThatIllegalStateException().isThrownBy(() -> new EventManagerServices("missing-config-file"));
89     }
90
91     @Test
92     public void testIsGuardEnabled() {
93         // cannot check guard
94         services = new EventManagerServices(FILEPFX + "event-svc-no-guard-actor.properties");
95         assertTrue(services.getDataManager() instanceof OperationHistoryDataManagerStub);
96
97         // force exception when checking for guard operator
98         services = new EventManagerServices(FILEPFX + "event-svc-with-db.properties") {
99             @Override
100             public ActorService getActorService() {
101                 ActorService svc = mock(ActorService.class);
102                 when(svc.getActor(any())).thenThrow(EXPECTED_EXCEPTION);
103                 return svc;
104             }
105         };
106         assertTrue(services.getDataManager() instanceof OperationHistoryDataManagerStub);
107     }
108
109     @Test
110     public void testMakeDataManager() {
111         assertThatThrownBy(() -> new EventManagerServices(FILEPFX + "event-svc-invalid-db.properties"));
112     }
113
114
115     private void closeDb() {
116         if (services != null) {
117             services.getDataManager().stop();
118         }
119     }
120 }