add junit coverage
[so.git] / so-optimization-clients / src / main / java / org / onap / so / client / oof / OofValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018 Intel Corp. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.client.oof;
22
23
24 import java.util.Map;
25 import org.json.JSONObject;
26 import org.onap.so.client.exception.BadResponseException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.stereotype.Component;
30 import static org.apache.commons.lang3.StringUtils.isNotBlank;
31
32
33 @Component
34 public class OofValidator {
35
36     private static final Logger logger = LoggerFactory.getLogger(OofValidator.class);
37
38     /**
39      * Validates the synchronous homing response from oof
40      *
41      * @throws BadResponseException when validation failed
42      */
43     public void validateDemandsResponse(Map<?, ?> response) throws BadResponseException {
44         logger.debug("Validating oofs synchronous response");
45         if (!response.isEmpty()) {
46             JSONObject jsonResponse = new JSONObject(response);
47             if (jsonResponse.has("requestStatus")) {
48                 String status = jsonResponse.getString("requestStatus");
49                 if (status.equals("accepted")) {
50                     logger.debug("oofs synchronous response indicates accepted");
51                 } else {
52                     String message = jsonResponse.getString("statusMessage");
53                     if (isNotBlank(message)) {
54                         logger.debug("oofs response indicates failed: {}", message);
55                     } else {
56                         logger.debug("oofs response indicates failed: no status message provided");
57                         message = "error message not provided";
58                     }
59                     throw new BadResponseException("oofs synchronous response indicates failed: {}", message);
60                 }
61             } else {
62                 logger.debug("oofs synchronous response does not contain: request status");
63                 throw new BadResponseException("oofs synchronous response does not contain: request status");
64             }
65         } else {
66             logger.debug("oofs synchronous response is empty");
67             throw new BadResponseException("oofs synchronous response i is empty");
68         }
69     }
70
71     /**
72      * Validates the asynchronous/callback response from oof which contains the homing and licensing solutions
73      *
74      * @throws BadResponseException when validation failed
75      */
76     public void validateSolution(String response) throws BadResponseException {
77         logger.debug("Validating oofs asynchronous callback response");
78         if (isNotBlank(response)) {
79             JSONObject jsonResponse = new JSONObject(response);
80             if (!jsonResponse.has("serviceException")) {
81                 logger.debug("oofs asynchronous response is valid");
82             } else {
83                 String message = jsonResponse.getJSONObject("serviceException").getString("text");
84                 if (isNotBlank(message)) {
85                     logger.debug("oofs response contains a service exception: {}", message);
86                 } else {
87                     logger.debug("oofs response contains a service exception: no service exception text provided");
88                     message = "error message not provided";
89                 }
90                 throw new BadResponseException("oofs asynchronous response contains a service exception: " + message);
91             }
92         } else {
93             logger.debug("oofs asynchronous response is empty");
94             throw new BadResponseException("oofs asynchronous response is empty");
95         }
96     }
97
98 }