Add UT for actions
[holmes/common.git] / holmes-actions / src / test / java / org / openo / holmes / common / utils / MSBRegisterUtilTest.java
1 /**\r
2  * Copyright 2017 ZTE Corporation.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package org.openo.holmes.common.utils;\r
18 \r
19 import com.eclipsesource.jaxrs.consumer.ConsumerFactory;\r
20 import javax.ws.rs.QueryParam;\r
21 import org.easymock.EasyMock;\r
22 import org.junit.Rule;\r
23 import org.junit.Test;\r
24 import org.openo.holmes.common.api.entity.ServiceRegisterEntity;\r
25 import org.openo.holmes.common.config.MicroServiceConfig;\r
26 import org.openo.holmes.common.msb.MicroserviceBusRest;\r
27 import org.powermock.api.easymock.PowerMock;\r
28 import org.powermock.core.classloader.annotations.PrepareForTest;\r
29 import org.powermock.modules.junit4.rule.PowerMockRule;\r
30 \r
31 @PrepareForTest(ConsumerFactory.class)\r
32 public class MSBRegisterUtilTest {\r
33 \r
34     @Rule\r
35     public PowerMockRule powerMockRule = new PowerMockRule();\r
36     private MSBRegisterUtil msbRegisterUtil = new MSBRegisterUtil();\r
37     private MicroserviceBusRest microserviceBusRest = new MicroserviceBusRestProxy();\r
38 \r
39     @Test\r
40     public void registerTest() throws Exception {\r
41         ServiceRegisterEntity entity = initServiceEntity();\r
42         PowerMock.mockStatic(ConsumerFactory.class);\r
43         EasyMock.expect(ConsumerFactory\r
44                 .createConsumer(EasyMock.anyObject(String.class), EasyMock.anyObject(Class.class)))\r
45                 .andReturn(microserviceBusRest);\r
46         PowerMock.replayAll();\r
47 \r
48         msbRegisterUtil.register(initServiceEntity());\r
49 \r
50         PowerMock.verifyAll();\r
51     }\r
52 \r
53     private ServiceRegisterEntity initServiceEntity() {\r
54         ServiceRegisterEntity serviceRegisterEntity = new ServiceRegisterEntity();\r
55         serviceRegisterEntity.setServiceName("holmes-rule-mgmt");\r
56         serviceRegisterEntity.setProtocol("REST");\r
57         serviceRegisterEntity.setVersion("v1");\r
58         serviceRegisterEntity.setUrl("/openoapi/holmes-rule-mgmt/v1");\r
59         serviceRegisterEntity.setSingleNode(MicroServiceConfig.getServiceIp(), "9101", 0);\r
60         serviceRegisterEntity.setVisualRange("1|0");\r
61         return serviceRegisterEntity;\r
62     }\r
63 \r
64     class MicroserviceBusRestProxy implements MicroserviceBusRest {\r
65 \r
66         @Override\r
67         public ServiceRegisterEntity registerServce(@QueryParam("createOrUpdate") String createOrUpdate,\r
68                 ServiceRegisterEntity entity) throws Exception {\r
69             return null;\r
70         }\r
71     }\r
72 }