Change: add OPEN-O seed code for VF-C
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / integration-test / java / org / openo / nfvo / vnfmadapter / util / MyTestManager.java
1 /*
2  * Copyright 2016 Huawei Technologies Co., Ltd.
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
17 package org.openo.nfvo.vnfmadapter.util;
18
19 import java.io.File;
20 import java.util.Iterator;
21 import java.util.Map;
22
23 import org.junit.Assert;
24 import org.openo.baseservice.remoteservice.exception.ServiceException;
25 import org.openo.baseservice.roa.util.restclient.RestfulParametes;
26 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
27 import org.openo.sdno.testframework.checker.DefaultChecker;
28 import org.openo.sdno.testframework.http.model.HttpModelUtils;
29 import org.openo.sdno.testframework.http.model.HttpRequest;
30 import org.openo.sdno.testframework.http.model.HttpResponse;
31 import org.openo.sdno.testframework.http.model.HttpRquestResponse;
32 import org.openo.sdno.testframework.restclient.HttpRestClient;
33 import org.openo.sdno.testframework.testmanager.TestManager;
34 import org.openo.sdno.testframework.util.file.FileUtils;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * <br>
40  * <p>
41  * </p>
42  * 
43  * @author
44  * @version NFVO 0.5 Sep 21, 2016
45  */
46 public class MyTestManager extends TestManager {
47
48     private HttpRestClient restClient;
49
50     public MyTestManager() {
51         restClient = new HttpRestClient();
52     }
53
54     private static final Logger LOGGER = LoggerFactory.getLogger(MyTestManager.class);
55
56     /**
57      * <br>
58      * 
59      * @param file
60      * @return
61      * @throws ServiceException
62      * @since NFVO 0.5
63      */
64     @Override
65     public HttpResponse execTestCase(File file) throws ServiceException {
66         String content = FileUtils.readFromJson(file);
67         HttpRquestResponse httpObject = HttpModelUtils.praseHttpRquestResponse(content);
68         return send(httpObject.getRequest(), httpObject.getResponse());
69     }
70
71     private HttpResponse send(HttpRequest request, HttpResponse response) {
72         try {
73             RestfulResponse responseResult = doSend(request);
74             DefaultChecker checker = new MyChecker(response);
75             HttpResponse httpResponse = HttpModelUtils.convertResponse(responseResult);
76             Assert.assertEquals(Boolean.valueOf(checker.check(httpResponse)), Boolean.valueOf(true));
77             return httpResponse;
78         } catch(ServiceException e) {
79             LOGGER.error("call the restful interface failed.", e);
80         }
81         return null;
82     }
83
84     private RestfulResponse doSend(HttpRequest request) throws ServiceException {
85         String url = request.getUri();
86         String method = request.getMethod();
87         String body = request.getData();
88         RestfulParametes restfulParametes = new RestfulParametes();
89         Map requestHeaders = request.getHeaders();
90         if(null != requestHeaders) {
91             java.util.Map.Entry curEntity;
92             for(Iterator iterator = requestHeaders.entrySet().iterator(); iterator.hasNext(); restfulParametes
93                     .putHttpContextHeader((String)curEntity.getKey(), (String)curEntity.getValue()))
94                 curEntity = (java.util.Map.Entry)iterator.next();
95
96         }
97         Map paramMap = request.getQueries();
98         if(null != paramMap)
99             restfulParametes.setParamMap(paramMap);
100         if(null != body)
101             restfulParametes.setRawData(body);
102         return callRestfulMotheds(url, method, restfulParametes);
103     }
104
105     private RestfulResponse callRestfulMotheds(String url, String method, RestfulParametes restfulParametes)
106             throws ServiceException {
107         String s = method;
108         byte byte0 = -1;
109         switch(s.hashCode()) {
110             case 3446944:
111                 if(s.equals("post"))
112                     byte0 = 0;
113                 break;
114
115             case 102230:
116                 if(s.equals("get"))
117                     byte0 = 1;
118                 break;
119
120             case 111375:
121                 if(s.equals("put"))
122                     byte0 = 2;
123                 break;
124
125             case -1335458389:
126                 if(s.equals("delete"))
127                     byte0 = 3;
128                 break;
129
130             case 3198432:
131                 if(s.equals("head"))
132                     byte0 = 4;
133                 break;
134
135             case 106438728:
136                 if(s.equals("patch"))
137                     byte0 = 5;
138                 break;
139         }
140         switch(byte0) {
141             case 0: // '\0'
142                 return restClient.post(url, restfulParametes);
143
144             case 1: // '\001'
145                 return restClient.get(url, restfulParametes);
146
147             case 2: // '\002'
148                 return restClient.put(url, restfulParametes);
149
150             case 3: // '\003'
151                 return restClient.delete(url, restfulParametes);
152
153             case 4: // '\004'
154                 return restClient.head(url, restfulParametes);
155
156             case 5: // '\005'
157                 return restClient.patch(url, restfulParametes);
158         }
159         LOGGER.error("The method is unsupported.");
160         throw new ServiceException("The method is unsupported.");
161     }
162
163 }