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.Vector;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.oransc.policyagent.clients.A1Client;
35 import org.oransc.policyagent.configuration.ApplicationConfig;
36 import org.oransc.policyagent.repository.ImmutablePolicyType;
37 import org.oransc.policyagent.repository.Policies;
38 import org.oransc.policyagent.repository.Policy;
39 import org.oransc.policyagent.repository.PolicyType;
40 import org.oransc.policyagent.repository.PolicyTypes;
41 import org.oransc.policyagent.repository.Rics;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.boot.test.context.SpringBootTest;
44 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
45 import org.springframework.boot.test.context.TestConfiguration;
46 import org.springframework.boot.web.server.LocalServerPort;
47 import org.springframework.context.annotation.Bean;
48 import org.springframework.test.context.junit4.SpringRunner;
50 import reactor.core.publisher.Flux;
51 import reactor.core.publisher.Mono;
53 @RunWith(SpringRunner.class)
54 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
55 public class MockPolicyAgent {
61 private Policies policies;
64 private PolicyTypes policyTypes;
66 static class MockApplicationConfig extends ApplicationConfig {
68 public void initialize() {
69 URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
70 loadConfigurationFromFile(url.getFile());
74 static class A1ClientMock implements A1Client {
75 private final Policies policies;
76 private final PolicyTypes policyTypes;
78 A1ClientMock(Policies policies, PolicyTypes policyTypes) {
79 this.policies = policies;
80 this.policyTypes = policyTypes;
84 public Flux<String> getPolicyTypeIdentities(String nearRtRicUrl) {
85 Vector<String> result = new Vector<>();
86 for (PolicyType p : this.policyTypes.getAll()) {
89 return Flux.fromIterable(result);
93 public Flux<String> getPolicyIdentities(String nearRtRicUrl) {
94 Vector<String> result = new Vector<>();
95 for (Policy p : this.policies.getAll()) {
98 return Flux.fromIterable(result);
102 public Mono<String> getPolicyType(String nearRtRicUrl, String policyTypeId) {
104 return Mono.just(this.policyTypes.getType(policyTypeId).toString());
105 } catch (Exception e) {
106 return Mono.error(e);
111 public Mono<String> putPolicy(String nearRtRicUrl, String policyId, String policyString) {
112 return Mono.just("OK");
116 public Mono<Void> deletePolicy(String nearRtRicUrl, String policyId) {
117 return Mono.error(new Exception("TODO We cannot use Void like this")); // TODO We cannot use Void like this
123 * overrides the BeanFactory
126 static class TestBeanFactory {
128 private final Rics rics = new Rics();
129 private final Policies policies = new Policies();
130 private final PolicyTypes policyTypes = new PolicyTypes();
133 public ApplicationConfig getApplicationConfig() {
134 return new MockApplicationConfig();
138 A1Client getA1Client() {
139 return new A1ClientMock(this.policies, this.policyTypes);
143 public Policies getPolicies() {
144 return this.policies;
148 public PolicyTypes getPolicyTypes() {
149 return this.policyTypes;
153 public Rics getRics() {
162 public void keepServerAlive() {
163 System.out.println("Keeping server alive!");
165 synchronized (this) {
168 } catch (Exception ex) {
169 System.out.println("Unexpected: " + ex.toString());
173 private static File[] getResourceFolderFiles(String folder) {
174 ClassLoader loader = Thread.currentThread().getContextClassLoader();
175 URL url = loader.getResource(folder);
176 String path = url.getPath();
177 return new File(path).listFiles();
180 private static String readFile(File file) throws IOException {
181 return new String(Files.readAllBytes(file.toPath()));
184 private static String title(String jsonSchema) {
185 JsonObject parsedSchema = (JsonObject) new JsonParser().parse(jsonSchema);
186 String title = parsedSchema.get("title").getAsString();
190 private static void loadTypes(PolicyTypes policyTypes) {
191 File[] files = getResourceFolderFiles("policy_types/");
192 for (File file : files) {
194 String schema = readFile(file);
195 String typeName = title(schema);
196 PolicyType type = ImmutablePolicyType.builder().name(typeName).build();
197 policyTypes.put(type);
198 } catch (Exception e) {
199 System.out.println("Could not load json schema " + e);
205 public void runMock() throws Exception {
206 loadTypes(this.policyTypes);