Added one typeless Policy in MockPolicyAgent
[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.configuration.ApplicationConfig;
34 import org.oransc.policyagent.repository.ImmutablePolicy;
35 import org.oransc.policyagent.repository.ImmutablePolicyType;
36 import org.oransc.policyagent.repository.Policies;
37 import org.oransc.policyagent.repository.Policy;
38 import org.oransc.policyagent.repository.PolicyType;
39 import org.oransc.policyagent.repository.PolicyTypes;
40 import org.oransc.policyagent.repository.Ric;
41 import org.oransc.policyagent.repository.Rics;
42 import org.oransc.policyagent.utils.MockA1ClientFactory;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.boot.test.context.SpringBootTest;
47 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
48 import org.springframework.boot.test.context.TestConfiguration;
49 import org.springframework.boot.web.server.LocalServerPort;
50 import org.springframework.context.annotation.Bean;
51 import org.springframework.test.context.junit.jupiter.SpringExtension;
52
53 @ExtendWith(SpringExtension.class)
54 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
55 public class MockPolicyAgent {
56     private static final Logger logger = LoggerFactory.getLogger(MockPolicyAgent.class);
57
58     @Autowired
59     Rics rics;
60
61     @Autowired
62     Policies policies;
63
64     @Autowired
65     PolicyTypes policyTypes;
66
67     static class MockApplicationConfig extends ApplicationConfig {
68         @Override
69         public String getLocalConfigurationFilePath() {
70             URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
71             return url.getFile();
72         }
73     }
74
75     /**
76      * Overrides the BeanFactory.
77      */
78     @TestConfiguration
79     static class TestBeanFactory {
80
81         private final Rics rics = new Rics();
82         private final Policies policies = new Policies();
83         private final PolicyTypes policyTypes = new PolicyTypes();
84
85         @Bean
86         public ApplicationConfig getApplicationConfig() {
87             return new MockApplicationConfig();
88         }
89
90         @Bean
91         public MockA1ClientFactory getA1ClientFactory() {
92             PolicyTypes ricTypes = new PolicyTypes();
93             loadTypes(ricTypes);
94             return new MockA1ClientFactory(ricTypes);
95         }
96
97         @Bean
98         public Policies getPolicies() {
99             return this.policies;
100         }
101
102         @Bean
103         public PolicyTypes getPolicyTypes() {
104             return this.policyTypes;
105         }
106
107         @Bean
108         public Rics getRics() {
109             return this.rics;
110         }
111
112         private static File[] getResourceFolderFiles(String folder) {
113             return getFile(folder).listFiles();
114         }
115
116         private static String readFile(File file) throws IOException {
117             return new String(Files.readAllBytes(file.toPath()));
118         }
119
120         private void loadTypes(PolicyTypes policyTypes) {
121             File[] files = getResourceFolderFiles("policy_types/");
122             for (File file : files) {
123                 try {
124                     String schema = readFile(file);
125                     String typeName = title(schema);
126                     PolicyType type = ImmutablePolicyType.builder().name(typeName).schema(schema).build();
127                     policyTypes.put(type);
128                 } catch (Exception e) {
129                     logger.error("Could not load json schema ", e);
130                 }
131             }
132             policyTypes.put(ImmutablePolicyType.builder().name("").schema("{}").build());
133         }
134     }
135
136     private static File getFile(String path) {
137         ClassLoader loader = Thread.currentThread().getContextClassLoader();
138         URL url = loader.getResource(path);
139         return new File(url.getPath());
140     }
141
142     @LocalServerPort
143     private int port;
144
145     private void keepServerAlive() throws InterruptedException, IOException {
146         logger.info("Keeping server alive!");
147         Thread.sleep(1000);
148         loadInstances();
149         synchronized (this) {
150             this.wait();
151         }
152     }
153
154     private static String title(String jsonSchema) {
155         JsonObject parsedSchema = (JsonObject) JsonParser.parseString(jsonSchema);
156         String title = parsedSchema.get("title").getAsString();
157         return title;
158     }
159
160     private void loadInstances() throws IOException {
161         PolicyType unnamedPolicyType = policyTypes.get("");
162         Ric ric = rics.get("ric1");
163         File jsonFile = getFile("test_application_configuration.json");
164         String json = new String(Files.readAllBytes(jsonFile.toPath()));
165
166         Policy policy = ImmutablePolicy.builder() //
167             .id("typelessPolicy") //
168             .json(json) //
169             .ownerServiceName("MockPolicyAgent") //
170             .ric(ric) //
171             .type(unnamedPolicyType) //
172             .lastModified("now") //
173             .build();
174         this.policies.put(policy);
175     }
176
177     @Test
178     @SuppressWarnings("squid:S2699") // Tests should include assertions. This test is only for keeping the server
179                                      // alive,
180                                      // so it will only be confusing to add an assertion.
181     public void runMock() throws Exception {
182         keepServerAlive();
183     }
184
185 }