Remove unused method
[portal/nonrtric-controlpanel.git] / webapp-backend / src / test / java / org / oransc / portal / nonrtric / controlpanel / mock / PolicyControllerMockConfiguration.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * Modifications Copyright (C) 2020 Nordix Foundation
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  * ========================LICENSE_END===================================
20  */
21 package org.oransc.portal.nonrtric.controlpanel.mock;
22
23 import com.google.gson.GsonBuilder;
24
25 import java.io.BufferedReader;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.lang.invoke.MethodHandles;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Optional;
35 import java.util.Vector;
36 import java.util.stream.Collectors;
37
38 import org.oransc.portal.nonrtric.controlpanel.model.PolicyInfo;
39 import org.oransc.portal.nonrtric.controlpanel.model.PolicyInstances;
40 import org.oransc.portal.nonrtric.controlpanel.model.PolicyType;
41 import org.oransc.portal.nonrtric.controlpanel.model.PolicyTypes;
42 import org.oransc.portal.nonrtric.controlpanel.policyagentapi.PolicyAgentApi;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.boot.test.context.TestConfiguration;
46 import org.springframework.context.annotation.Bean;
47 import org.springframework.http.HttpStatus;
48 import org.springframework.http.ResponseEntity;
49 import org.springframework.web.client.RestClientException;
50
51 /**
52  * Creates a mock implementation of the policy controller client API.
53  */
54 @TestConfiguration
55 public class PolicyControllerMockConfiguration {
56
57     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
58
59     private static com.google.gson.Gson gson = new GsonBuilder() //
60         .serializeNulls() //
61         .create(); //
62
63     @Bean
64     public PolicyAgentApi policyAgentApi() {
65         MockPolicyAgentApi apiClient = new MockPolicyAgentApi();
66         return apiClient;
67     }
68
69     class MockPolicyAgentApi implements PolicyAgentApi {
70         private final Database database = new Database();
71
72         @Override
73         public ResponseEntity<Object> getPolicyInstance(String id) {
74             return new ResponseEntity<>(database.getInstance(id), HttpStatus.OK);
75         }
76
77         @Override
78         public ResponseEntity<String> putPolicy(String policyTypeIdString, String policyInstanceId, Object json,
79             String ric) {
80             database.putInstance(policyTypeIdString, policyInstanceId, json, ric);
81             return new ResponseEntity<>(HttpStatus.OK);
82         }
83
84         @Override
85         public ResponseEntity<String> deletePolicy(String policyInstanceId) {
86             database.deleteInstance(policyInstanceId);
87             return new ResponseEntity<>(HttpStatus.OK);
88         }
89
90         @Override
91         public ResponseEntity<String> getAllPolicyTypes() {
92             PolicyTypes result = new PolicyTypes();
93             result.addAll(database.getTypes());
94             return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
95         }
96
97         @Override
98         public ResponseEntity<String> getPolicyInstancesForType(String type) {
99             PolicyInstances result = new PolicyInstances();
100             List<PolicyInfo> inst = database.getInstances(Optional.of(type));
101             result.addAll(inst);
102             return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
103         }
104
105         @Override
106         public ResponseEntity<String> getRicsSupportingType(String typeName) {
107             Vector<String> res = new Vector<>();
108             res.add("ric_1");
109             res.add("ric_2");
110             res.add("ric_3");
111             return new ResponseEntity<>(gson.toJson(res), HttpStatus.OK);
112         }
113     }
114
115     class Database {
116
117         Database() {
118             PolicyType policyType = new PolicyType("", "{}");
119             types.put("", policyType);
120
121             String schema = getStringFromFile("demo-policy-schema-1.json");
122             policyType = new PolicyType("type2", schema);
123             types.put("type2", policyType);
124
125             schema = getStringFromFile("demo-policy-schema-2.json");
126             policyType = new PolicyType("type3", schema);
127             types.put("type3", policyType);
128
129             schema = getStringFromFile("demo-policy-schema-3.json");
130             policyType = new PolicyType("type4", schema);
131             types.put("type4", policyType);
132
133             putInstance("", "123", "{\"data\":\"data\"}", "ric_1");
134         }
135
136         private String getStringFromFile(String path) {
137             try {
138                 InputStream inputStream =
139                     MethodHandles.lookup().lookupClass().getClassLoader().getResourceAsStream(path);
140                 return new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n"));
141             } catch (Exception e) {
142                 logger.error("Cannot read file :" + path, e);
143                 return "";
144             }
145         }
146
147         String normalize(String str) {
148             return str.replace('\n', ' ');
149         }
150
151         private String getTimeStampUTC() {
152             return java.time.Instant.now().toString();
153         }
154
155         void putInstance(String typeId, String instanceId, Object instanceData, String ric) {
156             PolicyInfo i = PolicyInfo.builder() //
157                 .policyData(instanceData) //
158                 .lastModified(getTimeStampUTC()) //
159                 .policyId(instanceId) //
160                 .ricId(ric) //
161                 .serviceId("service") //
162                 .policyTypeId(typeId) //
163                 .build(); //
164             instances.put(instanceId, i);
165         }
166
167         public void deleteInstance(String instanceId) {
168             instances.remove(instanceId);
169         }
170
171         Object getInstance(String id) throws RestClientException {
172             PolicyInfo i = instances.get(id);
173             if (i == null) {
174                 throw new RestClientException("Type not found: " + id);
175             }
176             return i.policyData;
177         }
178
179         public Collection<PolicyType> getTypes() {
180             return types.values();
181         }
182
183         public List<PolicyInfo> getInstances(Optional<String> typeId) {
184             ArrayList<PolicyInfo> result = new ArrayList<>();
185             for (PolicyInfo i : instances.values()) {
186                 if (typeId.isPresent()) {
187                     if (i.policyTypeId.equals(typeId.get())) {
188                         result.add(i);
189                     }
190
191                 } else {
192                     result.add(i);
193                 }
194             }
195             return result;
196         }
197
198         private Map<String, PolicyType> types = new HashMap<>();
199         private Map<String, PolicyInfo> instances = new HashMap<>();
200
201     }
202
203 }