Modify method calls in A1 client
[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
21 package org.oransc.policyagent;
22
23 import com.google.gson.JsonObject;
24 import com.google.gson.JsonParser;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.net.URL;
29 import java.nio.file.Files;
30 import java.util.Vector;
31
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;
49
50 import reactor.core.publisher.Flux;
51 import reactor.core.publisher.Mono;
52
53 @RunWith(SpringRunner.class)
54 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
55 public class MockPolicyAgent {
56
57     @Autowired
58     private Rics rics;
59
60     @Autowired
61     private Policies policies;
62
63     @Autowired
64     private PolicyTypes policyTypes;
65
66     static class MockApplicationConfig extends ApplicationConfig {
67         @Override
68         public void initialize() {
69             URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
70             loadConfigurationFromFile(url.getFile());
71         }
72     }
73
74     static class A1ClientMock implements A1Client {
75         private final Policies policies;
76         private final PolicyTypes policyTypes;
77
78         A1ClientMock(Policies policies, PolicyTypes policyTypes) {
79             this.policies = policies;
80             this.policyTypes = policyTypes;
81         }
82
83         @Override
84         public Flux<String> getPolicyTypeIdentities(String nearRtRicUrl) {
85             Vector<String> result = new Vector<>();
86             for (PolicyType p : this.policyTypes.getAll()) {
87                 result.add(p.name());
88             }
89             return Flux.fromIterable(result);
90         }
91
92         @Override
93         public Flux<String> getPolicyIdentities(String nearRtRicUrl) {
94             Vector<String> result = new Vector<>();
95             for (Policy p : this.policies.getAll()) {
96                 result.add(p.id());
97             }
98             return Flux.fromIterable(result);
99         }
100
101         @Override
102         public Mono<String> getPolicyType(String nearRtRicUrl, String policyTypeId) {
103             try {
104                 return Mono.just(this.policyTypes.getType(policyTypeId).jsonSchema());
105             } catch (Exception e) {
106                 return Mono.error(e);
107             }
108         }
109
110         @Override
111         public Mono<String> putPolicy(String nearRtRicUrl, String policyId, String policyString) {
112             return Mono.just("OK");
113         }
114
115         @Override
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
118         }
119
120     }
121
122     /**
123      * overrides the BeanFactory
124      */
125     @TestConfiguration
126     static class TestBeanFactory {
127
128         private final Rics rics = new Rics();
129         private final Policies policies = new Policies();
130         private final PolicyTypes policyTypes = new PolicyTypes();
131
132         @Bean
133         public ApplicationConfig getApplicationConfig() {
134             return new MockApplicationConfig();
135         }
136
137         @Bean
138         A1Client getA1Client() {
139             return new A1ClientMock(this.policies, this.policyTypes);
140         }
141
142         @Bean
143         public Policies getPolicies() {
144             return this.policies;
145         }
146
147         @Bean
148         public PolicyTypes getPolicyTypes() {
149             return this.policyTypes;
150         }
151
152         @Bean
153         public Rics getRics() {
154             return this.rics;
155         }
156
157     }
158
159     @LocalServerPort
160     private int port;
161
162     public void keepServerAlive() {
163         System.out.println("Keeping server alive!");
164         try {
165             synchronized (this) {
166                 this.wait();
167             }
168         } catch (Exception ex) {
169             System.out.println("Unexpected: " + ex.toString());
170         }
171     }
172
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();
178     }
179
180     private static String readFile(File file) throws IOException {
181         return new String(Files.readAllBytes(file.toPath()));
182     }
183
184     private static String title(String jsonSchema) {
185         JsonObject parsedSchema = (JsonObject) new JsonParser().parse(jsonSchema);
186         String title = parsedSchema.get("title").getAsString();
187         return title;
188     }
189
190     private static void loadTypes(PolicyTypes policyTypes) {
191         File[] files = getResourceFolderFiles("policy_types/");
192         for (File file : files) {
193             try {
194                 String schema = readFile(file);
195                 String typeName = title(schema);
196                 PolicyType type = ImmutablePolicyType.builder().name(typeName).jsonSchema(schema).build();
197                 policyTypes.put(type);
198             } catch (Exception e) {
199                 System.out.println("Could not load json schema " + e);
200             }
201         }
202     }
203
204     @Test
205     public void runMock() throws Exception {
206         loadTypes(this.policyTypes);
207         keepServerAlive();
208     }
209
210 }