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.PolicyType;
39 import org.oransc.policyagent.repository.PolicyTypes;
40 import org.oransc.policyagent.repository.Rics;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.boot.test.context.SpringBootTest;
43 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
44 import org.springframework.boot.test.context.TestConfiguration;
45 import org.springframework.boot.web.server.LocalServerPort;
46 import org.springframework.context.annotation.Bean;
47 import org.springframework.test.context.junit4.SpringRunner;
49 import reactor.core.publisher.Flux;
50 import reactor.core.publisher.Mono;
52 @RunWith(SpringRunner.class)
53 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
54 public class MockPolicyAgent {
60 private Policies policies;
63 private PolicyTypes policyTypes;
65 static class MockApplicationConfig extends ApplicationConfig {
67 public void initialize() {
68 URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
69 loadConfigurationFromFile(url.getFile());
73 static class A1ClientMock implements A1Client {
74 private final Policies policies;
75 private final PolicyTypes policyTypes;
77 A1ClientMock(Policies policies, PolicyTypes policyTypes) {
78 this.policies = policies;
79 this.policyTypes = policyTypes;
83 public Flux<String> getAllPolicyTypes(String nearRtRicUrl) {
84 Vector<String> result = new Vector<>();
85 for (PolicyType p : this.policyTypes.getAll()) {
88 return Flux.fromIterable(result);
92 public Flux<String> getPoliciesForType(String nearRtRicUrl, String policyTypeId) {
97 public Mono<String> getPolicy(String nearRtRicUrl, String policyId) {
99 return Mono.just(this.policies.get(policyId).json());
100 } catch (Exception e) {
101 return Mono.error(e);
106 public Mono<String> putPolicy(String nearRtRicUrl, String policyId, String policyString) {
107 return Mono.just("OK");
111 public Mono<Void> deletePolicy(String nearRtRicUrl, String policyId) {
112 return Mono.error(new Exception("TODO We cannot use Void like this")); // TODO We cannot use Void like this
118 * overrides the BeanFactory
121 static class TestBeanFactory {
123 private final Rics rics = new Rics();
124 private final Policies policies = new Policies();
125 private final PolicyTypes policyTypes = new PolicyTypes();
128 public ApplicationConfig getApplicationConfig() {
129 return new MockApplicationConfig();
133 A1Client getA1Client() {
134 return new A1ClientMock(this.policies, this.policyTypes);
138 public Policies getPolicies() {
139 return this.policies;
143 public PolicyTypes getPolicyTypes() {
144 return this.policyTypes;
148 public Rics getRics() {
157 public void keepServerAlive() {
158 System.out.println("Keeping server alive!");
160 synchronized (this) {
163 } catch (Exception ex) {
164 System.out.println("Unexpected: " + ex.toString());
168 private static File[] getResourceFolderFiles(String folder) {
169 ClassLoader loader = Thread.currentThread().getContextClassLoader();
170 URL url = loader.getResource(folder);
171 String path = url.getPath();
172 return new File(path).listFiles();
175 private static String readFile(File file) throws IOException {
176 return new String(Files.readAllBytes(file.toPath()));
179 private static String title(String jsonSchema) {
180 JsonObject parsedSchema = (JsonObject) new JsonParser().parse(jsonSchema);
181 String title = parsedSchema.get("title").getAsString();
185 private static void loadTypes(PolicyTypes policyTypes) {
186 File[] files = getResourceFolderFiles("policy_types/");
187 for (File file : files) {
189 String schema = readFile(file);
190 String typeName = title(schema);
191 PolicyType type = ImmutablePolicyType.builder().name(typeName).jsonSchema(schema).build();
192 policyTypes.put(type);
193 } catch (Exception e) {
194 System.out.println("Could not load json schema " + e);
200 public void runMock() throws Exception {
201 loadTypes(this.policyTypes);