diff options
author | Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> | 2020-09-03 15:01:42 +0200 |
---|---|---|
committer | Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> | 2020-09-09 08:33:17 +0200 |
commit | 5fd768b457c0ec2eaf75963681fe7fcd1e69ae4e (patch) | |
tree | 1c07bf6566423b47967b258ff3ab9085eb32ed3d /vnfmarket-be/vnf-sdk-marketplace/src/test | |
parent | 0c15423accb26f152167fd89cf3dc9ad1b504d61 (diff) |
Add list of errors in responce for GET request.
Signed-off-by: Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com>
Issue-ID: VNFSDK-594
Change-Id: I61fd4b5e514ca4185174027ff08d8908a968e404
Diffstat (limited to 'vnfmarket-be/vnf-sdk-marketplace/src/test')
4 files changed, 343 insertions, 28 deletions
diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/VTPExecutionResourceTest.java b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/VTPExecutionResourceTest.java index c1a28303..1a3200ab 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/VTPExecutionResourceTest.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/VTPExecutionResourceTest.java @@ -1,5 +1,6 @@ /** * Copyright 2019 Huawei Technologies Co., Ltd. + * Copyright 2020 Nokia. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,37 +16,66 @@ */ package org.onap.vtp; +import com.google.common.collect.Lists; +import com.google.gson.Gson; +import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonParser; import org.glassfish.jersey.media.multipart.ContentDisposition; import org.glassfish.jersey.media.multipart.FormDataBodyPart; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; +import org.onap.vtp.error.VTPError; import org.onap.vtp.execution.VTPExecutionResource; import org.onap.vtp.execution.model.VTPTestExecution; -import org.open.infc.grpc.Result; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; @RunWith(MockitoJUnitRunner.class) public class VTPExecutionResourceTest { + static class VTPExecutionResourceForTests extends VTPExecutionResource { + public JsonElement expectedRpcResponse = null; + public List<String> expectedArguments = null; + + VTPExecutionResourceForTests() { + VTPExecutionResource.pathToExecutions = "src/test/resources/executions"; + } + + @Override + protected JsonElement makeRpcAndGetJson(List<String> args, int timeout) throws VTPError.VTPException { + if(expectedRpcResponse != null && expectedArguments != null) { + if (args.containsAll(expectedArguments)) { + return expectedRpcResponse; + } else { + return null; + } + } else { + return super.makeRpcAndGetJson( args, timeout); + } + } + } + @Mock FormDataBodyPart formDataBodyPart; @Mock ContentDisposition contentDisposition; String requestId; - VTPExecutionResource vtpExecutionResource; + VTPExecutionResourceForTests vtpExecutionResource; @Before - public void setUp() throws Exception { - vtpExecutionResource= new VTPExecutionResource(); + public void setUp() { + vtpExecutionResource= new VTPExecutionResourceForTests(); requestId = UUID.randomUUID().toString(); } @Test(expected = Exception.class) @@ -74,31 +104,221 @@ public class VTPExecutionResourceTest { System.out.println(executions.getExecutions()); assertNotNull(executions.getExecutions()); vtpExecutionResource.executeHandler(executions,null); - //vtpExecutionResource.executeHandler(executions,requestId); - //for handler } - @Test(expected = Exception.class) - public void testListTestExecutionsHandler() throws Exception - { - vtpExecutionResource.listTestExecutionsHandler(requestId,"VTP Scenario 1","testsuite-1","s1.ts1.testcase-1","open-cli-schema","2019-03-12T11:49:52.845","2020-03-12T11:49:52.845"); - } -// -// @Test -// public void testListTestExecutionsHandler1() throws Exception -// { VTPExecutionResource vtpExecutionResource1=mock(VTPExecutionResource.class); -// List<String> list= new ArrayList<>(); -// list.add("abc"); -// Result result= Result.getDefaultInstance(); -// -// when(vtpExecutionResource1.makeRpc(list)).thenReturn(result); -// verify(vtpExecutionResource1).makeRpc(list); -// -// } + + @Test + public void whenListTestExecutionsHandlerIsCalledWithProperParametersThenCorrectExecutionDataIsReturned() + throws IOException, VTPError.VTPException { + // given + String testStartTime = "2019-03-12T11:49:52.845"; + String testEndTime = "2020-03-12T11:49:52.845"; + String testProduct = "VTP Scenario 1"; + String testCommand = "s1.ts1.testcase-1"; + String testSuiteName = "testsuite-1"; + String testRequestId = "test-01-request-id"; + String testExecutionId = testRequestId + "-execution-id"; + String testProfile = "open-cli-schema"; + String expectedStatus = "SUCCESS"; + JsonElement expectedResult = new Gson().fromJson("" + + "[{" + + "\"test_1\": \"error01\"," + + "\"test_2\": \"error02\" " + + "}]", JsonArray.class); + + prepareMockRpcMethods( + testStartTime, testEndTime, testProduct, testCommand, testSuiteName, + testRequestId, testExecutionId, testProfile, expectedStatus + ); + + // when + VTPTestExecution.VTPTestExecutionList testExecutionResponse = + vtpExecutionResource.listTestExecutionsHandler( + testRequestId, testProduct, testSuiteName, testCommand, testProfile, testStartTime, testEndTime + ); + + // then + assertThatListOfExecutionsContainsOneCorrectExecutionResponse( + testExecutionResponse, + testStartTime, testEndTime, testProduct, testCommand, testSuiteName, + testRequestId, testExecutionId, testProfile, expectedStatus, expectedResult + ); + } + + @Test + public void whenListTestExecutionsHandlerIsCalledWithIdThatDoesNotMatchAnyExecutionFileThenResultContainsProperInformation() + throws IOException, VTPError.VTPException { + // given + String testStartTime = "2020-08-09T08:49:52.845"; + String testEndTime = "2020-08-10T08:49:55.845"; + String testProduct = "VTP Scenario 2"; + String testCommand = "s1.ts1.testcase-2"; + String testSuiteName = "testsuite-2"; + String testRequestId = "test-02-request-id"; + String testExecutionId = testRequestId + "-execution-id"; + String testProfile = "open-cli-schema"; + String expectedStatus = "FAIL"; + JsonElement expectedResult = new Gson().fromJson("" + + "[{ \"error\": \"unable to find execution results\"}]", JsonArray.class); + + prepareMockRpcMethods( + testStartTime, testEndTime, testProduct, testCommand, testSuiteName, + testRequestId, testExecutionId, testProfile, expectedStatus + ); + + // when + VTPTestExecution.VTPTestExecutionList testExecutionResponse = + vtpExecutionResource.listTestExecutionsHandler( + testRequestId, testProduct, testSuiteName, testCommand, testProfile, testStartTime, testEndTime + ); + + // then + assertThatListOfExecutionsContainsOneCorrectExecutionResponse( + testExecutionResponse, + testStartTime, testEndTime, testProduct, testCommand, testSuiteName, + testRequestId, testExecutionId, testProfile, expectedStatus, expectedResult + ); + } + + @Test + public void whenListTestExecutionsHandlerIsCalledWithIdThatMatchIncorrectExecutionFileThenResultContainsProperInformation() + throws IOException, VTPError.VTPException { + // given + String testStartTime = "2020-08-09T08:49:52.845"; + String testEndTime = "2020-08-10T08:49:55.845"; + String testProduct = "VTP Scenario 3"; + String testCommand = "s1.ts1.testcase-3"; + String testSuiteName = "testsuite-3"; + String testRequestId = "test-incorrect-request-id"; + String testExecutionId = testRequestId + "-execution-id"; + String testProfile = "open-cli-schema"; + String expectedStatus = "FAIL"; + JsonElement expectedResult = new Gson().fromJson("" + + "[{ " + + "\"error\": \"fail to load execution result\"," + + "\"reason\":\"Expected a com.google.gson.JsonArray but was com.google.gson.JsonPrimitive\"" + + "}]", + JsonArray.class + ); + + prepareMockRpcMethods( + testStartTime, testEndTime, testProduct, testCommand, testSuiteName, + testRequestId, testExecutionId, testProfile, expectedStatus + ); + + // when + VTPTestExecution.VTPTestExecutionList testExecutionResponse = + vtpExecutionResource.listTestExecutionsHandler( + testRequestId, testProduct, testSuiteName, testCommand, testProfile, testStartTime, testEndTime + ); + + // then + assertThatListOfExecutionsContainsOneCorrectExecutionResponse( + testExecutionResponse, + testStartTime, testEndTime, testProduct, testCommand, testSuiteName, + testRequestId, testExecutionId, testProfile, expectedStatus, expectedResult + ); + } + + @Rule + public ExpectedException exceptionRule = ExpectedException.none(); + + @Test + public void whenListTestExecutionsHandlerIsCalledAndGRpcClientIsUnReachableThenExceptionShouldBeThrown() + throws IOException, VTPError.VTPException { + // given + String testStartTime = "2020-08-10T08:50:20.845"; + String testEndTime = "2020-08-11T08:51:50.845"; + String testProduct = "VTP Scenario 3"; + String testCommand = "s1.ts1.testcase-3"; + String testSuiteName = "testsuite-3"; + String testRequestId = "test-03-request-id"; + String testProfile = "open-cli-schema"; + + // when + exceptionRule.expect(VTPError.VTPException.class); + exceptionRule.expectMessage("Timed out. Please use request-id to track the progress."); + vtpExecutionResource.listTestExecutionsHandler( + testRequestId, testProduct, testSuiteName, testCommand, testProfile, testStartTime, testEndTime + ); + + } + + private void assertThatListOfExecutionsContainsOneCorrectExecutionResponse( + VTPTestExecution.VTPTestExecutionList testExecutionResponse, + String testStartTime, String testEndTime, String testProduct, String testCommand, + String testSuiteName, String testRequestId, String testExecutionId, String testProfile, + String expectedStatus, JsonElement expectedResult ) { + + assertNotNull(testExecutionResponse); + assertEquals(1, testExecutionResponse.getExecutions().size()); + VTPTestExecution vtpTestExecution = testExecutionResponse.getExecutions().get(0); + assertFieldsInExecutionResponseAreCorrect( + vtpTestExecution, + testStartTime, testEndTime, testProduct, testCommand, testSuiteName, + testRequestId, testExecutionId, testProfile, expectedStatus, expectedResult + ); + } + + private void prepareMockRpcMethods(String testStartTime, String testEndTime, String testProduct, String testCommand, String testSuiteName, String testRequestId, String testExecutionId, String testProfile, String expectedStatus) { + vtpExecutionResource.expectedArguments = + buildListOfMockRpcArguments( + testStartTime, testEndTime, testProduct, testCommand, testRequestId, testSuiteName + ); + vtpExecutionResource.expectedRpcResponse = + buildExpectedRpcResponse( + testStartTime, testEndTime, testProduct, testCommand, testSuiteName, testRequestId, + testExecutionId, testProfile, expectedStatus + ); + } + + private JsonArray buildExpectedRpcResponse( + String testStartTime, String testEndTime, String testProduct, + String testCommand, String testSuiteName, String testRequestId, + String testExecutionId, String testProfile, String expectedStatus) { + return new Gson().fromJson("[{" + + "\"start-time\":\"" + testStartTime + "\";" + + "\"end-time\":\"" + testEndTime + "\";" + + "\"product\":\"" + testProduct + "\";" + + "\"command\":\"" + testCommand + "\";" + + "\"service\":\"" + testSuiteName + "\";" + + "\"request-id\":\"" + testRequestId + "\";" + + "\"execution-id\":\"" + testExecutionId + "\";" + + "\"profile\":\"" + testProfile + "\";" + + "\"status\":\"" + expectedStatus + "\"" + + "}]", JsonArray.class); + } + + private List<String> buildListOfMockRpcArguments( + String testStartTime, String testEndTime, String testProduct, + String testCommand, String testRequestId, String testSuiteName) { + return Lists.newArrayList( + "--product", "open-cli", "execution-list", "--format", "json", + "--start-time", testStartTime, "--end-time", testEndTime, + "--service", testSuiteName, "--product", testProduct, + "--command", testCommand, "--request-id", testRequestId); + } + + private void assertFieldsInExecutionResponseAreCorrect( + VTPTestExecution vtpTestExecution, + String testStartTime, String testEndTime, String testProduct, + String testCommand, String testSuiteName, String testRequestId, + String testExecutionId, String testProfile, String expectedStatus, JsonElement expectedResult) { + assertEquals(testStartTime, vtpTestExecution.getStartTime()); + assertEquals(testEndTime, vtpTestExecution.getEndTime()); + assertEquals(testProduct, vtpTestExecution.getScenario()); + assertEquals(testCommand, vtpTestExecution.getTestCaseName()); + assertEquals(testSuiteName, vtpTestExecution.getTestSuiteName()); + assertEquals(testExecutionId, vtpTestExecution.getExecutionId()); + assertEquals(testRequestId, vtpTestExecution.getRequestId()); + assertEquals(testProfile, vtpTestExecution.getProfile()); + assertEquals(expectedStatus, vtpTestExecution.getStatus()); + assertEquals(expectedResult, vtpTestExecution.getResults()); + } @Test(expected = Exception.class) public void testListTestExecutions() throws Exception { - vtpExecutionResource.listTestExecutions(requestId,"abc","abc","abc","abc","123","123"); + vtpExecutionResource.listTestExecutions(requestId, "abc", "abc", "abc", "abc", "123", "123"); } @Test(expected = Exception.class) public void testGetTestExecution() throws Exception @@ -130,4 +350,4 @@ public class VTPExecutionResourceTest { public void testGetTestExecutionLogsHandler() throws Exception { assertNotNull(vtpExecutionResource.getTestExecutionLogsHandler("1234", "action")); } -}
\ No newline at end of file +} diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/execution/VTPExecutionResultsSupplierTest.java b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/execution/VTPExecutionResultsSupplierTest.java new file mode 100644 index 00000000..7be8a7d4 --- /dev/null +++ b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/execution/VTPExecutionResultsSupplierTest.java @@ -0,0 +1,88 @@ +/** + * Copyright 2020 Nokia. + * + * 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.vtp.execution; + +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class VTPExecutionResultsSupplierTest { + + private static final String TEST_PATH_TO_EXECUTION = "src/test/resources/executions"; + + @Test + public void whenGetExecutionOutputsFromFileIsCalledWithPathToCorrectFileThenContentShouldBeLoadedAsJson() { + // given + VTPExecutionResultsSupplier vtpExecutionResultsSupplier = + new VTPExecutionResultsSupplier(TEST_PATH_TO_EXECUTION); + String pathToCorrectFile = "test-01-request-id-execution-id"; + JsonElement expectedResult = new Gson().fromJson("" + + "[{" + + "\"test_1\": \"error01\"," + + "\"test_2\": \"error02\" " + + "}]", JsonArray.class); + + // when + JsonElement executionOutputsFromFile = + vtpExecutionResultsSupplier.getExecutionOutputsFromFile(pathToCorrectFile); + + // then + assertEquals(executionOutputsFromFile, expectedResult); + } + + @Test + public void whenGetExecutionOutputsFromFileIsCalledWithPathToNonExistingFileThenProperMessageShouldBeReturned() { + // given + VTPExecutionResultsSupplier vtpExecutionResultsSupplier = + new VTPExecutionResultsSupplier(TEST_PATH_TO_EXECUTION); + String pathToCorrectFile = "test-02-request-id-execution-id"; + JsonElement expectedErrorMessage = new Gson().fromJson("" + + "[{ \"error\": \"unable to find execution results\"}]", JsonArray.class); + + // when + JsonElement executionOutputsFromFile = + vtpExecutionResultsSupplier.getExecutionOutputsFromFile(pathToCorrectFile); + + // then + assertEquals(executionOutputsFromFile, expectedErrorMessage); + } + + @Test + public void whenGetExecutionOutputsFromFileIsCalledWithPathToIncorrectFileThenProperMessageShouldBeReturned() { + // given + VTPExecutionResultsSupplier vtpExecutionResultsSupplier = + new VTPExecutionResultsSupplier(TEST_PATH_TO_EXECUTION); + String pathToCorrectFile = "test-incorrect-request-id-execution-id-data"; + JsonElement expectedErrorMessage = new Gson().fromJson("" + + "[{ " + + "\"error\": \"fail to load execution result\"," + + "\"reason\":\"Expected a com.google.gson.JsonArray but was com.google.gson.JsonPrimitive\"" + + "}]", + JsonArray.class + ); + + // when + JsonElement executionOutputsFromFile = + vtpExecutionResultsSupplier.getExecutionOutputsFromFile(pathToCorrectFile); + + // then + assertEquals(executionOutputsFromFile, expectedErrorMessage); + } +} diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/test/resources/executions/test-01-request-id-execution-id-data/output b/vnfmarket-be/vnf-sdk-marketplace/src/test/resources/executions/test-01-request-id-execution-id-data/output new file mode 100644 index 00000000..e52c4f4e --- /dev/null +++ b/vnfmarket-be/vnf-sdk-marketplace/src/test/resources/executions/test-01-request-id-execution-id-data/output @@ -0,0 +1,6 @@ +[ + { + "test_1": "error01", + "test_2": "error02" + } +] diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/test/resources/executions/test-incorrect-request-id-execution-id-data/output b/vnfmarket-be/vnf-sdk-marketplace/src/test/resources/executions/test-incorrect-request-id-execution-id-data/output new file mode 100644 index 00000000..747c69b0 --- /dev/null +++ b/vnfmarket-be/vnf-sdk-marketplace/src/test/resources/executions/test-incorrect-request-id-execution-id-data/output @@ -0,0 +1 @@ +wrong output, not a JSON |