Refactor policy/api csit tests
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / parameters / ApiParameterHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
4  * ================================================================================
5  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
6  * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.api.main.parameters;
25
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import java.io.FileReader;
29 import org.onap.policy.api.main.exception.PolicyApiException;
30 import org.onap.policy.api.main.startstop.ApiCommandLineArguments;
31 import org.onap.policy.common.parameters.ValidationResult;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * This class handles reading, parsing and validating of policy api parameters from JSON files.
37  */
38 public class ApiParameterHandler {
39
40     private static final Logger LOGGER = LoggerFactory.getLogger(ApiParameterHandler.class);
41
42     /**
43      * Read the parameters from the parameter file.
44      *
45      * @param arguments the arguments passed to policy api
46      * @return the parameters read from the configuration file
47      * @throws PolicyApiException on parameter exceptions
48      */
49     public ApiParameterGroup getParameters(final ApiCommandLineArguments arguments)
50             throws PolicyApiException {
51         ApiParameterGroup apiParameterGroup = null;
52
53         // Read the parameters
54         try {
55             // Read the parameters from JSON using Gson
56             final Gson gson = new GsonBuilder().create();
57             apiParameterGroup = gson.fromJson(new FileReader(arguments.getFullConfigurationFilePath()),
58                     ApiParameterGroup.class);
59         } catch (final Exception e) {
60             final String errorMessage = "error reading parameters from \"" + arguments.getConfigurationFilePath()
61                     + "\"\n" + "(" + e.getClass().getSimpleName() + "):" + e.getMessage();
62             throw new PolicyApiException(errorMessage, e);
63         }
64
65         // The JSON processing returns null if there is an empty file
66         if (apiParameterGroup == null) {
67             final String errorMessage = "no parameters found in \"" + arguments.getConfigurationFilePath() + "\"";
68             LOGGER.error(errorMessage);
69             throw new PolicyApiException(errorMessage);
70         }
71
72         // validate the parameters
73         final ValidationResult validationResult = apiParameterGroup.validate();
74         if (!validationResult.isValid()) {
75             String returnMessage =
76                     "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath() + "\"\n";
77             returnMessage += validationResult.getResult();
78
79             LOGGER.error(returnMessage);
80             throw new PolicyApiException(returnMessage);
81         }
82
83         return apiParameterGroup;
84     }
85 }