cf8bc30e34673f5f3753f779d0017bd15d0ef845
[vfc/nfvo/driver/vnfm/gvnfm.git] / juju / juju-vnfmadapter / Juju-vnfmadapterService / service / src / test / java / org / openo / nfvo / jujuvnfmadapter / service / rest / fullstack / TestHelper.java
1 /*
2  * Copyright (c) 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.jujuvnfmadapter.service.rest.fullstack;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.InputStream;
24
25 import org.apache.commons.io.IOUtils;
26 import org.openo.nfvo.jujuvnfmadapter.common.FileUtils;
27 import org.springframework.mock.web.MockHttpServletRequest;
28
29 /**
30  * <br/>
31  * <p>
32  * </p>
33  * 
34  * @author quanzhong@huawei.com
35  * @version NFVO 0.5 Nov 3, 2016
36  */
37 public class TestHelper {
38
39     @SuppressWarnings("deprecation")
40     public static MockHttpServletRequest buildMockRequest(File file) throws FileNotFoundException, IOException {
41
42         String contnet = IOUtils.toString(new FileInputStream(file));
43         byte[] content = contnet.getBytes();
44         MockHttpServletRequest context = new MockHttpServletRequest();
45         context.setContentType("application/json");
46         context.setContent(content);
47         return context;
48     }
49
50     public static MockHttpServletRequest buildMockRequest(String content) throws FileNotFoundException, IOException {
51         MockHttpServletRequest context = new MockHttpServletRequest();
52         context.setContentType("application/json");
53         context.setContent(content.getBytes());
54         return context;
55     }
56
57     public static MockHttpServletRequest buildDefaultRequest(String fileName)
58             throws FileNotFoundException, IOException {
59         MockHttpServletRequest context = new MockHttpServletRequest();
60         context.setContentType("application/json");
61         if(fileName != null) {
62             String file = FileUtils.getClassPath() + File.separator + fileName;
63             InputStream input = new FileInputStream(file);
64             @SuppressWarnings("deprecation")
65             String contnet = IOUtils.toString(input);
66             byte[] content = contnet.getBytes();
67             context.setContent(content);
68         }
69         return context;
70     }
71
72 }