Migrate from DW to Springboot
[holmes/rule-management.git] / rulemgt / src / test / java / org / onap / holmes / rulemgt / InitializerTest.java
1 /**
2  * Copyright 2020-2022 ZTE Corporation.
3  * <p>
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  * <p>
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * <p>
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14
15 package org.onap.holmes.rulemgt;
16
17 import org.easymock.EasyMock;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.onap.holmes.common.config.MicroServiceConfig;
21 import org.onap.holmes.common.utils.MsbRegister;
22 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
23 import org.powermock.api.easymock.PowerMock;
24 import org.powermock.core.classloader.annotations.PrepareForTest;
25 import org.powermock.modules.junit4.PowerMockRunner;
26
27 import java.util.concurrent.TimeUnit;
28
29 @RunWith(PowerMockRunner.class)
30 @PrepareForTest(MicroServiceConfig.class)
31 public class InitializerTest {
32
33     @Test
34     public void process() throws Exception {
35         MsbRegister mockedMsbRegister = PowerMock.createMock(MsbRegister.class);
36         Initializer initializer = new Initializer(mockedMsbRegister);
37
38         PowerMock.mockStaticPartial(MicroServiceConfig.class, "getMicroServiceIpAndPort", "getEnv");
39         EasyMock.expect(MicroServiceConfig.getMicroServiceIpAndPort()).andReturn(new String[]{"127.0.0.1", "443"});
40         System.setProperty("ENABLE_ENCRYPT", "true");
41
42         mockedMsbRegister.register2Msb(EasyMock.anyObject(MicroServiceInfo.class));
43         EasyMock.expectLastCall();
44
45         PowerMock.replayAll();
46
47         setReadyFlagAfter(3);
48
49         initializer.run(null);
50
51         TimeUnit.SECONDS.sleep(6);
52
53         PowerMock.verifyAll();
54     }
55
56     private void setReadyFlagAfter(final int second) {
57         new Thread(() -> {
58             try {
59                 TimeUnit.SECONDS.sleep(second);
60             } catch (InterruptedException e) {
61                 e.printStackTrace();
62             }
63             Initializer.setReadyForMsbReg(true);
64         }).start();
65     }
66 }