2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
20 package org.oransc.ric.portal.dashboard.config;
22 import java.io.BufferedReader;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.lang.invoke.MethodHandles;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.List;
31 import java.util.Optional;
32 import java.util.stream.Collectors;
34 import org.oransc.ric.portal.dashboard.model.ImmutablePolicyInfo;
35 import org.oransc.ric.portal.dashboard.model.PolicyInfo;
36 import org.oransc.ric.portal.dashboard.model.PolicyInstances;
37 import org.oransc.ric.portal.dashboard.model.PolicyType;
38 import org.oransc.ric.portal.dashboard.model.PolicyTypes;
39 import org.oransc.ric.portal.dashboard.policyagentapi.PolicyAgentApi;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.boot.test.context.TestConfiguration;
43 import org.springframework.context.annotation.Bean;
44 import org.springframework.web.client.RestClientException;
47 * Creates a mock implementation of the policy controller client API.
50 public class PolicyControllerMockConfiguration {
52 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
55 public PolicyAgentApi policyAgentApi() {
56 MockPolicyAgentApi apiClient = new MockPolicyAgentApi();
60 class MockPolicyAgentApi implements PolicyAgentApi {
61 private final Database database = new Database();
64 public String getPolicyInstance(String id) throws RestClientException {
65 return database.getInstance(id);
69 public void putPolicy(String policyTypeIdString, String policyInstanceId, String json)
70 throws RestClientException {
71 database.putInstance(policyTypeIdString, policyInstanceId, json);
75 public void deletePolicy(String policyInstanceId) throws RestClientException {
76 database.deleteInstance(policyInstanceId);
80 public PolicyTypes getAllPolicyTypes() throws RestClientException {
81 PolicyTypes result = new PolicyTypes();
82 result.addAll(database.getTypes());
87 public PolicyInstances getPolicyInstancesForType(String type) {
88 PolicyInstances result = new PolicyInstances();
89 List<PolicyInfo> inst = database.getInstances(Optional.of(type));
98 String schema = getStringFromFile("anr-policy-schema.json");
99 PolicyType policy = new PolicyType("ANR", schema);
100 types.put("ANR", policy);
102 schema = getStringFromFile("demo-policy-schema-1.json");
103 policy = new PolicyType("type2", schema);
104 types.put("type2", policy);
106 schema = getStringFromFile("demo-policy-schema-2.json");
107 policy = new PolicyType("type3", schema);
108 types.put("type3", policy);
110 schema = getStringFromFile("demo-policy-schema-3.json");
111 policy = new PolicyType("type4", schema);
112 types.put("type4", policy);
114 putInstance("ANR", "ANR-1", getStringFromFile("anr-policy-instance.json"));
115 } catch (Exception e) {
120 private String getStringFromFile(String path) {
122 InputStream inputStream = MethodHandles.lookup().lookupClass().getClassLoader()
123 .getResourceAsStream(path);
124 return new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n"));
125 } catch (Exception e) {
126 logger.error("Cannot read file :" + path, e);
131 String normalize(String str) {
132 return str.replace('\n', ' ');
135 private String getTimeStampUTC() {
136 return java.time.Instant.now().toString();
139 void putInstance(String typeId, String instanceId, String instanceData) {
140 PolicyInfo i = ImmutablePolicyInfo.builder().json(instanceData).lastModified(getTimeStampUTC())
141 .id(instanceId).ric("ricXX").service("service").type(typeId).build();
142 instances.put(instanceId, i);
145 public void deleteInstance(String instanceId) {
146 instances.remove(instanceId);
149 String getInstance(String id) throws RestClientException {
150 PolicyInfo i = instances.get(id);
152 throw new RestClientException("Type not found: " + id);
157 public Collection<PolicyType> getTypes() {
158 return types.values();
161 public List<PolicyInfo> getInstances(Optional<String> typeId) {
162 ArrayList<PolicyInfo> result = new ArrayList<>();
163 for (PolicyInfo i : instances.values()) {
164 if (typeId.isPresent()) {
165 if (i.type().equals(typeId.get())) {
176 private Map<String, PolicyType> types = new HashMap<>();
177 private Map<String, PolicyInfo> instances = new HashMap<>();