b7ea7dd1cc2af01de2de15199bcc46234e5c1901
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / MockPolicyAgent.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20 package org.oransc.policyagent;
21
22 import com.google.gson.JsonObject;
23 import com.google.gson.JsonParser;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.net.URL;
28 import java.nio.file.Files;
29 import java.util.Vector;
30
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;
47
48 import reactor.core.publisher.Flux;
49 import reactor.core.publisher.Mono;
50
51 @RunWith(SpringRunner.class)
52 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
53 public class MockPolicyAgent {
54
55     @Autowired
56     private Rics rics;
57
58     @Autowired
59     private Policies policies;
60
61     @Autowired
62     private PolicyTypes policyTypes;
63
64     static class MockApplicationConfig extends ApplicationConfig {
65         @Override
66         public void initialize() {
67             URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
68             loadConfigurationFromFile(url.getFile());
69         }
70     }
71
72     static class A1ClientMock implements A1Client {
73         private final Policies policies;
74         private final PolicyTypes policyTypes;
75
76         A1ClientMock(Policies policies, PolicyTypes policyTypes) {
77             this.policies = policies;
78             this.policyTypes = policyTypes;
79         }
80
81         @Override
82         public Flux<String> getAllPolicyTypes(String nearRtRicUrl) {
83             Vector<String> result = new Vector<>();
84             for (PolicyType p : this.policyTypes.getAll()) {
85                 result.add(p.name());
86             }
87             return Flux.fromIterable(result);
88         }
89
90         @Override
91         public Flux<String> getPoliciesForType(String nearRtRicUrl, String policyTypeId) {
92             return Flux.empty();
93         }
94
95         @Override
96         public Mono<String> getPolicy(String nearRtRicUrl, String policyId) {
97             try {
98                 return Mono.just(this.policies.get(policyId).json());
99             } catch (Exception e) {
100                 return Mono.error(e);
101             }
102         }
103
104         @Override
105         public Mono<String> putPolicy(String nearRtRicUrl, String policyId, String policyString) {
106             return Mono.just("OK");
107         }
108
109         @Override
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
112         }
113
114     }
115
116     /**
117      * overrides the BeanFactory
118      */
119     @TestConfiguration
120     static class TestBeanFactory {
121
122         private final Rics rics = new Rics();
123         private final Policies policies = new Policies();
124         private final PolicyTypes policyTypes = new PolicyTypes();
125
126         @Bean
127         public ApplicationConfig getApplicationConfig() {
128             return new MockApplicationConfig();
129         }
130
131         @Bean
132         A1Client getA1Client() {
133             return new A1ClientMock(this.policies, this.policyTypes);
134         }
135
136         @Bean
137         public Policies getPolicies() {
138             return this.policies;
139         }
140
141         @Bean
142         public PolicyTypes getPolicyTypes() {
143             return this.policyTypes;
144         }
145
146         @Bean
147         public Rics getRics() {
148             return this.rics;
149         }
150
151     }
152
153     @LocalServerPort
154     private int port;
155
156     public void keepServerAlive() {
157         System.out.println("Keeping server alive!");
158         try {
159             synchronized (this) {
160                 this.wait();
161             }
162         } catch (Exception ex) {
163             System.out.println("Unexpected: " + ex.toString());
164         }
165     }
166
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();
172     }
173
174     private static String readFile(File file) throws IOException {
175         return new String(Files.readAllBytes(file.toPath()));
176     }
177
178     private static String title(String jsonSchema) {
179         JsonObject parsedSchema = (JsonObject) new JsonParser().parse(jsonSchema);
180         String title = parsedSchema.get("title").getAsString();
181         return title;
182     }
183
184     private static void loadTypes(PolicyTypes policyTypes) {
185         File[] files = getResourceFolderFiles("policy_types/");
186         for (File file : files) {
187             try {
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);
194             }
195         }
196     }
197
198     @Test
199     public void runMock() throws Exception {
200         loadTypes(this.policyTypes);
201         keepServerAlive();
202     }
203
204 }