2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.policyagent;
23 import static org.awaitility.Awaitility.await;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParser;
29 import java.io.IOException;
31 import java.nio.file.Files;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.oransc.policyagent.configuration.ApplicationConfig;
36 import org.oransc.policyagent.repository.ImmutablePolicy;
37 import org.oransc.policyagent.repository.ImmutablePolicyType;
38 import org.oransc.policyagent.repository.Policies;
39 import org.oransc.policyagent.repository.Policy;
40 import org.oransc.policyagent.repository.PolicyType;
41 import org.oransc.policyagent.repository.PolicyTypes;
42 import org.oransc.policyagent.repository.Ric;
43 import org.oransc.policyagent.repository.Rics;
44 import org.oransc.policyagent.utils.MockA1ClientFactory;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.boot.test.context.SpringBootTest;
49 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
50 import org.springframework.boot.test.context.TestConfiguration;
51 import org.springframework.boot.web.server.LocalServerPort;
52 import org.springframework.context.annotation.Bean;
53 import org.springframework.test.context.junit.jupiter.SpringExtension;
54 import org.springframework.util.StringUtils;
56 @ExtendWith(SpringExtension.class)
57 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
58 public class MockPolicyAgent {
59 private static final Logger logger = LoggerFactory.getLogger(MockPolicyAgent.class);
68 PolicyTypes policyTypes;
70 static class MockApplicationConfig extends ApplicationConfig {
72 public String getLocalConfigurationFilePath() {
73 URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
79 * Overrides the BeanFactory.
82 static class TestBeanFactory {
84 private final Rics rics = new Rics();
85 private final Policies policies = new Policies();
86 private final PolicyTypes policyTypes = new PolicyTypes();
89 public ApplicationConfig getApplicationConfig() {
90 return new MockApplicationConfig();
94 public MockA1ClientFactory getA1ClientFactory() {
95 PolicyTypes ricTypes = new PolicyTypes();
97 return new MockA1ClientFactory(ricTypes);
101 public Policies getPolicies() {
102 return this.policies;
106 public PolicyTypes getPolicyTypes() {
107 return this.policyTypes;
111 public Rics getRics() {
115 private static File[] getResourceFolderFiles(String folder) {
116 return getFile(folder).listFiles();
119 private static String readFile(File file) throws IOException {
120 return new String(Files.readAllBytes(file.toPath()));
123 private void loadTypes(PolicyTypes policyTypes) {
124 File[] files = getResourceFolderFiles("policy_types/");
125 for (File file : files) {
127 String schema = readFile(file);
128 String typeName = title(schema);
129 PolicyType type = ImmutablePolicyType.builder().name(typeName).schema(schema).build();
130 policyTypes.put(type);
131 } catch (Exception e) {
132 logger.error("Could not load json schema ", e);
135 policyTypes.put(ImmutablePolicyType.builder().name("").schema("{}").build());
139 private static File getFile(String path) {
140 ClassLoader loader = Thread.currentThread().getContextClassLoader();
141 URL url = loader.getResource(path);
142 return new File(url.getPath());
148 private void keepServerAlive() throws InterruptedException, IOException {
149 waitForConfigurationToBeLoaded();
151 logger.info("Keeping server alive!");
152 synchronized (this) {
157 private void waitForConfigurationToBeLoaded() throws IOException {
158 String json = getConfigJsonFromFile();
160 int noOfRicsInConfigFile = StringUtils.countOccurrencesOf(json, "baseUrl");
161 await().until(() -> rics.size() == noOfRicsInConfigFile);
162 } catch (Exception e) {
163 logger.info("Loaded rics: {}, and no of rics in config file: {} never matched!", rics.size(),
164 StringUtils.countOccurrencesOf(json, "baseUrl"));
168 private static String title(String jsonSchema) {
169 JsonObject parsedSchema = (JsonObject) JsonParser.parseString(jsonSchema);
170 String title = parsedSchema.get("title").getAsString();
174 private void loadInstances() throws IOException {
175 PolicyType unnamedPolicyType = policyTypes.get("");
176 Ric ric = rics.get("ric1");
177 String json = getConfigJsonFromFile();
179 Policy policy = ImmutablePolicy.builder() //
180 .id("typelessPolicy") //
182 .ownerServiceName("MockPolicyAgent") //
184 .type(unnamedPolicyType) //
185 .lastModified("now") //
187 this.policies.put(policy);
190 private String getConfigJsonFromFile() throws IOException {
191 File jsonFile = getFile("test_application_configuration.json");
192 String json = new String(Files.readAllBytes(jsonFile.toPath()));
197 @SuppressWarnings("squid:S2699") // Tests should include assertions. This test is only for keeping the server alive,
198 // so it will only be confusing to add an assertion.
199 public void runMock() throws Exception {