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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.distribution.reception.parameters;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
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;
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;
40 * Class for unit testing ReceptionHandlerConfigurationParametersJsonAdapter class.
42 * @author Adheli Tavares (adheli.tavares@est.tech)
45 public class TestReceptionHandlerConfigurationParametersJsonAdapter {
48 public void testDeserialize() throws JsonSyntaxException, IOException {
49 final String validJsonFile = "src/test/resources/ReceptionHandlerConfiguration.json";
50 final JsonElement mockJsonElement = JsonParser.parseString(getJsonValues(validJsonFile));
52 Gson gson = new GsonBuilder().registerTypeAdapter(ReceptionHandlerConfigurationParameterGroup.class,
53 new ReceptionHandlerConfigurationParametersJsonAdapter()).create();
55 ReceptionHandlerConfigurationParameterGroup result =
56 gson.fromJson(mockJsonElement, ReceptionHandlerConfigurationParameterGroup.class);
58 assertNotNull(result);
59 assertEquals(result.getClass(), DummyReceptionHandlerParameterGroup.class);
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";
67 validateParsing(jsonFile, expectedErrorMsg);
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";
75 validateParsing(jsonFile, expectedErrorMsg);
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";
84 validateParsing(jsonFile, expectedErrorMsg);
87 private void validateParsing(final String jsonFile, String expectedErrorMsg) throws IOException {
88 final JsonElement mockJsonElement = JsonParser.parseString(getJsonValues(jsonFile));
90 Gson gson = new GsonBuilder().registerTypeAdapter(ReceptionHandlerConfigurationParameterGroup.class,
91 new ReceptionHandlerConfigurationParametersJsonAdapter()).create();
93 assertThatThrownBy(() -> gson.fromJson(mockJsonElement, ReceptionHandlerConfigurationParameterGroup.class))
94 .isInstanceOf(IllegalArgumentException.class).hasMessage(expectedErrorMsg);
97 private String getJsonValues(String path) throws IOException {
98 return new String(Files.readAllBytes(new File(path).toPath()), StandardCharsets.UTF_8);