Merge "Remove unused exceptions from dashboard backend"
[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 import java.io.File;
26 import java.io.IOException;
27 import java.net.URL;
28 import java.nio.file.Files;
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.api.extension.ExtendWith;
31 import org.oransc.policyagent.configuration.ApplicationConfig;
32 import org.oransc.policyagent.repository.ImmutablePolicyType;
33 import org.oransc.policyagent.repository.Policies;
34 import org.oransc.policyagent.repository.PolicyType;
35 import org.oransc.policyagent.repository.PolicyTypes;
36 import org.oransc.policyagent.repository.Rics;
37 import org.oransc.policyagent.utils.MockA1ClientFactory;
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.junit.jupiter.SpringExtension;
45
46 @ExtendWith(SpringExtension.class)
47 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
48 public class MockPolicyAgent {
49
50     @Autowired
51     Rics rics;
52
53     static class MockApplicationConfig extends ApplicationConfig {
54         @Override
55         public String getLocalConfigurationFilePath() {
56             URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
57             return url.getFile();
58         }
59     }
60
61     /**
62      * Overrides the BeanFactory.
63      */
64     @TestConfiguration
65     static class TestBeanFactory {
66
67         private final Rics rics = new Rics();
68         private final Policies policies = new Policies();
69         private final PolicyTypes policyTypes = new PolicyTypes();
70
71         @Bean
72         public ApplicationConfig getApplicationConfig() {
73             return new MockApplicationConfig();
74         }
75
76         @Bean
77         public MockA1ClientFactory getA1ClientFactory() {
78             PolicyTypes ricTypes = new PolicyTypes();
79             loadTypes(ricTypes);
80             return new MockA1ClientFactory(ricTypes);
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     @LocalServerPort
125     private int port;
126
127     private void keepServerAlive() {
128         System.out.println("Keeping server alive!");
129         try {
130             synchronized (this) {
131                 this.wait();
132             }
133         } catch (Exception ex) {
134             System.out.println("Unexpected: " + ex.toString());
135         }
136     }
137
138     private static String title(String jsonSchema) {
139         JsonObject parsedSchema = (JsonObject) JsonParser.parseString(jsonSchema);
140         String title = parsedSchema.get("title").getAsString();
141         return title;
142     }
143
144     @Test
145     public void runMock() throws Exception {
146         keepServerAlive();
147     }
148
149 }