diff options
author | Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com> | 2018-09-21 10:42:45 +0530 |
---|---|---|
committer | Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com> | 2018-09-21 11:18:46 +0530 |
commit | d51c6871dc5cad267fa4acf8e0e9a70717a6c106 (patch) | |
tree | e8264bf7aeb749b8fa503790e2978da7d5074bc1 /vnfmarket-be | |
parent | 7c8dcee452164e07feff118325c244e2a10b6213 (diff) |
VTP: Make VTP separate REST controller
Issue-ID: VNFSDK-304
Change-Id: I7fbe7d4dbb7a97b594fd893734408ecca0c1765c
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Diffstat (limited to 'vnfmarket-be')
4 files changed, 223 insertions, 141 deletions
diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/resource/PackageResource.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/resource/PackageResource.java index be80937b..22609081 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/resource/PackageResource.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/resource/PackageResource.java @@ -18,9 +18,6 @@ package org.onap.vnfsdk.marketplace.resource; import java.io.IOException; import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; -import java.util.Map.Entry; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.Consumes; @@ -36,12 +33,10 @@ import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import org.apache.commons.io.IOUtils; import org.eclipse.jetty.http.HttpStatus; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; import org.glassfish.jersey.media.multipart.FormDataParam; import org.onap.vnfsdk.marketplace.common.CommonConstant; -import org.onap.vnfsdk.marketplace.common.ToolUtil; import org.onap.vnfsdk.marketplace.db.exception.MarketplaceResourceException; import org.onap.vnfsdk.marketplace.db.resource.PackageManager; import org.onap.vnfsdk.marketplace.entity.response.CsarFileUriResponse; @@ -52,10 +47,6 @@ import org.onap.vnfsdk.marketplace.rest.RestConstant; import org.onap.vnfsdk.marketplace.rest.RestResponse; import org.onap.vnfsdk.marketplace.rest.RestfulClient; import org.onap.vnfsdk.marketplace.wrapper.PackageWrapper; -import org.open.infc.grpc.Result; -import org.open.infc.grpc.client.OpenRemoteCli; - -import com.google.gson.internal.LinkedTreeMap; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -133,64 +124,6 @@ public class PackageResource { return PackageWrapper.getInstance().uploadPackage(uploadedInputStream, fileDetail, details, head); } - @Path("/vtp/tests") - @GET - @ApiOperation(value = "VTP Test cases", response = String.class) - @Produces(MediaType.APPLICATION_JSON) - @ApiResponses(value = { - @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Failed to retrieve the tests", response = String.class) }) - public Response listTests() throws IOException, MarketplaceResourceException { - Result result = null; - try { - result = OpenRemoteCli.run(new String[] { "-P", "open-cli", "schema-list", "--product", "onap-vtp", "--format", "json" }); - } catch (Exception e) { - return Response.serverError().build(); - } - - if (result.getExitCode() != 0) { - return Response.serverError().entity(result.getOutput()).build(); - } - - return Response.ok(result.getOutput(), MediaType.APPLICATION_JSON).build(); - } - - @Path("/vtp/tests/{testName}/run") - @POST - @ApiOperation(value = "Run VTP testcase") - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - @ApiResponses(value = { - @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "Test case not found", response = String.class), - @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "VTP internal failure", response = String.class) }) - public Response runTest(@ApiParam(value = "test Name") @PathParam("testName") String testName, - @Context HttpServletRequest request) - throws IOException, MarketplaceResourceException { - String details = IOUtils.toString(request.getInputStream()); - Result result = null; - try { - List<String> cmdArgsList = new ArrayList<>(); - for (String defaultArg: new String[] { "-P", "onap-vtp", testName, "--format", "json" }) { - cmdArgsList.add(defaultArg); - } - - LinkedTreeMap<String, String> cmdArgs = ToolUtil.fromJson(details, LinkedTreeMap.class); - for (Entry<String, String> arg : cmdArgs.entrySet()) { - cmdArgsList.add("--" + arg.getKey()); - cmdArgsList.add(arg.getValue()); - } - - result = OpenRemoteCli.run(cmdArgsList.toArray(new String []{})); - } catch (Exception e) { - return Response.serverError().build(); - } - - if (result.getExitCode() != 0) { - return Response.serverError().entity(result.getOutput()).build(); - } - - return Response.ok(result.getOutput(), MediaType.APPLICATION_JSON).build(); - } - @Path("/csars/{csarId}") @DELETE @ApiOperation(value = "delete a package") diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/resource/VTPResource.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/resource/VTPResource.java new file mode 100644 index 00000000..6c8b127f --- /dev/null +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/resource/VTPResource.java @@ -0,0 +1,111 @@ +/** + * Copyright 2018 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.vnfsdk.marketplace.resource; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map.Entry; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.apache.commons.io.IOUtils; +import org.eclipse.jetty.http.HttpStatus; +import org.onap.vnfsdk.marketplace.common.ToolUtil; +import org.onap.vnfsdk.marketplace.db.exception.MarketplaceResourceException; +import org.open.infc.grpc.Result; +import org.open.infc.grpc.client.OpenRemoteCli; + +import com.google.gson.internal.LinkedTreeMap; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; + + +@Path("/vtp") +@Api(tags = { "VNF Test Platform" }) +public class VTPResource { + @Path("/tests") + @GET + @ApiOperation(value = "VTP Test cases", response = String.class) + @Produces(MediaType.APPLICATION_JSON) + @ApiResponses(value = { + @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Failed to retrieve the tests", response = String.class) }) + public Response listTests() throws IOException, MarketplaceResourceException { + Result result = null; + try { + result = OpenRemoteCli.run(new String[] { "-P", "open-cli", "schema-list", "--product", "onap-vtp", "--format", "json" }); + } catch (Exception e) { + return Response.serverError().build(); + } + + if (result.getExitCode() != 0) { + return Response.serverError().entity(result.getOutput()).build(); + } + + return Response.ok(result.getOutput(), MediaType.APPLICATION_JSON).build(); + } + + @Path("/tests/{testName}/run") + @POST + @ApiOperation(value = "Run VTP testcase") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + @ApiResponses(value = { + @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "Test case not found", response = String.class), + @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "VTP internal failure", response = String.class) }) + public Response runTest(@ApiParam(value = "test Name") @PathParam("testName") String testName, + @Context HttpServletRequest request) + throws IOException, MarketplaceResourceException { + String details = IOUtils.toString(request.getInputStream()); + Result result = null; + try { + List<String> cmdArgsList = new ArrayList<>(); + for (String defaultArg: new String[] { "-P", "onap-vtp", testName, "--format", "json" }) { + cmdArgsList.add(defaultArg); + } + + LinkedTreeMap<String, String> cmdArgs = ToolUtil.fromJson(details, LinkedTreeMap.class); + for (Entry<String, String> arg : cmdArgs.entrySet()) { + cmdArgsList.add("--" + arg.getKey()); + cmdArgsList.add(arg.getValue()); + } + + result = OpenRemoteCli.run(cmdArgsList.toArray(new String []{})); + } catch (Exception e) { + return Response.serverError().build(); + } + + if (result.getExitCode() != 0) { + return Response.serverError().entity(result.getOutput()).build(); + } + + return Response.ok(result.getOutput(), MediaType.APPLICATION_JSON).build(); + } +} diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vnfsdk/marketplace/resource/PackageResourceTest.java b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vnfsdk/marketplace/resource/PackageResourceTest.java index 0728e3ac..6ec0406a 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vnfsdk/marketplace/resource/PackageResourceTest.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vnfsdk/marketplace/resource/PackageResourceTest.java @@ -21,15 +21,12 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import java.io.BufferedInputStream; -import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; -import java.io.IOException; import java.io.InputStream; -import java.nio.charset.StandardCharsets; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; @@ -37,9 +34,6 @@ import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; -import javax.servlet.ReadListener; -import javax.servlet.ServletInputStream; -import javax.servlet.http.HttpServletRequest; import javax.ws.rs.core.Response; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; @@ -787,74 +781,6 @@ public class PackageResourceTest { } @Test - public void testVtpGetTests() throws Exception { - new MockUp<OpenRemoteCli>() { - - @Mock - public Result run(String[] args) { - Result result = Result.newBuilder(). - setExitCode(0). - setOutput("{}"). - build(); - - return result; - } - }; - - Response result = packageResource.listTests(); - assertEquals(200, result.getStatus()); - } - - @Test - public void testVtpRunTests() throws Exception { - new MockUp<OpenRemoteCli>() { - - @Mock - public Result run(String[] args) { - Result result = Result.newBuilder(). - setExitCode(0). - setOutput("{}"). - build(); - - return result; - } - }; - - MockUp mockReq = new MockUp<HttpServletRequest>() { - - @Mock - public ServletInputStream getInputStream() throws IOException { - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( - "{\"csar\"=\"VoLTE.csar\"}".getBytes()); - - return new ServletInputStream(){ - public int read() throws IOException { - return byteArrayInputStream.read(); - } - - @Override - public boolean isFinished() { - return true; - } - - @Override - public boolean isReady() { - return true; - } - - @Override - public void setReadListener(ReadListener arg0) { - } - }; - } - - }; - - Response result = packageResource.runTest("csar-validate", (HttpServletRequest) mockReq.getMockInstance()); - assertEquals(200, result.getStatus()); - } - - @Test public void testGetOnBoardingStepsSuccess() { new MockUp<org.onap.vnfsdk.marketplace.filemanage.http.ToolUtil>() { diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vnfsdk/marketplace/resource/VTPResourceTest.java b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vnfsdk/marketplace/resource/VTPResourceTest.java new file mode 100644 index 00000000..4dfd5811 --- /dev/null +++ b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vnfsdk/marketplace/resource/VTPResourceTest.java @@ -0,0 +1,112 @@ +/** + * Copyright 2018 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.vnfsdk.marketplace.resource; + +import static org.junit.Assert.assertEquals; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import javax.servlet.ReadListener; +import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.Response; + +import org.junit.Before; +import org.junit.Test; +import org.open.infc.grpc.Result; +import org.open.infc.grpc.client.OpenRemoteCli; + +import mockit.Mock; +import mockit.MockUp; + +public class VTPResourceTest { + private VTPResource vtpResource = null; + + + @Before + public void setUp() { + vtpResource = new VTPResource(); + } + @Test + public void testVtpGetTests() throws Exception { + new MockUp<OpenRemoteCli>() { + + @Mock + public Result run(String[] args) { + Result result = Result.newBuilder(). + setExitCode(0). + setOutput("{}"). + build(); + + return result; + } + }; + + Response result = vtpResource.listTests(); + assertEquals(200, result.getStatus()); + } + + @Test + public void testVtpRunTests() throws Exception { + new MockUp<OpenRemoteCli>() { + + @Mock + public Result run(String[] args) { + Result result = Result.newBuilder(). + setExitCode(0). + setOutput("{}"). + build(); + + return result; + } + }; + + MockUp mockReq = new MockUp<HttpServletRequest>() { + + @Mock + public ServletInputStream getInputStream() throws IOException { + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( + "{\"csar\"=\"VoLTE.csar\"}".getBytes()); + + return new ServletInputStream(){ + public int read() throws IOException { + return byteArrayInputStream.read(); + } + + @Override + public boolean isFinished() { + return true; + } + + @Override + public boolean isReady() { + return true; + } + + @Override + public void setReadListener(ReadListener arg0) { + } + }; + } + + }; + + Response result = vtpResource.runTest("csar-validate", (HttpServletRequest) mockReq.getMockInstance()); + assertEquals(200, result.getStatus()); + } +} |