Switched from Dropwizard to Springboot
[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.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.onap.holmes.common.dcae.DcaeConfigurationsCache;
23 import org.onap.holmes.common.utils.SpringContextUtil;
24 import org.onap.holmes.dsa.dmaappolling.DMaaPResponseUtil;
25 import org.onap.holmes.dsa.dmaappolling.Subscriber;
26 import org.onap.holmes.engine.dmaap.SubscriberAction;
27 import org.powermock.api.easymock.PowerMock;
28 import org.powermock.core.classloader.annotations.PrepareForTest;
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 @PrepareForTest({SpringContextUtil.class})
37 public class ConfigFileScanningTaskTest {
38
39     @Test
40     public void run() {
41         PowerMock.mockStatic(SpringContextUtil.class);
42         SubscriberAction mockedSa = PowerMock.createMock(SubscriberAction.class);
43         EasyMock.expect(SpringContextUtil.getBean(SubscriberAction.class)).andReturn(mockedSa);
44         // This is invoked while executing new Subscriber().
45         EasyMock.expect(SpringContextUtil.getBean(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         PowerMock.mockStatic(SpringContextUtil.class);
64         SubscriberAction mockedSa = PowerMock.createMock(SubscriberAction.class);
65         // mocked objects will be only used once
66         EasyMock.expect(SpringContextUtil.getBean(SubscriberAction.class)).andReturn(mockedSa);
67         // This is invoked while executing new Subscriber().
68         EasyMock.expect(SpringContextUtil.getBean(DMaaPResponseUtil.class)).andReturn(new DMaaPResponseUtil());
69         mockedSa.addSubscriber(EasyMock.anyObject(Subscriber.class));
70         EasyMock.expectLastCall();
71
72         ConfigFileScanningTask cfst = new ConfigFileScanningTask(null);
73         String configFilePath = ConfigFileScanningTaskTest.class.getResource("/cfy.json").getFile();
74         Whitebox.setInternalState(cfst, "configFile", configFilePath);
75
76         PowerMock.replayAll();
77         cfst.run();
78         cfst.run();
79         PowerMock.verifyAll();
80     }
81 }