8b488d8bbb50328ca8282de89353e10af5cf7baa
[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
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.api.extension.ExtendWith;
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.oransc.policyagent.utils.MockA1Client;
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.junit.jupiter.SpringExtension;
47
48 @ExtendWith(SpringExtension.class)
49 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
50 public class MockPolicyAgent {
51
52     static class MockApplicationConfig extends ApplicationConfig {
53         @Override
54         protected String getLocalConfigurationFilePath() {
55             URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
56             return url.getFile();
57         }
58     }
59
60     /**
61      * overrides the BeanFactory
62      */
63     @TestConfiguration
64     static class TestBeanFactory {
65
66         private final Rics rics = new Rics();
67         private final Policies policies = new Policies();
68         private final PolicyTypes policyTypes = new PolicyTypes();
69
70         @Bean
71         public ApplicationConfig getApplicationConfig() {
72             return new MockApplicationConfig();
73         }
74
75         @Bean
76         public A1Client getA1Client() {
77             PolicyTypes ricTypes = new PolicyTypes();
78             loadTypes(ricTypes);
79             A1Client client = new MockA1Client(ricTypes);
80             return client;
81         }
82
83         @Bean
84         public Policies getPolicies() {
85             return this.policies;
86         }
87
88         @Bean
89         public PolicyTypes getPolicyTypes() {
90             return this.policyTypes;
91         }
92
93         @Bean
94         public Rics getRics() {
95             return this.rics;
96         }
97
98         private static File[] getResourceFolderFiles(String folder) {
99             ClassLoader loader = Thread.currentThread().getContextClassLoader();
100             URL url = loader.getResource(folder);
101             String path = url.getPath();
102             return new File(path).listFiles();
103         }
104
105         private static String readFile(File file) throws IOException {
106             return new String(Files.readAllBytes(file.toPath()));
107         }
108
109         private void loadTypes(PolicyTypes policyTypes) {
110             File[] files = getResourceFolderFiles("policy_types/");
111             for (File file : files) {
112                 try {
113                     String schema = readFile(file);
114                     String typeName = title(schema);
115                     PolicyType type = ImmutablePolicyType.builder().name(typeName).schema(schema).build();
116                     policyTypes.put(type);
117                 } catch (Exception e) {
118                     System.out.println("Could not load json schema " + e);
119                 }
120             }
121         }
122
123     }
124
125     @LocalServerPort
126     private int port;
127
128     private void keepServerAlive() {
129         System.out.println("Keeping server alive!");
130         try {
131             synchronized (this) {
132                 this.wait();
133             }
134         } catch (Exception ex) {
135             System.out.println("Unexpected: " + ex.toString());
136         }
137     }
138
139     private static String title(String jsonSchema) {
140         JsonObject parsedSchema = (JsonObject) new JsonParser().parse(jsonSchema);
141         String title = parsedSchema.get("title").getAsString();
142         return title;
143     }
144
145     @Test
146     public void runMock() throws Exception {
147         keepServerAlive();
148     }
149
150 }