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===================================
21 package org.oransc.policyagent;
23 import com.google.gson.JsonObject;
24 import com.google.gson.JsonParser;
27 import java.io.IOException;
29 import java.nio.file.Files;
30 import java.util.HashMap;
32 import java.util.Vector;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.oransc.policyagent.clients.A1Client;
37 import org.oransc.policyagent.configuration.ApplicationConfig;
38 import org.oransc.policyagent.repository.ImmutablePolicyType;
39 import org.oransc.policyagent.repository.Policies;
40 import org.oransc.policyagent.repository.PolicyType;
41 import org.oransc.policyagent.repository.PolicyTypes;
42 import org.oransc.policyagent.repository.Rics;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.boot.test.context.SpringBootTest;
45 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
46 import org.springframework.boot.test.context.TestConfiguration;
47 import org.springframework.boot.web.server.LocalServerPort;
48 import org.springframework.context.annotation.Bean;
49 import org.springframework.test.context.junit4.SpringRunner;
51 import reactor.core.publisher.Flux;
52 import reactor.core.publisher.Mono;
54 @RunWith(SpringRunner.class)
55 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
56 public class MockPolicyAgent {
62 private Policies policies;
65 private PolicyTypes policyTypes;
67 static class MockApplicationConfig extends ApplicationConfig {
70 public void initialize() {
71 URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
72 loadConfigurationFromFile(url.getFile());
76 private static class RicPolicyDatabase {
77 private Map<String, Map<String, String>> policies = new HashMap<>();
79 public void putPolicy(String nearRtRicUrl, String policyId, String policyString) {
80 getPolicies(nearRtRicUrl).put(policyId, policyString);
83 public Iterable<String> getPolicyIdentities(String nearRtRicUrl) {
84 return getPolicies(nearRtRicUrl).keySet();
87 public void deletePolicy(String nearRtRicUrl, String policyId) {
88 getPolicies(nearRtRicUrl).remove(policyId);
91 private Map<String, String> getPolicies(String nearRtRicUrl) {
92 if (!policies.containsKey(nearRtRicUrl)) {
93 policies.put(nearRtRicUrl, new HashMap<>());
95 return policies.get(nearRtRicUrl);
99 static class A1ClientMock implements A1Client {
101 private final RicPolicyDatabase policies = new RicPolicyDatabase();
102 private final PolicyTypes policyTypes = new PolicyTypes();
105 loadTypes(this.policyTypes);
109 public Flux<String> getPolicyTypeIdentities(String nearRtRicUrl) {
110 Vector<String> result = new Vector<>();
111 for (PolicyType p : this.policyTypes.getAll()) {
112 result.add(p.name());
114 return Flux.fromIterable(result);
118 public Flux<String> getPolicyIdentities(String nearRtRicUrl) {
119 Iterable<String> result = policies.getPolicyIdentities(nearRtRicUrl);
120 return Flux.fromIterable(result);
124 public Mono<String> getPolicyType(String nearRtRicUrl, String policyTypeId) {
126 return Mono.just(this.policyTypes.getType(policyTypeId).schema());
127 } catch (Exception e) {
128 return Mono.error(e);
133 public Mono<String> putPolicy(String nearRtRicUrl, String policyId, String policyString) {
134 policies.putPolicy(nearRtRicUrl, policyId, policyString);
135 return Mono.just("OK");
139 public Mono<String> deletePolicy(String nearRtRicUrl, String policyId) {
140 policies.deletePolicy(nearRtRicUrl, policyId);
141 return Mono.just("OK");
144 private static File[] getResourceFolderFiles(String folder) {
145 ClassLoader loader = Thread.currentThread().getContextClassLoader();
146 URL url = loader.getResource(folder);
147 String path = url.getPath();
148 return new File(path).listFiles();
151 private static String readFile(File file) throws IOException {
152 return new String(Files.readAllBytes(file.toPath()));
155 private void loadTypes(PolicyTypes policyTypes) {
156 File[] files = getResourceFolderFiles("policy_types/");
157 for (File file : files) {
159 String schema = readFile(file);
160 String typeName = title(schema);
161 PolicyType type = ImmutablePolicyType.builder().name(typeName).schema(schema).build();
162 policyTypes.put(type);
163 } catch (Exception e) {
164 System.out.println("Could not load json schema " + e);
171 * overrides the BeanFactory
174 static class TestBeanFactory {
176 private final Rics rics = new Rics();
177 private final Policies policies = new Policies();
178 private final PolicyTypes policyTypes = new PolicyTypes();
181 public ApplicationConfig getApplicationConfig() {
182 return new MockApplicationConfig();
186 A1Client getA1Client() {
187 return new A1ClientMock();
191 public Policies getPolicies() {
192 return this.policies;
196 public PolicyTypes getPolicyTypes() {
197 return this.policyTypes;
201 public Rics getRics() {
209 private void keepServerAlive() {
210 System.out.println("Keeping server alive!");
212 synchronized (this) {
215 } catch (Exception ex) {
216 System.out.println("Unexpected: " + ex.toString());
220 private static String title(String jsonSchema) {
221 JsonObject parsedSchema = (JsonObject) new JsonParser().parse(jsonSchema);
222 String title = parsedSchema.get("title").getAsString();
227 public void runMock() throws Exception {