Add UT for actions
author6092002067 <wu.youbo@zte.com.cn>
Tue, 14 Mar 2017 11:12:07 +0000 (19:12 +0800)
committer6092002067 <wu.youbo@zte.com.cn>
Tue, 14 Mar 2017 11:12:07 +0000 (19:12 +0800)
Issue-ID: HOLMES-50

Change-Id: I295a72e8a8b522e48e43b8ee32152bdf71656e4d
Signed-off-by: youbowu <wu.youbo@zte.com.cn>
holmes-actions/src/main/java/org/openo/holmes/common/utils/MSBRegisterUtil.java
holmes-actions/src/test/java/org/openo/holmes/common/config/MicroServiceConfigTest.java [new file with mode: 0644]
holmes-actions/src/test/java/org/openo/holmes/common/utils/MSBRegisterUtilTest.java [new file with mode: 0644]

index e18a15c..91e28a6 100644 (file)
@@ -19,7 +19,6 @@ package org.openo.holmes.common.utils;
 import com.eclipsesource.jaxrs.consumer.ConsumerFactory;\r
 import java.io.IOException;\r
 import lombok.extern.slf4j.Slf4j;\r
-import org.glassfish.jersey.client.ClientConfig;\r
 import org.jvnet.hk2.annotations.Service;\r
 import org.openo.holmes.common.api.entity.ServiceRegisterEntity;\r
 import org.openo.holmes.common.config.MicroServiceConfig;\r
@@ -49,12 +48,11 @@ public class MSBRegisterUtil {
     }\r
 \r
     private boolean inner_register(ServiceRegisterEntity entity) {\r
-        ClientConfig config = new ClientConfig();\r
         try {\r
             log.info("msbServerAddr:" + MicroServiceConfig.getMsbServerAddr());\r
             log.info("entity:" + entity);\r
             MicroserviceBusRest resourceserviceproxy = ConsumerFactory.createConsumer(\r
-                    MicroServiceConfig.getMsbServerAddr(), config, MicroserviceBusRest.class);\r
+                    MicroServiceConfig.getMsbServerAddr(), MicroserviceBusRest.class);\r
             resourceserviceproxy.registerServce("false", entity);\r
         } catch (Exception error) {\r
             log.error("microservice register failed!" + error.getMessage());\r
diff --git a/holmes-actions/src/test/java/org/openo/holmes/common/config/MicroServiceConfigTest.java b/holmes-actions/src/test/java/org/openo/holmes/common/config/MicroServiceConfigTest.java
new file mode 100644 (file)
index 0000000..7ec7fe6
--- /dev/null
@@ -0,0 +1,39 @@
+/**\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.openo.holmes.common.config;\r
+\r
+import static org.hamcrest.core.IsEqual.equalTo;\r
+import static org.junit.Assert.assertThat;\r
+\r
+import org.junit.Test;\r
+\r
+public class MicroServiceConfigTest {\r
+\r
+    @Test\r
+    public void getMsbServerAddrTest() {\r
+        System.setProperty("MSB_ADDR", "test");\r
+        assertThat("http://test", equalTo(MicroServiceConfig.getMsbServerAddr()));\r
+        System.clearProperty("MSB_ADDR");\r
+    }\r
+\r
+    @Test\r
+    public void getServiceIpTest() {\r
+        System.setProperty("SERVICE_IP", "test");\r
+        assertThat("test", equalTo(MicroServiceConfig.getServiceIp()));\r
+        System.clearProperty("SERVICE_IP");\r
+    }\r
+}
\ No newline at end of file
diff --git a/holmes-actions/src/test/java/org/openo/holmes/common/utils/MSBRegisterUtilTest.java b/holmes-actions/src/test/java/org/openo/holmes/common/utils/MSBRegisterUtilTest.java
new file mode 100644 (file)
index 0000000..0845981
--- /dev/null
@@ -0,0 +1,72 @@
+/**\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.openo.holmes.common.utils;\r
+\r
+import com.eclipsesource.jaxrs.consumer.ConsumerFactory;\r
+import javax.ws.rs.QueryParam;\r
+import org.easymock.EasyMock;\r
+import org.junit.Rule;\r
+import org.junit.Test;\r
+import org.openo.holmes.common.api.entity.ServiceRegisterEntity;\r
+import org.openo.holmes.common.config.MicroServiceConfig;\r
+import org.openo.holmes.common.msb.MicroserviceBusRest;\r
+import org.powermock.api.easymock.PowerMock;\r
+import org.powermock.core.classloader.annotations.PrepareForTest;\r
+import org.powermock.modules.junit4.rule.PowerMockRule;\r
+\r
+@PrepareForTest(ConsumerFactory.class)\r
+public class MSBRegisterUtilTest {\r
+\r
+    @Rule\r
+    public PowerMockRule powerMockRule = new PowerMockRule();\r
+    private MSBRegisterUtil msbRegisterUtil = new MSBRegisterUtil();\r
+    private MicroserviceBusRest microserviceBusRest = new MicroserviceBusRestProxy();\r
+\r
+    @Test\r
+    public void registerTest() throws Exception {\r
+        ServiceRegisterEntity entity = initServiceEntity();\r
+        PowerMock.mockStatic(ConsumerFactory.class);\r
+        EasyMock.expect(ConsumerFactory\r
+                .createConsumer(EasyMock.anyObject(String.class), EasyMock.anyObject(Class.class)))\r
+                .andReturn(microserviceBusRest);\r
+        PowerMock.replayAll();\r
+\r
+        msbRegisterUtil.register(initServiceEntity());\r
+\r
+        PowerMock.verifyAll();\r
+    }\r
+\r
+    private ServiceRegisterEntity initServiceEntity() {\r
+        ServiceRegisterEntity serviceRegisterEntity = new ServiceRegisterEntity();\r
+        serviceRegisterEntity.setServiceName("holmes-rule-mgmt");\r
+        serviceRegisterEntity.setProtocol("REST");\r
+        serviceRegisterEntity.setVersion("v1");\r
+        serviceRegisterEntity.setUrl("/openoapi/holmes-rule-mgmt/v1");\r
+        serviceRegisterEntity.setSingleNode(MicroServiceConfig.getServiceIp(), "9101", 0);\r
+        serviceRegisterEntity.setVisualRange("1|0");\r
+        return serviceRegisterEntity;\r
+    }\r
+\r
+    class MicroserviceBusRestProxy implements MicroserviceBusRest {\r
+\r
+        @Override\r
+        public ServiceRegisterEntity registerServce(@QueryParam("createOrUpdate") String createOrUpdate,\r
+                ServiceRegisterEntity entity) throws Exception {\r
+            return null;\r
+        }\r
+    }\r
+}
\ No newline at end of file