msb protocol synch change
[msb/apigateway.git] / msb-core / apiroute / apiroute-service / src / test / java / org / openo / msb / wrapper / MicroServiceWrapperTest.java
1 /**
2  * Copyright 2016 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openo.msb.wrapper;
17
18 import java.util.HashSet;
19 import java.util.Set;
20
21 import org.junit.After;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.openo.msb.api.MicroServiceFullInfo;
27 import org.openo.msb.api.MicroServiceInfo;
28 import org.openo.msb.api.Node;
29 import org.openo.msb.api.exception.ExtendedInternalServerErrorException;
30 import org.openo.msb.api.exception.ExtendedNotFoundException;
31
32 public class MicroServiceWrapperTest {
33  private static MicroServiceWrapper microServiceWrapper;
34     
35     @BeforeClass
36     public static void setUpBeforeClass() throws Exception {
37         microServiceWrapper=microServiceWrapper.getInstance();
38     }
39     
40     @Before
41     public void setUp() {
42         MicroServiceInfo microServiceInfo=new MicroServiceInfo();
43         microServiceInfo.setServiceName("testForJunit");
44         microServiceInfo.setVersion("v1");
45         microServiceInfo.setUrl("/api/test/v1");
46         microServiceInfo.setProtocol("REST");
47         microServiceInfo.setVisualRange("0");
48         Set<Node> nodes=new HashSet<Node>();
49         nodes.add(new Node("127.0.0.1","8080",0));
50         microServiceInfo.setNodes(nodes);
51         try {
52             microServiceWrapper.saveMicroServiceInstance(microServiceInfo,true,"","");
53         }catch(ExtendedInternalServerErrorException e){
54         }
55         catch(Exception e){
56             Assert.fail("Exception" + e.getMessage());
57         }
58         
59     }
60     
61     @After
62     public void tearDown() {        
63         try {
64             microServiceWrapper.deleteMicroService("testForJunit", "v1", "");      
65         }catch(ExtendedInternalServerErrorException e){
66         } catch (Exception e) {
67             Assert.fail("Exception" + e.getMessage());
68         }
69     }
70     
71     @Test
72     public void testGetAllMicroServiceInstances(){
73         try {
74          MicroServiceFullInfo[] MicroServiceInfoList=microServiceWrapper.getAllMicroServiceInstances();
75         Assert.assertTrue(MicroServiceInfoList.length >= 0);
76         }catch(ExtendedInternalServerErrorException e){
77         }
78         catch(Exception e){
79             Assert.fail("Exception" + e.getMessage());
80         }
81        
82     }
83     
84     @Test
85     public void testGetMicroServiceInstance_not_exist(){
86         MicroServiceFullInfo microServiceInfo = null;
87         try {
88            microServiceInfo=microServiceWrapper.getMicroServiceInstance("testForJunit", "v2","");
89            Assert.assertNotNull(microServiceInfo);
90         }catch(ExtendedInternalServerErrorException e){
91         }
92         catch(ExtendedNotFoundException e){
93             Assert.assertNull(microServiceInfo);
94         }
95        
96     }
97     
98     @Test
99     public void testGetMicroServiceInstance_exist(){
100         try {
101         MicroServiceFullInfo microServiceInfo=microServiceWrapper.getMicroServiceInstance("testForJunit", "v1","");
102         Assert.assertNotNull(microServiceInfo);
103         }catch(ExtendedInternalServerErrorException e){
104         }
105         catch(Exception e){
106             Assert.fail("Exception" + e.getMessage());
107         }
108        
109     }
110     
111     @Test
112     public void testUpdateMicroServiceInstance(){
113         try {
114             
115             MicroServiceInfo microServiceInfo=new MicroServiceInfo();
116             microServiceInfo.setServiceName("testForJunit");
117             microServiceInfo.setVersion("v1");
118             microServiceInfo.setUrl("/api/test2/v1");
119             microServiceInfo.setProtocol("REST");
120             microServiceInfo.setVisualRange("0");
121             Set<Node> nodes=new HashSet<Node>();
122             nodes.add(new Node("127.0.0.1","8080",0));
123             microServiceInfo.setNodes(nodes);
124             
125             MicroServiceFullInfo new_MicroServiceInfo= microServiceWrapper.updateMicroServiceInstance("testForJunit", "v1", microServiceInfo);
126             Assert.assertEquals("/api/test2/v1", new_MicroServiceInfo.getUrl());
127         }catch(ExtendedInternalServerErrorException e){
128         }
129         catch(Exception e){
130             Assert.fail("Exception" + e.getMessage());
131         
132         }
133     }
134     
135     @Test
136     public void testUpdateMicroServiceStatus(){
137         try { 
138             MicroServiceFullInfo new_MicroServiceInfo= microServiceWrapper.updateMicroServiceStatus("testForJunit", "v1", "0");
139             Assert.assertEquals("0",new_MicroServiceInfo.getStatus());
140         }catch(ExtendedInternalServerErrorException e){
141         }catch(Exception e){
142             Assert.fail("Exception" + e.getMessage());
143         
144         }
145     }
146     
147    
148 }