ae85e0dcef7a42f1582b0dba8c56688fe2673f54
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.reception.parameters;
22
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26
27 import com.google.gson.Gson;
28 import com.google.gson.GsonBuilder;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonParser;
31 import com.google.gson.JsonSyntaxException;
32 import java.io.File;
33 import java.io.IOException;
34 import java.nio.charset.StandardCharsets;
35 import java.nio.file.Files;
36 import org.junit.Test;
37 import org.onap.policy.distribution.reception.testclasses.DummyReceptionHandlerParameterGroup;
38
39 /**
40  * Class for unit testing ReceptionHandlerConfigurationParametersJsonAdapter class.
41  *
42  * @author Adheli Tavares (adheli.tavares@est.tech)
43  *
44  */
45 public class TestReceptionHandlerConfigurationParametersJsonAdapter {
46
47     @Test
48     public void testDeserialize() throws JsonSyntaxException, IOException {
49         final String validJsonFile = "src/test/resources/ReceptionHandlerConfiguration.json";
50         final JsonElement mockJsonElement = JsonParser.parseString(getJsonValues(validJsonFile));
51
52         Gson gson = new GsonBuilder().registerTypeAdapter(ReceptionHandlerConfigurationParameterGroup.class,
53                 new ReceptionHandlerConfigurationParametersJsonAdapter()).create();
54
55         ReceptionHandlerConfigurationParameterGroup result =
56                 gson.fromJson(mockJsonElement, ReceptionHandlerConfigurationParameterGroup.class);
57
58         assertNotNull(result);
59         assertEquals(result.getClass(), DummyReceptionHandlerParameterGroup.class);
60     }
61
62     @Test
63     public void testDeserialize_shouldThrowExceptionEmptyClassName() throws JsonSyntaxException, IOException {
64         final String jsonFile = "src/test/resources/EmptyClassName.json";
65         String expectedErrorMsg = "parameter \"parameterClassName\" value \"\" invalid in JSON file";
66
67         validateParsing(jsonFile, expectedErrorMsg);
68     }
69
70     @Test
71     public void testDeserialize_shouldThrowExceptionNullClassName() throws JsonSyntaxException, IOException {
72         final String jsonFile = "src/test/resources/NullClassName.json";
73         String expectedErrorMsg = "parameter \"parameterClassName\" value \"null\" invalid in JSON file";
74
75         validateParsing(jsonFile, expectedErrorMsg);
76     }
77
78     @Test
79     public void testDeserialize_shouldThrowExceptionWrongClassName() throws JsonSyntaxException, IOException {
80         final String jsonFile = "src/test/resources/WrongClassName.json";
81         String expectedErrorMsg = "parameter \"parameterClassName\" value "
82                 + "\"org.onap.policy.distribution.reception.testclasses.NotExistentClass\", could not find class";
83
84         validateParsing(jsonFile, expectedErrorMsg);
85     }
86
87     private void validateParsing(final String jsonFile, String expectedErrorMsg) throws IOException {
88         final JsonElement mockJsonElement = JsonParser.parseString(getJsonValues(jsonFile));
89
90         Gson gson = new GsonBuilder().registerTypeAdapter(ReceptionHandlerConfigurationParameterGroup.class,
91                 new ReceptionHandlerConfigurationParametersJsonAdapter()).create();
92
93         assertThatThrownBy(() -> gson.fromJson(mockJsonElement, ReceptionHandlerConfigurationParameterGroup.class))
94                 .isInstanceOf(IllegalArgumentException.class).hasMessage(expectedErrorMsg);
95     }
96
97     private String getJsonValues(String path) throws IOException {
98         return new String(Files.readAllBytes(new File(path).toPath()), StandardCharsets.UTF_8);
99     }
100
101 }