19fba9624ea631d4b5a9b592112dea47065a02a1
[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 testVtpRunTests() throws Exception {
84         new MockUp<OpenRemoteCli>() {
85
86             @Mock
87             public Result run(String[] args) {
88                 Result result = Result.newBuilder().
89                         setExitCode(0).
90                         setOutput("{}").
91                         build();
92
93                 return result;
94             }
95         };
96
97         MockUp mockReq = new MockUp<HttpServletRequest>() {
98
99             @Mock
100             public ServletInputStream getInputStream() throws IOException {
101                   ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
102                           "{\"csar\"=\"VoLTE.csar\"}".getBytes());
103
104                   return new ServletInputStream(){
105                     public int read() throws IOException {
106                       return byteArrayInputStream.read();
107                     }
108
109                     @Override
110                     public boolean isFinished() {
111                         return true;
112                     }
113
114                     @Override
115                     public boolean isReady() {
116                         return true;
117                     }
118
119                     @Override
120                     public void setReadListener(ReadListener arg0) {
121                     }
122                   };
123                 }
124
125         };
126
127         Response result = vtpResource.runTest("csar-validate", (HttpServletRequest) mockReq.getMockInstance());
128         assertEquals(200, result.getStatus());
129     }
130     
131     @Test
132     public void testVtpRunTestsFailure1() throws Exception {
133         new MockUp<OpenRemoteCli>() {
134
135             @Mock
136             public Result run(String[] args) {
137                 Result result = Result.newBuilder().
138                         setExitCode(1).
139                         build();
140
141                 return result;
142             }
143         };
144
145         MockUp mockReq = new MockUp<HttpServletRequest>() {
146
147             @Mock
148             public ServletInputStream getInputStream() throws IOException {
149                   ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
150                           "{\"csar\"=\"VoLTE.csar\"}".getBytes());
151
152                   return new ServletInputStream(){
153                     public int read() throws IOException {
154                       return byteArrayInputStream.read();
155                     }
156
157                     @Override
158                     public boolean isFinished() {
159                         return true;
160                     }
161
162                     @Override
163                     public boolean isReady() {
164                         return true;
165                     }
166
167                     @Override
168                     public void setReadListener(ReadListener arg0) {
169                     }
170                   };
171                 }
172
173         };
174
175         Response result = vtpResource.runTest("csar-validate", (HttpServletRequest) mockReq.getMockInstance());
176         assertEquals(500, result.getStatus());
177     }
178 }