Fixes for sonar critical issues
[policy/engine.git] / PolicyEngineClient / src / test / java / org / onap / policyengine / ClosedLoopPolicyClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineClient
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. 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.policyengine;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.StringReader;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.text.SimpleDateFormat;
32 import java.util.Date;
33 import java.util.UUID;
34
35 import javax.json.Json;
36 import javax.json.JsonObject;
37 import javax.json.JsonReader;
38
39 import org.onap.policy.api.PolicyChangeResponse;
40 import org.onap.policy.api.PolicyConfigType;
41 import org.onap.policy.api.PolicyEngine;
42 import org.onap.policy.api.PolicyParameters;
43 import org.onap.policy.api.PolicyType;
44
45 public class ClosedLoopPolicyClient {
46         //For updating a ClosedLoop_Fault policy set the "isEdit" flag to true.   
47         //For creating a ClosedLoop_Fault policy set the "isEdit" flag to false.  
48         static Boolean isEdit = false;
49              
50         //Builds JSONObject from File  
51         private static JsonObject buildJSON(File jsonInput, String jsonString) throws FileNotFoundException {
52             JsonObject json = null;;
53             JsonReader jsonReader = null;    
54             if (jsonString != null && jsonInput == null) {
55                 StringReader in = null;
56                     in = new StringReader(jsonString);
57                     jsonReader = Json.createReader(in);
58                     json = jsonReader.readObject();
59                     in.close();
60             }else {
61                 InputStream in = null;
62                 in = new FileInputStream(jsonInput);
63                 jsonReader = Json.createReader(in);
64                 json = jsonReader.readObject();
65                 try {
66                                 in.close();
67                         } catch (IOException e) {
68                                 System.err.println("Exception Occured while closing input stream"+e);
69                         }
70             }
71             jsonReader.close();
72                    
73             return json;
74         }
75         public static void main(String[] args) {
76                         try {
77                                 PolicyEngine policyEngine = new PolicyEngine("config.properties");
78                                 PolicyParameters policyParameters = new PolicyParameters();
79                                 // Set Policy Type 
80                                 policyParameters.setPolicyConfigType(PolicyConfigType.ClosedLoop_Fault);
81                                 policyParameters.setPolicyName("MikeAPItests.ClosedLoopFaultApiTest45");
82                                 policyParameters.setPolicyDescription("This is a sample ClosedLoop_Fault policy CREATE example");
83                                 //policyParameters.setPolicyScope("MikeAPItests");
84                                 
85                                 // Set up Micro Services Attributes 
86                                 File jsonFile = null;
87                                 String MSjsonString= null;
88                                 Path file = Paths.get("C:\\policyAPI\\ClosedLoopJSON\\faultTestJson.json");
89                                 jsonFile = file.toFile();
90                                 
91                                 policyParameters.setConfigBody(buildJSON(jsonFile, MSjsonString).toString());           
92                                 policyParameters.setConfigBodyType(PolicyType.JSON);
93
94                                 policyParameters.setRequestID(UUID.randomUUID());
95                     // Set Safe Policy value for Risk Type
96                                 SimpleDateFormat dateformat3 = new SimpleDateFormat("dd/MM/yyyy");
97                                 Date date = dateformat3.parse("15/10/2016");
98                                 policyParameters.setTtlDate(date);
99                                 // Set Safe Policy value for Guard
100                                 policyParameters.setGuard(true);
101                                 // Set Safe Policy value for Risk Level
102                                 policyParameters.setRiskLevel("5");
103                                 // Set Safe Policy value for Risk Type
104                                 policyParameters.setRiskType("PROD");
105                                 
106                                 // API method to create or update Policy. 
107                         PolicyChangeResponse response = null;
108                         if (!isEdit) {
109                             response = policyEngine.createPolicy(policyParameters);
110                         } 
111                         else {  
112                                 response = policyEngine.updatePolicy(policyParameters);
113                         }
114                         
115                                 if(response.getResponseCode()==200){
116                                         System.out.println(response.getResponseMessage());
117                                         System.out.println("Policy Created Successfully!");
118                                 }else{
119                                         System.out.println("Error! " + response.getResponseMessage());
120                                 }
121                         } catch (Exception e) {
122                                 System.err.println(e.getMessage());
123                         }
124                 }
125 }