4dfd58110500ad23809b2f34fc1df5c33e193041
[vnfsdk/refrepo.git] /
1 /**
2  * Copyright 2018 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.onap.vnfsdk.marketplace.resource;
18
19 import static org.junit.Assert.assertEquals;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.IOException;
23
24 import javax.servlet.ReadListener;
25 import javax.servlet.ServletInputStream;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.ws.rs.core.Response;
28
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.open.infc.grpc.Result;
32 import org.open.infc.grpc.client.OpenRemoteCli;
33
34 import mockit.Mock;
35 import mockit.MockUp;
36
37 public class VTPResourceTest {
38     private VTPResource vtpResource = null;
39
40
41     @Before
42     public void setUp() {
43         vtpResource = new VTPResource();
44     }
45     @Test
46     public void testVtpGetTests() throws Exception {
47         new MockUp<OpenRemoteCli>() {
48
49             @Mock
50             public Result run(String[] args) {
51                 Result result = Result.newBuilder().
52                         setExitCode(0).
53                         setOutput("{}").
54                         build();
55
56                 return result;
57             }
58         };
59
60         Response result = vtpResource.listTests();
61         assertEquals(200, result.getStatus());
62     }
63
64     @Test
65     public void testVtpRunTests() throws Exception {
66         new MockUp<OpenRemoteCli>() {
67
68             @Mock
69             public Result run(String[] args) {
70                 Result result = Result.newBuilder().
71                         setExitCode(0).
72                         setOutput("{}").
73                         build();
74
75                 return result;
76             }
77         };
78
79         MockUp mockReq = new MockUp<HttpServletRequest>() {
80
81             @Mock
82             public ServletInputStream getInputStream() throws IOException {
83                   ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
84                           "{\"csar\"=\"VoLTE.csar\"}".getBytes());
85
86                   return new ServletInputStream(){
87                     public int read() throws IOException {
88                       return byteArrayInputStream.read();
89                     }
90
91                     @Override
92                     public boolean isFinished() {
93                         return true;
94                     }
95
96                     @Override
97                     public boolean isReady() {
98                         return true;
99                     }
100
101                     @Override
102                     public void setReadListener(ReadListener arg0) {
103                     }
104                   };
105                 }
106
107         };
108
109         Response result = vtpResource.runTest("csar-validate", (HttpServletRequest) mockReq.getMockInstance());
110         assertEquals(200, result.getStatus());
111     }
112 }