d3a0e3d409a4ca90e5e47a26786baa903186b122
[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.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.JsonParser;
29 import com.google.gson.JsonSyntaxException;
30 import java.io.File;
31 import java.io.IOException;
32 import java.nio.file.Files;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.distribution.reception.testclasses.DummyPolicyDecoderParameterGroup;
35
36 /**
37  * Class for unit testing PolicyDecoderConfigurationParametersJsonAdapter class.
38  *
39  * @author Adheli Tavares (adheli.tavares@est.tech)
40  *
41  */
42 public class TestPolicyDecoderConfigurationParametersJsonAdapter {
43
44     @Test
45     void testDeserialize() throws JsonSyntaxException, IOException {
46         final var validJsonFile = "src/test/resources/PolicyDecoderConfiguration.json";
47         final var mockJsonElement = JsonParser.parseString(getJsonValues(validJsonFile));
48
49         var gson = new GsonBuilder().registerTypeAdapter(PolicyDecoderConfigurationParameterGroup.class,
50                 new PolicyDecoderConfigurationParametersJsonAdapter()).create();
51
52         var result = gson.fromJson(mockJsonElement, PolicyDecoderConfigurationParameterGroup.class);
53
54         assertNotNull(result);
55         assertEquals(result.getClass(), DummyPolicyDecoderParameterGroup.class);
56     }
57
58     @Test
59     void testDeserialize_shouldThrowExceptionEmptyClassName() throws JsonSyntaxException, IOException {
60         final var jsonFile = "src/test/resources/EmptyClassName.json";
61         var expectedErrorMsg = "parameter \"parameterClassName\" value \"\" invalid in JSON file";
62
63         validateParsing(jsonFile, expectedErrorMsg);
64     }
65
66     @Test
67     void testDeserialize_shouldThrowExceptionNullClassName() throws JsonSyntaxException, IOException {
68         final var jsonFile = "src/test/resources/NullClassName.json";
69         var expectedErrorMsg = "parameter \"parameterClassName\" value \"null\" invalid in JSON file";
70
71         validateParsing(jsonFile, expectedErrorMsg);
72     }
73
74     @Test
75     void testDeserialize_shouldThrowExceptionWrongClassName() throws JsonSyntaxException, IOException {
76         final var jsonFile = "src/test/resources/WrongClassName.json";
77         var expectedErrorMsg = "parameter \"parameterClassName\" value "
78                 + "\"org.onap.policy.distribution.reception.testclasses.NotExistentClass\", could not find class";
79
80         validateParsing(jsonFile, expectedErrorMsg);
81     }
82
83     private void validateParsing(final String jsonFile, String expectedErrorMsg) throws IOException {
84         final var mockJsonElement = JsonParser.parseString(getJsonValues(jsonFile));
85
86         var gson = new GsonBuilder().registerTypeAdapter(PolicyDecoderConfigurationParameterGroup.class,
87                 new PolicyDecoderConfigurationParametersJsonAdapter()).create();
88
89         assertThatThrownBy(() -> gson.fromJson(mockJsonElement, PolicyDecoderConfigurationParameterGroup.class))
90                 .isInstanceOf(IllegalArgumentException.class).hasMessage(expectedErrorMsg);
91     }
92
93     private String getJsonValues(String path) throws IOException {
94         return Files.readString(new File(path).toPath());
95     }
96
97 }