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