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.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.JsonParser;
29 import com.google.gson.JsonSyntaxException;
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;
37 * Class for unit testing PolicyDecoderConfigurationParametersJsonAdapter class.
39 * @author Adheli Tavares (adheli.tavares@est.tech)
42 public class TestPolicyDecoderConfigurationParametersJsonAdapter {
45 void testDeserialize() throws JsonSyntaxException, IOException {
46 final var validJsonFile = "src/test/resources/PolicyDecoderConfiguration.json";
47 final var mockJsonElement = JsonParser.parseString(getJsonValues(validJsonFile));
49 var gson = new GsonBuilder().registerTypeAdapter(PolicyDecoderConfigurationParameterGroup.class,
50 new PolicyDecoderConfigurationParametersJsonAdapter()).create();
52 var result = gson.fromJson(mockJsonElement, PolicyDecoderConfigurationParameterGroup.class);
54 assertNotNull(result);
55 assertEquals(result.getClass(), DummyPolicyDecoderParameterGroup.class);
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";
63 validateParsing(jsonFile, expectedErrorMsg);
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";
71 validateParsing(jsonFile, expectedErrorMsg);
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";
80 validateParsing(jsonFile, expectedErrorMsg);
83 private void validateParsing(final String jsonFile, String expectedErrorMsg) throws IOException {
84 final var mockJsonElement = JsonParser.parseString(getJsonValues(jsonFile));
86 var gson = new GsonBuilder().registerTypeAdapter(PolicyDecoderConfigurationParameterGroup.class,
87 new PolicyDecoderConfigurationParametersJsonAdapter()).create();
89 assertThatThrownBy(() -> gson.fromJson(mockJsonElement, PolicyDecoderConfigurationParameterGroup.class))
90 .isInstanceOf(IllegalArgumentException.class).hasMessage(expectedErrorMsg);
93 private String getJsonValues(String path) throws IOException {
94 return Files.readString(new File(path).toPath());