3b0596087f1944bf1414aaf3dd09fa1b0d37768f
[msb/apigateway.git] / msb-core / apiroute / apiroute-service / src / test / java / org / openo / msb / wrapper / MicroServiceWrapperTest.java
1 /**\r
2  * Copyright 2016 ZTE, Inc. and others.\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 package org.openo.msb.wrapper;\r
17 \r
18 import java.util.HashSet;\r
19 import java.util.Set;\r
20 \r
21 import org.junit.After;\r
22 import org.junit.Assert;\r
23 import org.junit.Before;\r
24 import org.junit.BeforeClass;\r
25 import org.junit.Test;\r
26 import org.openo.msb.api.MicroServiceFullInfo;\r
27 import org.openo.msb.api.MicroServiceInfo;\r
28 import org.openo.msb.api.Node;\r
29 import org.openo.msb.api.exception.ExtendedInternalServerErrorException;\r
30 import org.openo.msb.api.exception.ExtendedNotFoundException;\r
31 \r
32 public class MicroServiceWrapperTest {\r
33  private static MicroServiceWrapper microServiceWrapper;\r
34     \r
35     @BeforeClass\r
36     public static void setUpBeforeClass() throws Exception {\r
37         microServiceWrapper=microServiceWrapper.getInstance();\r
38     }\r
39     \r
40     @Before\r
41     public void setUp() {\r
42         MicroServiceInfo microServiceInfo=new MicroServiceInfo();\r
43         microServiceInfo.setServiceName("testForJunit");\r
44         microServiceInfo.setVersion("v1");\r
45         microServiceInfo.setUrl("/api/test/v1");\r
46         microServiceInfo.setProtocol("REST");\r
47         microServiceInfo.setVisualRange("0");\r
48         Set<Node> nodes=new HashSet<Node>();\r
49         nodes.add(new Node("127.0.0.1","8080",0));\r
50         microServiceInfo.setNodes(nodes);\r
51         try {\r
52             microServiceWrapper.saveMicroServiceInstance(microServiceInfo,true,"","");\r
53         }catch(ExtendedInternalServerErrorException e){\r
54         }\r
55         catch(Exception e){\r
56             Assert.fail("Exception" + e.getMessage());\r
57         }\r
58         \r
59     }\r
60     \r
61     @After\r
62     public void tearDown() {        \r
63         try {\r
64             microServiceWrapper.deleteMicroService("testForJunit", "v1", "");      \r
65         }catch(ExtendedInternalServerErrorException e){\r
66         } catch (Exception e) {\r
67             Assert.fail("Exception" + e.getMessage());\r
68         }\r
69     }\r
70     \r
71     @Test\r
72     public void testGetAllMicroServiceInstances(){\r
73         try {\r
74          MicroServiceFullInfo[] MicroServiceInfoList=microServiceWrapper.getAllMicroServiceInstances();\r
75         Assert.assertTrue(MicroServiceInfoList.length >= 0);\r
76         }catch(ExtendedInternalServerErrorException e){\r
77         }\r
78         catch(Exception e){\r
79             Assert.fail("Exception" + e.getMessage());\r
80         }\r
81        \r
82     }\r
83     \r
84     @Test\r
85     public void testGetMicroServiceInstance_not_exist(){\r
86         MicroServiceFullInfo microServiceInfo = null;\r
87         try {\r
88            microServiceInfo=microServiceWrapper.getMicroServiceInstance("testForJunit", "v2","");\r
89            Assert.assertNotNull(microServiceInfo);\r
90         }catch(ExtendedInternalServerErrorException e){\r
91         }\r
92         catch(ExtendedNotFoundException e){\r
93             Assert.assertNull(microServiceInfo);\r
94         }\r
95        \r
96     }\r
97     \r
98     @Test\r
99     public void testGetMicroServiceInstance_exist(){\r
100         try {\r
101         MicroServiceFullInfo microServiceInfo=microServiceWrapper.getMicroServiceInstance("testForJunit", "v1","");\r
102         Assert.assertNotNull(microServiceInfo);\r
103         }catch(ExtendedInternalServerErrorException e){\r
104         }\r
105         catch(Exception e){\r
106             Assert.fail("Exception" + e.getMessage());\r
107         }\r
108        \r
109     }\r
110     \r
111     @Test\r
112     public void testUpdateMicroServiceInstance(){\r
113         try {\r
114             \r
115             MicroServiceInfo microServiceInfo=new MicroServiceInfo();\r
116             microServiceInfo.setServiceName("testForJunit");\r
117             microServiceInfo.setVersion("v1");\r
118             microServiceInfo.setUrl("/api/test2/v1");\r
119             microServiceInfo.setProtocol("REST");\r
120             microServiceInfo.setVisualRange("0");\r
121             Set<Node> nodes=new HashSet<Node>();\r
122             nodes.add(new Node("127.0.0.1","8080",0));\r
123             microServiceInfo.setNodes(nodes);\r
124             \r
125             MicroServiceFullInfo new_MicroServiceInfo= microServiceWrapper.updateMicroServiceInstance("testForJunit", "v1", microServiceInfo);\r
126             Assert.assertEquals("/api/test2/v1", new_MicroServiceInfo.getUrl());\r
127         }catch(ExtendedInternalServerErrorException e){\r
128         }\r
129         catch(Exception e){\r
130             Assert.fail("Exception" + e.getMessage());\r
131         \r
132         }\r
133     }\r
134     \r
135     @Test\r
136     public void testUpdateMicroServiceStatus(){\r
137         try { \r
138             MicroServiceFullInfo new_MicroServiceInfo= microServiceWrapper.updateMicroServiceStatus("testForJunit", "v1", "0");\r
139             Assert.assertEquals("0",new_MicroServiceInfo.getStatus());\r
140         }catch(ExtendedInternalServerErrorException e){\r
141         }catch(Exception e){\r
142             Assert.fail("Exception" + e.getMessage());\r
143         \r
144         }\r
145     }\r
146     \r
147    \r
148 }\r