add junit coverage
[so.git] / so-optimization-clients / src / main / java / org / onap / so / client / sniro / SniroValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.client.sniro;
24
25
26 import static org.apache.commons.lang3.StringUtils.*;
27 import java.util.Map;
28 import org.json.JSONObject;
29 import org.onap.so.client.exception.BadResponseException;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.stereotype.Component;
33
34
35
36 @Component
37 public class SniroValidator {
38
39     private static final Logger logger = LoggerFactory.getLogger(SniroValidator.class);
40
41     private static final String MESSAGE_NOT_PROVIDED = "error message not provided";
42
43     /**
44      * Validates the synchronous homing response from sniro manager
45      *
46      * @throws BadResponseException
47      */
48     public void validateDemandsResponse(Map<String, Object> response) throws BadResponseException {
49         logger.debug("Validating Sniro Managers synchronous response");
50         if (!response.isEmpty()) {
51             JSONObject jsonResponse = new JSONObject(response);
52             if (jsonResponse.has("requestStatus")) {
53                 String status = jsonResponse.getString("requestStatus");
54                 if ("accepted".equals(status)) {
55                     logger.debug("Sniro Managers synchronous response indicates accepted");
56                 } else {
57                     String message = jsonResponse.getString("statusMessage");
58                     if (isNotBlank(message)) {
59                         logger.debug("Sniro Managers response indicates failed: {}", message);
60                     } else {
61                         logger.debug("Sniro Managers response indicates failed: no status message provided");
62                         message = MESSAGE_NOT_PROVIDED;
63                     }
64                     throw new BadResponseException("Sniro Managers synchronous response indicates failed: " + message);
65                 }
66             } else {
67                 logger.debug("Sniro Managers synchronous response does not contain: request status");
68                 throw new BadResponseException("Sniro Managers synchronous response does not contain: request status");
69             }
70         } else {
71             logger.debug("Sniro Managers synchronous response is empty");
72             throw new BadResponseException("Sniro Managers synchronous response is empty");
73         }
74     }
75
76     /**
77      * Validates the asynchronous/callback response from sniro manager which contains the homing and licensing solutions
78      *
79      * @throws BadResponseException
80      */
81     public static void validateSolution(String response) throws BadResponseException {
82         logger.debug("Validating Sniro Managers asynchronous callback response");
83         if (isNotBlank(response)) {
84             JSONObject jsonResponse = new JSONObject(response);
85             if (!jsonResponse.has("serviceException")) {
86                 logger.debug("Sniro Managers asynchronous response is valid");
87             } else {
88                 String message = jsonResponse.getJSONObject("serviceException").getString("text");
89                 if (isNotBlank(message)) {
90                     logger.debug("Sniro Managers response contains a service exception: {}", message);
91                 } else {
92                     logger.debug(
93                             "Sniro Managers response contains a service exception: no service exception text provided");
94                     message = MESSAGE_NOT_PROVIDED;
95                 }
96                 throw new BadResponseException(
97                         "Sniro Managers asynchronous response contains a service exception: " + message);
98             }
99         } else {
100             logger.debug("Sniro Managers asynchronous response is empty");
101             throw new BadResponseException("Sniro Managers asynchronous response is empty");
102         }
103     }
104
105
106     /**
107      * Validates the release response from sniro conductor
108      *
109      * @throws BadResponseException
110      */
111     public void validateReleaseResponse(Map<String, Object> response) throws BadResponseException {
112         logger.debug("Validating Sniro Conductors response");
113         if (!response.isEmpty()) {
114             String status = (String) response.get("status");
115             if (isNotBlank(status)) {
116                 if ("success".equals(status)) {
117                     logger.debug("Sniro Conductors synchronous response indicates success");
118                 } else {
119                     String message = (String) response.get("message");
120                     if (isNotBlank(message)) {
121                         logger.debug("Sniro Conductors response indicates failed: {}", message);
122                     } else {
123                         logger.debug("Sniro Conductors response indicates failed: error message not provided");
124                         message = MESSAGE_NOT_PROVIDED;
125                     }
126                     throw new BadResponseException(
127                             "Sniro Conductors synchronous response indicates failed: " + message);
128                 }
129             } else {
130                 logger.debug("Sniro Managers Conductors response does not contain: status");
131                 throw new BadResponseException("Sniro Conductors synchronous response does not contain: status");
132             }
133         } else {
134             logger.debug("Sniro Conductors response is empty");
135             throw new BadResponseException("Sniro Conductors response is empty");
136         }
137
138     }
139
140 }