2d2bed0852d360ad70173561a82b04323318901d
[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 testVtpGetTestsFailure1() throws Exception {
66         new MockUp<OpenRemoteCli>() {
67
68             @Mock
69             public Result run(String[] args) {
70                 Result result = Result.newBuilder().
71                         setExitCode(1).
72                         build();
73
74                 return result;
75             }
76         };
77
78         Response result = vtpResource.listTests();
79         assertEquals(500, result.getStatus());
80     }
81     
82     @Test
83     public void testVtpGetTestsFailure2() throws Exception {
84         new MockUp<OpenRemoteCli>() {
85
86             @Mock
87             public Result run(String[] args) throws Exception {
88                 throw new Exception();
89             }
90         };
91
92         Response result = vtpResource.listTests();
93         assertEquals(500, result.getStatus());
94     }
95     
96     @Test
97     public void testVtpRunTests() throws Exception {
98         new MockUp<OpenRemoteCli>() {
99
100             @Mock
101             public Result run(String[] args) {
102                 Result result = Result.newBuilder().
103                         setExitCode(0).
104                         setOutput("{}").
105                         build();
106
107                 return result;
108             }
109         };
110
111         MockUp mockReq = new MockUp<HttpServletRequest>() {
112
113             @Mock
114             public ServletInputStream getInputStream() throws IOException {
115                   ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
116                           "{\"csar\"=\"VoLTE.csar\"}".getBytes());
117
118                   return new ServletInputStream(){
119                     public int read() throws IOException {
120                       return byteArrayInputStream.read();
121                     }
122
123                     @Override
124                     public boolean isFinished() {
125                         return true;
126                     }
127
128                     @Override
129                     public boolean isReady() {
130                         return true;
131                     }
132
133                     @Override
134                     public void setReadListener(ReadListener arg0) {
135                     }
136                   };
137                 }
138
139         };
140
141         Response result = vtpResource.runTest("csar-validate", (HttpServletRequest) mockReq.getMockInstance());
142         assertEquals(200, result.getStatus());
143     }
144     
145     @Test
146     public void testVtpRunTestsFailure1() throws Exception {
147         new MockUp<OpenRemoteCli>() {
148
149             @Mock
150             public Result run(String[] args) {
151                 Result result = Result.newBuilder().
152                         setExitCode(1).
153                         build();
154
155                 return result;
156             }
157         };
158
159         MockUp mockReq = new MockUp<HttpServletRequest>() {
160
161             @Mock
162             public ServletInputStream getInputStream() throws IOException {
163                   ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
164                           "{\"csar\"=\"VoLTE.csar\"}".getBytes());
165
166                   return new ServletInputStream(){
167                     public int read() throws IOException {
168                       return byteArrayInputStream.read();
169                     }
170
171                     @Override
172                     public boolean isFinished() {
173                         return true;
174                     }
175
176                     @Override
177                     public boolean isReady() {
178                         return true;
179                     }
180
181                     @Override
182                     public void setReadListener(ReadListener arg0) {
183                     }
184                   };
185                 }
186
187         };
188
189         Response result = vtpResource.runTest("csar-validate", (HttpServletRequest) mockReq.getMockInstance());
190         assertEquals(500, result.getStatus());
191     }
192     
193     @Test
194     public void testVtpRunTestsFailure2() throws Exception {
195         new MockUp<OpenRemoteCli>() {
196
197             @Mock
198             public Result run(String[] args) throws Exception {
199                 throw new Exception();
200             }
201         };
202
203         MockUp mockReq = new MockUp<HttpServletRequest>() {
204
205             @Mock
206             public ServletInputStream getInputStream() throws IOException {
207                   ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
208                           "{\"csar\"=\"VoLTE.csar\"}".getBytes());
209
210                   return new ServletInputStream(){
211                     public int read() throws IOException {
212                       return byteArrayInputStream.read();
213                     }
214
215                     @Override
216                     public boolean isFinished() {
217                         return true;
218                     }
219
220                     @Override
221                     public boolean isReady() {
222                         return true;
223                     }
224
225                     @Override
226                     public void setReadListener(ReadListener arg0) {
227                     }
228                   };
229                 }
230
231         };
232
233         Response result = vtpResource.runTest("csar-validate", (HttpServletRequest) mockReq.getMockInstance());
234         assertEquals(500, result.getStatus());
235     }
236 }