fix powermock issue
[msb/apigateway.git] / apiroute / apiroute-service / src / test / java / org / onap / msb / apiroute / wrapper / service / MicroServiceFullServiceTest.java
index 7ca2ff7..dab7e61 100644 (file)
@@ -1,6 +1,33 @@
+/*******************************************************************************
+ * Copyright 2016-2017 ZTE, Inc. and others.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ ******************************************************************************/
 package org.onap.msb.apiroute.wrapper.service;
 
-import com.fiftyonred.mock_jedis.MockJedisPool;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.powermock.api.mockito.PowerMockito.when;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -8,31 +35,27 @@ import org.junit.runner.RunWith;
 import org.onap.msb.apiroute.api.MicroServiceFullInfo;
 import org.onap.msb.apiroute.api.Node;
 import org.onap.msb.apiroute.wrapper.dao.RedisAccessWrapper;
-import org.onap.msb.apiroute.wrapper.service.MicroServiceFullService;
 import org.onap.msb.apiroute.wrapper.util.JedisUtil;
 import org.onap.msb.apiroute.wrapper.util.MicroServiceUtil;
 import org.powermock.api.mockito.PowerMockito;
 import org.powermock.core.classloader.annotations.PowerMockIgnore;
 import org.powermock.core.classloader.annotations.PrepareForTest;
 import org.powermock.modules.junit4.PowerMockRunner;
-import redis.clients.jedis.JedisPool;
-import redis.clients.jedis.JedisPoolConfig;
 
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.Method;
-import java.util.*;
+import com.fiftyonred.mock_jedis.MockJedisPool;
 
-import static org.junit.Assert.*;
-import static org.powermock.api.mockito.PowerMockito.when;
+import redis.clients.jedis.JedisPool;
+import redis.clients.jedis.JedisPoolConfig;
 
 @RunWith(PowerMockRunner.class)
-@PrepareForTest({JedisUtil.class,RedisAccessWrapper.class})
-@PowerMockIgnore( {"javax.management.*"})
+@PrepareForTest({JedisUtil.class, RedisAccessWrapper.class})
+@PowerMockIgnore({"javax.management.*", "jdk.internal.reflect.*"})
 public class MicroServiceFullServiceTest {
     private static MicroServiceFullService microServiceFullService = null;
     private static Comparator<MicroServiceFullInfo> serviceComparator = null;
+
     @BeforeClass
-    public static void setUp() throws Exception{
+    public static void setUp() throws Exception {
         microServiceFullService = MicroServiceFullService.getInstance();
         serviceComparator = new Comparator<MicroServiceFullInfo>() {
             @Override
@@ -45,11 +68,12 @@ public class MicroServiceFullServiceTest {
             }
         };
     }
+
     @Before
     public void setUpBeforeTest() throws Exception {
         final JedisPool mockJedisPool = new MockJedisPool(new JedisPoolConfig(), "localhost");
         PowerMockito.mockStatic(JedisUtil.class);
-        JedisUtil jedisUtil=PowerMockito.mock(JedisUtil.class);
+        JedisUtil jedisUtil = PowerMockito.mock(JedisUtil.class);
         when(jedisUtil.borrowJedisInstance()).thenReturn(mockJedisPool.getResource());
 
         PowerMockito.replace(PowerMockito.method(RedisAccessWrapper.class, "filterKeys")).with(new InvocationHandler() {
@@ -61,15 +85,16 @@ public class MicroServiceFullServiceTest {
     }
 
     @Test
-    public void testExistsMicroServiceInstance_notExist(){
+    public void testExistsMicroServiceInstance_notExist() {
         try {
             assertFalse(microServiceFullService.existsMicroServiceInstance("notExist", "v1"));
         } catch (Exception e) {
-            assert false:"throw exception means error occured!"+e.getMessage();
+            assert false : "throw exception means error occured!" + e.getMessage();
         }
     }
+
     @Test
-    public void testExistsMicroServiceInstance_Exist(){
+    public void testExistsMicroServiceInstance_Exist() {
         MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo();
         microServiceFullInfo.setServiceName("testService");
         microServiceFullInfo.setVersion("v1");
@@ -79,20 +104,20 @@ public class MicroServiceFullServiceTest {
         microServiceFullInfo.setProtocol("http");
         microServiceFullInfo.setEnable_ssl(false);
         Set<Node> nodeSet = new HashSet<>();
-        nodeSet.add(new Node("10.74.148.88","8080"));
+        nodeSet.add(new Node("10.74.148.88", "8080"));
         microServiceFullInfo.setNodes(nodeSet);
         try {
             assertFalse(microServiceFullService.existsMicroServiceInstance("testService", "v1"));
             microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo);
             assertTrue(microServiceFullService.existsMicroServiceInstance("testService", "v1"));
         } catch (Exception e) {
-            assert false:"throw exception means error occured!"+e.getMessage();
+            assert false : "throw exception means error occured!" + e.getMessage();
         }
     }
 
 
     @Test
-    public void testSaveMicroServiceInfo2Redis(){
+    public void testSaveMicroServiceInfo2Redis() {
         MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo();
         microServiceFullInfo.setServiceName("testService");
         microServiceFullInfo.setVersion("v1");
@@ -102,7 +127,7 @@ public class MicroServiceFullServiceTest {
         microServiceFullInfo.setProtocol("http");
         microServiceFullInfo.setEnable_ssl(false);
         Set<Node> nodeSet = new HashSet<>();
-        nodeSet.add(new Node("10.74.148.88","8080"));
+        nodeSet.add(new Node("10.74.148.88", "8080"));
         microServiceFullInfo.setNodes(nodeSet);
         try {
             microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo);
@@ -110,12 +135,12 @@ public class MicroServiceFullServiceTest {
             assertEquals(microServiceFullInfo, actual);
         } catch (Exception e) {
             e.printStackTrace();
-            assert false:"throw exception means error occured!"+e.getMessage();
+            assert false : "throw exception means error occured!" + e.getMessage();
         }
     }
 
     @Test
-    public void testDeleteMicroService(){
+    public void testDeleteMicroService() {
         MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo();
         microServiceFullInfo.setServiceName("testService");
         microServiceFullInfo.setVersion("v1");
@@ -125,21 +150,21 @@ public class MicroServiceFullServiceTest {
         microServiceFullInfo.setProtocol("http");
         microServiceFullInfo.setEnable_ssl(false);
         Set<Node> nodeSet = new HashSet<>();
-        nodeSet.add(new Node("10.74.148.88","8080"));
+        nodeSet.add(new Node("10.74.148.88", "8080"));
         microServiceFullInfo.setNodes(nodeSet);
         try {
             microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo);
             assertTrue(microServiceFullService.existsMicroServiceInstance("testService", "v1"));
-            microServiceFullService.deleteMicroService("testService","v1");
+            microServiceFullService.deleteMicroService("testService", "v1");
             assertFalse(microServiceFullService.existsMicroServiceInstance("testService", "v1"));
         } catch (Exception e) {
-            assert false:"throw exception means error occured!"+e.getMessage();
+            assert false : "throw exception means error occured!" + e.getMessage();
         }
     }
 
 
     @Test
-    public void testUpdateMicroServiceStatus(){
+    public void testUpdateMicroServiceStatus() {
         MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo();
         microServiceFullInfo.setServiceName("testService");
         microServiceFullInfo.setVersion("v1");
@@ -149,20 +174,20 @@ public class MicroServiceFullServiceTest {
         microServiceFullInfo.setProtocol("http");
         microServiceFullInfo.setEnable_ssl(false);
         Set<Node> nodeSet = new HashSet<>();
-        nodeSet.add(new Node("10.74.148.88","8080"));
+        nodeSet.add(new Node("10.74.148.88", "8080"));
         microServiceFullInfo.setNodes(nodeSet);
         try {
             microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo);
-            assertEquals("1", microServiceFullService.getMicroServiceInstance("testService","v1").getStatus());
+            assertEquals("1", microServiceFullService.getMicroServiceInstance("testService", "v1").getStatus());
             microServiceFullService.updateMicroServiceStatus("testService", "v1", "0");
             assertEquals("0", microServiceFullService.getMicroServiceInstance("testService", "v1").getStatus());
         } catch (Exception e) {
-            assert false:"throw exception means error occured!"+e.getMessage();
+            assert false : "throw exception means error occured!" + e.getMessage();
         }
     }
 
     @Test
-    public void testGetAllMicroServiceKey(){
+    public void testGetAllMicroServiceKey() {
         MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo();
         microServiceFullInfo.setServiceName("testService");
         microServiceFullInfo.setVersion("v1");
@@ -172,7 +197,7 @@ public class MicroServiceFullServiceTest {
         microServiceFullInfo.setProtocol("http");
         microServiceFullInfo.setEnable_ssl(false);
         Set<Node> nodeSet = new HashSet<>();
-        nodeSet.add(new Node("10.74.148.88","8080"));
+        nodeSet.add(new Node("10.74.148.88", "8080"));
         microServiceFullInfo.setNodes(nodeSet);
 
         MicroServiceFullInfo microServiceFullInfo2 = new MicroServiceFullInfo();
@@ -184,7 +209,7 @@ public class MicroServiceFullServiceTest {
         microServiceFullInfo2.setProtocol("http");
         microServiceFullInfo2.setEnable_ssl(false);
         Set<Node> nodeSet2 = new HashSet<>();
-        nodeSet2.add(new Node("10.74.148.88","8081"));
+        nodeSet2.add(new Node("10.74.148.88", "8081"));
         microServiceFullInfo2.setNodes(nodeSet2);
 
         MicroServiceFullInfo microServiceFullInfo3 = new MicroServiceFullInfo();
@@ -196,7 +221,7 @@ public class MicroServiceFullServiceTest {
         microServiceFullInfo3.setProtocol("http");
         microServiceFullInfo3.setEnable_ssl(false);
         Set<Node> nodeSet3 = new HashSet<>();
-        nodeSet3.add(new Node("10.74.148.89","8080"));
+        nodeSet3.add(new Node("10.74.148.89", "8080"));
         microServiceFullInfo3.setNodes(nodeSet3);
 
         try {
@@ -205,19 +230,19 @@ public class MicroServiceFullServiceTest {
             microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo3);
 
             Set<String> result = microServiceFullService.getAllMicroServiceKey();
-            final Set<String> expected =new HashSet<String>();
+            final Set<String> expected = new HashSet<String>();
             expected.add("testService");
             expected.add("testService2");
 
             assertEquals(expected, result);
         } catch (Exception e) {
-            assert false:"throw exception means error occured!"+e.getMessage();
+            assert false : "throw exception means error occured!" + e.getMessage();
         }
 
     }
 
     @Test
-    public void testGetAllVersionsOfTheService(){
+    public void testGetAllVersionsOfTheService() {
         MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo();
         microServiceFullInfo.setServiceName("testService");
         microServiceFullInfo.setVersion("v1");
@@ -227,7 +252,7 @@ public class MicroServiceFullServiceTest {
         microServiceFullInfo.setProtocol("http");
         microServiceFullInfo.setEnable_ssl(false);
         Set<Node> nodeSet = new HashSet<>();
-        nodeSet.add(new Node("10.74.148.88","8080"));
+        nodeSet.add(new Node("10.74.148.88", "8080"));
         microServiceFullInfo.setNodes(nodeSet);
 
         MicroServiceFullInfo microServiceFullInfo2 = new MicroServiceFullInfo();
@@ -239,7 +264,7 @@ public class MicroServiceFullServiceTest {
         microServiceFullInfo2.setProtocol("http");
         microServiceFullInfo2.setEnable_ssl(false);
         Set<Node> nodeSet2 = new HashSet<>();
-        nodeSet2.add(new Node("10.74.148.88","8081"));
+        nodeSet2.add(new Node("10.74.148.88", "8081"));
         microServiceFullInfo2.setNodes(nodeSet2);
 
         MicroServiceFullInfo microServiceFullInfo3 = new MicroServiceFullInfo();
@@ -251,7 +276,7 @@ public class MicroServiceFullServiceTest {
         microServiceFullInfo3.setProtocol("http");
         microServiceFullInfo3.setEnable_ssl(false);
         Set<Node> nodeSet3 = new HashSet<>();
-        nodeSet3.add(new Node("10.74.148.89","8080"));
+        nodeSet3.add(new Node("10.74.148.89", "8080"));
         microServiceFullInfo3.setNodes(nodeSet3);
 
         try {
@@ -266,17 +291,17 @@ public class MicroServiceFullServiceTest {
             expected.add(microServiceFullInfo);
             expected.add(microServiceFullInfo3);
 
-            Collections.sort(expected,serviceComparator);
-            Collections.sort(result,serviceComparator);
+            Collections.sort(expected, serviceComparator);
+            Collections.sort(result, serviceComparator);
             assertEquals(expected, result);
         } catch (Exception e) {
-            assert false:"throw exception means error occured!"+e.getMessage();
+            assert false : "throw exception means error occured!" + e.getMessage();
         }
 
     }
 
     @Test
-    public void testGetAllMicroServicesInstances(){
+    public void testGetAllMicroServicesInstances() {
         MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo();
         microServiceFullInfo.setServiceName("testService");
         microServiceFullInfo.setVersion("v1");
@@ -286,7 +311,7 @@ public class MicroServiceFullServiceTest {
         microServiceFullInfo.setProtocol("http");
         microServiceFullInfo.setEnable_ssl(false);
         Set<Node> nodeSet = new HashSet<>();
-        nodeSet.add(new Node("10.74.148.88","8080"));
+        nodeSet.add(new Node("10.74.148.88", "8080"));
         microServiceFullInfo.setNodes(nodeSet);
 
         MicroServiceFullInfo microServiceFullInfo2 = new MicroServiceFullInfo();
@@ -298,7 +323,7 @@ public class MicroServiceFullServiceTest {
         microServiceFullInfo2.setProtocol("http");
         microServiceFullInfo2.setEnable_ssl(true);
         Set<Node> nodeSet2 = new HashSet<>();
-        nodeSet2.add(new Node("10.74.148.89","8080"));
+        nodeSet2.add(new Node("10.74.148.89", "8080"));
         microServiceFullInfo2.setNodes(nodeSet2);
 
         try {
@@ -310,17 +335,17 @@ public class MicroServiceFullServiceTest {
             expected.add(microServiceFullInfo2);
             List<MicroServiceFullInfo> result = microServiceFullService.getAllMicroServiceInstances();
             Collections.sort(expected, serviceComparator);
-            Collections.sort(result,serviceComparator );
+            Collections.sort(result, serviceComparator);
             assertEquals(expected, result);
         } catch (Exception e) {
             e.printStackTrace();
-            assert false:"throw exception means error occured!"+e.getMessage();
+            assert false : "throw exception means error occured!" + e.getMessage();
         }
     }
 
 
     @Test
-    public void testDeleteMultiMicroService(){
+    public void testDeleteMultiMicroService() {
         MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo();
         microServiceFullInfo.setServiceName("testService");
         microServiceFullInfo.setVersion("v1");
@@ -330,7 +355,7 @@ public class MicroServiceFullServiceTest {
         microServiceFullInfo.setProtocol("http");
         microServiceFullInfo.setEnable_ssl(false);
         Set<Node> nodeSet = new HashSet<>();
-        nodeSet.add(new Node("10.74.148.88","8080"));
+        nodeSet.add(new Node("10.74.148.88", "8080"));
         microServiceFullInfo.setNodes(nodeSet);
 
 
@@ -343,34 +368,36 @@ public class MicroServiceFullServiceTest {
         microServiceFullInfo3.setProtocol("http");
         microServiceFullInfo3.setEnable_ssl(false);
         Set<Node> nodeSet3 = new HashSet<>();
-        nodeSet3.add(new Node("10.74.148.89","8080"));
+        nodeSet3.add(new Node("10.74.148.89", "8080"));
         microServiceFullInfo3.setNodes(nodeSet3);
 
         try {
             microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo);
             microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo3);
-            //two versions of testservice exist
-            assertEquals(2,microServiceFullService.getAllVersionsOfTheService("testService").size());
-            //delete all versions of testservice
-            long size = microServiceFullService.deleteMultiMicroService(MicroServiceUtil.getPrefixedKey("testService","*"));
-            //after delete,no version exist
-            assertEquals(0,microServiceFullService.getAllVersionsOfTheService("testService").size());
+            // two versions of testservice exist
+            assertEquals(2, microServiceFullService.getAllVersionsOfTheService("testService").size());
+            // delete all versions of testservice
+            long size = microServiceFullService
+                            .deleteMultiMicroService(MicroServiceUtil.getPrefixedKey("testService", "*"));
+            // after delete,no version exist
+            assertEquals(0, microServiceFullService.getAllVersionsOfTheService("testService").size());
         } catch (Exception e) {
-            assert false:"throw exception means error occured!"+e.getMessage();
+            assert false : "throw exception means error occured!" + e.getMessage();
         }
     }
 
     @Test
-    public void tesGetMicroServiceInstance_notExist(){
+    public void tesGetMicroServiceInstance_notExist() {
         try {
-            assertNull(microServiceFullService.getMicroServiceInstance("notExist","v1"));
+            assertNull(microServiceFullService.getMicroServiceInstance("notExist", "v1"));
         } catch (Exception e) {
-            assert false:"throw exception means error occured!"+e.getMessage();
+            assert false : "throw exception means error occured!" + e.getMessage();
         }
 
     }
+
     @Test
-    public void tesExistsGetUpdateDeleteMicroServiceStatus_versionNull(){
+    public void tesExistsGetUpdateDeleteMicroServiceStatus_versionNull() {
         MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo();
         microServiceFullInfo.setServiceName("testService");
         microServiceFullInfo.setVersion("");
@@ -380,29 +407,29 @@ public class MicroServiceFullServiceTest {
         microServiceFullInfo.setProtocol("http");
         microServiceFullInfo.setEnable_ssl(false);
         Set<Node> nodeSet = new HashSet<>();
-        nodeSet.add(new Node("10.74.148.88","8080"));
+        nodeSet.add(new Node("10.74.148.88", "8080"));
         microServiceFullInfo.setNodes(nodeSet);
         try {
-            //test null
+            // test null
             assertFalse(microServiceFullService.existsMicroServiceInstance("testService", "null"));
             microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo);
-            assertEquals("1", microServiceFullService.getMicroServiceInstance("testService","null").getStatus());
+            assertEquals("1", microServiceFullService.getMicroServiceInstance("testService", "null").getStatus());
             microServiceFullService.updateMicroServiceStatus("testService", "null", "0");
             assertEquals("0", microServiceFullService.getMicroServiceInstance("testService", "null").getStatus());
-            microServiceFullService.deleteMicroService("testService","null");
+            microServiceFullService.deleteMicroService("testService", "null");
             assertFalse(microServiceFullService.existsMicroServiceInstance("testService", "null"));
 
 
-            //test String "null"
+            // test String "null"
             assertFalse(microServiceFullService.existsMicroServiceInstance("testService", null));
             microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo);
-            assertEquals("1", microServiceFullService.getMicroServiceInstance("testService",null).getStatus());
+            assertEquals("1", microServiceFullService.getMicroServiceInstance("testService", null).getStatus());
             microServiceFullService.updateMicroServiceStatus("testService", null, "0");
             assertEquals("0", microServiceFullService.getMicroServiceInstance("testService", null).getStatus());
-            microServiceFullService.deleteMicroService("testService",null);
+            microServiceFullService.deleteMicroService("testService", null);
             assertFalse(microServiceFullService.existsMicroServiceInstance("testService", null));
         } catch (Exception e) {
-            assert false:"throw exception means error occured!"+e.getMessage();
+            assert false : "throw exception means error occured!" + e.getMessage();
         }
     }