Dashboard using policy agent NBI
[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
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.oransc.policyagent.configuration.ApplicationConfig;
33 import org.oransc.policyagent.repository.ImmutablePolicyType;
34 import org.oransc.policyagent.repository.Policies;
35 import org.oransc.policyagent.repository.PolicyType;
36 import org.oransc.policyagent.repository.PolicyTypes;
37 import org.oransc.policyagent.repository.Rics;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.boot.test.context.SpringBootTest;
40 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
41 import org.springframework.boot.test.context.TestConfiguration;
42 import org.springframework.boot.web.server.LocalServerPort;
43 import org.springframework.context.annotation.Bean;
44 import org.springframework.test.context.junit4.SpringRunner;
45
46 @RunWith(SpringRunner.class)
47 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
48 public class MockPolicyAgent {
49
50     @Autowired
51     private Rics rics;
52
53     @Autowired
54     private Policies policies;
55
56     @Autowired
57     private PolicyTypes policyTypes;
58
59     static class MockApplicationConfig extends ApplicationConfig {
60         @Override
61         public void initialize() {
62             URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
63             loadConfigurationFromFile(url.getFile());
64         }
65     }
66
67     /**
68      * overrides the BeanFactory
69      */
70     @TestConfiguration
71     static class TestBeanFactory {
72
73         @Bean
74         public ApplicationConfig getApplicationConfig() {
75             return new MockApplicationConfig();
76         }
77     }
78
79     @LocalServerPort
80     private int port;
81
82     public void keepServerAlive() {
83         System.out.println("Keeping server alive!");
84         try {
85             synchronized (this) {
86                 this.wait();
87             }
88         } catch (Exception ex) {
89             System.out.println("Unexpected: " + ex.toString());
90         }
91     }
92
93     private static File[] getResourceFolderFiles(String folder) {
94         ClassLoader loader = Thread.currentThread().getContextClassLoader();
95         URL url = loader.getResource(folder);
96         String path = url.getPath();
97         return new File(path).listFiles();
98     }
99
100     private static String readFile(File file) throws IOException {
101         return new String(Files.readAllBytes(file.toPath()));
102     }
103
104     private static String title(String jsonSchema) {
105         JsonObject parsedSchema = (JsonObject) new JsonParser().parse(jsonSchema);
106         String title = parsedSchema.get("title").getAsString();
107         return title;
108     }
109
110     private static void loadTypes(PolicyTypes policyTypes) {
111         File[] files = getResourceFolderFiles("policy_types/");
112         for (File file : files) {
113             try {
114                 String schema = readFile(file);
115                 String typeName = title(schema);
116                 PolicyType type = ImmutablePolicyType.builder().name(typeName).jsonSchema(schema).build();
117                 policyTypes.put(type);
118             } catch (Exception e) {
119                 System.out.println("Could not load json schema " + e);
120             }
121         }
122     }
123
124     @Test
125     public void runMock() throws Exception {
126         loadTypes(this.policyTypes);
127         keepServerAlive();
128     }
129
130 }