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.policyagent;
22 import com.google.gson.JsonObject;
23 import com.google.gson.JsonParser;
26 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.util.Vector;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.oransc.policyagent.clients.A1Client;
34 import org.oransc.policyagent.configuration.ApplicationConfig;
35 import org.oransc.policyagent.repository.ImmutablePolicyType;
36 import org.oransc.policyagent.repository.Policies;
37 import org.oransc.policyagent.repository.PolicyType;
38 import org.oransc.policyagent.repository.PolicyTypes;
39 import org.oransc.policyagent.repository.Rics;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.boot.test.context.SpringBootTest;
42 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
43 import org.springframework.boot.test.context.TestConfiguration;
44 import org.springframework.boot.web.server.LocalServerPort;
45 import org.springframework.context.annotation.Bean;
46 import org.springframework.test.context.junit4.SpringRunner;
48 import reactor.core.publisher.Flux;
49 import reactor.core.publisher.Mono;
51 @RunWith(SpringRunner.class)
52 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
53 public class MockPolicyAgent {
59 private Policies policies;
62 private PolicyTypes policyTypes;
64 static class MockApplicationConfig extends ApplicationConfig {
66 public void initialize() {
67 URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
68 loadConfigurationFromFile(url.getFile());
72 static class A1ClientMock implements A1Client {
73 private final Policies policies;
74 private final PolicyTypes policyTypes;
76 A1ClientMock(Policies policies, PolicyTypes policyTypes) {
77 this.policies = policies;
78 this.policyTypes = policyTypes;
82 public Flux<String> getAllPolicyTypes(String nearRtRicUrl) {
83 Vector<String> result = new Vector<>();
84 for (PolicyType p : this.policyTypes.getAll()) {
87 return Flux.fromIterable(result);
91 public Flux<String> getPoliciesForType(String nearRtRicUrl, String policyTypeId) {
96 public Mono<String> getPolicy(String nearRtRicUrl, String policyId) {
98 return Mono.just(this.policies.get(policyId).json());
99 } catch (Exception e) {
100 return Mono.error(e);
105 public Mono<String> putPolicy(String nearRtRicUrl, String policyId, String policyString) {
106 return Mono.just("OK");
110 public Mono<Void> deletePolicy(String nearRtRicUrl, String policyId) {
111 return Mono.error(new Exception("TODO We cannot use Void like this")); // TODO We cannot use Void like this
117 * overrides the BeanFactory
120 static class TestBeanFactory {
122 private final Rics rics = new Rics();
123 private final Policies policies = new Policies();
124 private final PolicyTypes policyTypes = new PolicyTypes();
127 public ApplicationConfig getApplicationConfig() {
128 return new MockApplicationConfig();
132 A1Client getA1Client() {
133 return new A1ClientMock(this.policies, this.policyTypes);
137 public Policies getPolicies() {
138 return this.policies;
142 public PolicyTypes getPolicyTypes() {
143 return this.policyTypes;
147 public Rics getRics() {
156 public void keepServerAlive() {
157 System.out.println("Keeping server alive!");
159 synchronized (this) {
162 } catch (Exception ex) {
163 System.out.println("Unexpected: " + ex.toString());
167 private static File[] getResourceFolderFiles(String folder) {
168 ClassLoader loader = Thread.currentThread().getContextClassLoader();
169 URL url = loader.getResource(folder);
170 String path = url.getPath();
171 return new File(path).listFiles();
174 private static String readFile(File file) throws IOException {
175 return new String(Files.readAllBytes(file.toPath()));
178 private static String title(String jsonSchema) {
179 JsonObject parsedSchema = (JsonObject) new JsonParser().parse(jsonSchema);
180 String title = parsedSchema.get("title").getAsString();
184 private static void loadTypes(PolicyTypes policyTypes) {
185 File[] files = getResourceFolderFiles("policy_types/");
186 for (File file : files) {
188 String schema = readFile(file);
189 String typeName = title(schema);
190 PolicyType type = ImmutablePolicyType.builder().name(typeName).jsonSchema(schema).build();
191 policyTypes.put(type);
192 } catch (Exception e) {
193 System.out.println("Could not load json schema " + e);
199 public void runMock() throws Exception {
200 loadTypes(this.policyTypes);