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