Add list of errors in responce for GET request.
[vnfsdk/refrepo.git] / vnfmarket-be / vnf-sdk-marketplace / src / test / java / org / onap / vtp / execution / VTPExecutionResultsSupplierTest.java
1 /**
2  * Copyright 2020 Nokia.
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.vtp.execution;
18
19 import com.google.gson.Gson;
20 import com.google.gson.JsonArray;
21 import com.google.gson.JsonElement;
22 import org.junit.Test;
23
24 import static org.junit.Assert.assertEquals;
25
26 public class VTPExecutionResultsSupplierTest {
27
28     private static final String TEST_PATH_TO_EXECUTION = "src/test/resources/executions";
29
30     @Test
31     public void whenGetExecutionOutputsFromFileIsCalledWithPathToCorrectFileThenContentShouldBeLoadedAsJson() {
32         // given
33         VTPExecutionResultsSupplier vtpExecutionResultsSupplier =
34             new VTPExecutionResultsSupplier(TEST_PATH_TO_EXECUTION);
35         String pathToCorrectFile = "test-01-request-id-execution-id";
36         JsonElement expectedResult = new Gson().fromJson("" +
37             "[{" +
38             "\"test_1\": \"error01\"," +
39             "\"test_2\": \"error02\" " +
40             "}]", JsonArray.class);
41
42         // when
43         JsonElement executionOutputsFromFile =
44             vtpExecutionResultsSupplier.getExecutionOutputsFromFile(pathToCorrectFile);
45
46         // then
47         assertEquals(executionOutputsFromFile, expectedResult);
48     }
49
50     @Test
51     public void whenGetExecutionOutputsFromFileIsCalledWithPathToNonExistingFileThenProperMessageShouldBeReturned() {
52         // given
53         VTPExecutionResultsSupplier vtpExecutionResultsSupplier =
54             new VTPExecutionResultsSupplier(TEST_PATH_TO_EXECUTION);
55         String pathToCorrectFile = "test-02-request-id-execution-id";
56         JsonElement expectedErrorMessage = new Gson().fromJson("" +
57             "[{ \"error\": \"unable to find execution results\"}]", JsonArray.class);
58
59         // when
60         JsonElement executionOutputsFromFile =
61             vtpExecutionResultsSupplier.getExecutionOutputsFromFile(pathToCorrectFile);
62
63         // then
64         assertEquals(executionOutputsFromFile, expectedErrorMessage);
65     }
66
67     @Test
68     public void whenGetExecutionOutputsFromFileIsCalledWithPathToIncorrectFileThenProperMessageShouldBeReturned() {
69         // given
70         VTPExecutionResultsSupplier vtpExecutionResultsSupplier =
71             new VTPExecutionResultsSupplier(TEST_PATH_TO_EXECUTION);
72         String pathToCorrectFile = "test-incorrect-request-id-execution-id-data";
73         JsonElement expectedErrorMessage = new Gson().fromJson("" +
74             "[{ " +
75                 "\"error\": \"fail to load execution result\"," +
76                 "\"reason\":\"Expected a com.google.gson.JsonArray but was com.google.gson.JsonPrimitive\"" +
77                 "}]",
78             JsonArray.class
79         );
80
81         // when
82         JsonElement executionOutputsFromFile =
83             vtpExecutionResultsSupplier.getExecutionOutputsFromFile(pathToCorrectFile);
84
85         // then
86         assertEquals(executionOutputsFromFile, expectedErrorMessage);
87     }
88 }