b663133402f7fa2b8759ff9a4801855fbfc1db80
[holmes/engine-management.git] / engine-d / src / test / java / org / onap / holmes / engine / dcae / ConfigFileScanningTaskTest.java
1 /**
2  * Copyright 2021 ZTE Corporation.
3  * <p>
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.holmes.engine.dcae;
18
19 import org.easymock.EasyMock;
20 import org.glassfish.hk2.api.ServiceLocator;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.onap.holmes.common.dcae.DcaeConfigurationsCache;
24 import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder;
25 import org.onap.holmes.dsa.dmaappolling.DMaaPResponseUtil;
26 import org.onap.holmes.dsa.dmaappolling.Subscriber;
27 import org.onap.holmes.engine.dmaap.SubscriberAction;
28 import org.powermock.api.easymock.PowerMock;
29 import org.powermock.modules.junit4.PowerMockRunner;
30 import org.powermock.reflect.Whitebox;
31
32 import static org.hamcrest.core.IsEqual.equalTo;
33 import static org.junit.Assert.assertThat;
34
35 @RunWith(PowerMockRunner.class)
36 public class ConfigFileScanningTaskTest {
37
38     @Test
39     public void run() {
40         ServiceLocator mockedSl = PowerMock.createMock(ServiceLocator.class);
41         SubscriberAction mockedSa = PowerMock.createMock(SubscriberAction.class);
42         ServiceLocatorHolder.setLocator(mockedSl);
43         EasyMock.expect(mockedSl.getService(SubscriberAction.class)).andReturn(mockedSa);
44         // This is invoked while executing new Subscriber().
45         EasyMock.expect(mockedSl.getService(DMaaPResponseUtil.class)).andReturn(new DMaaPResponseUtil());
46         mockedSa.addSubscriber(EasyMock.anyObject(Subscriber.class));
47         EasyMock.expectLastCall();
48
49         ConfigFileScanningTask cfst = new ConfigFileScanningTask(null);
50         String configFilePath = ConfigFileScanningTaskTest.class.getResource("/cfy.json").getFile();
51         Whitebox.setInternalState(cfst, "configFile", configFilePath);
52
53         PowerMock.replayAll();
54         cfst.run();
55         PowerMock.verifyAll();
56
57         assertThat(DcaeConfigurationsCache.getPubSecInfo("dcae_cl_out").getDmaapInfo().getTopicUrl(),
58                 equalTo("http://message-router.onap:3904/events/unauthenticated.DCAE_CL_OUTPUT"));
59     }
60
61     @Test
62     public void run_config_not_changed() {
63         ServiceLocator mockedSl = PowerMock.createMock(ServiceLocator.class);
64         SubscriberAction mockedSa = PowerMock.createMock(SubscriberAction.class);
65         ServiceLocatorHolder.setLocator(mockedSl);
66         // mocked objects will be only used once
67         EasyMock.expect(mockedSl.getService(SubscriberAction.class)).andReturn(mockedSa);
68         // This is invoked while executing new Subscriber().
69         EasyMock.expect(mockedSl.getService(DMaaPResponseUtil.class)).andReturn(new DMaaPResponseUtil());
70         mockedSa.addSubscriber(EasyMock.anyObject(Subscriber.class));
71         EasyMock.expectLastCall();
72
73         ConfigFileScanningTask cfst = new ConfigFileScanningTask(null);
74         String configFilePath = ConfigFileScanningTaskTest.class.getResource("/cfy.json").getFile();
75         Whitebox.setInternalState(cfst, "configFile", configFilePath);
76
77         PowerMock.replayAll();
78         cfst.run();
79         cfst.run();
80         PowerMock.verifyAll();
81     }
82 }