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 com.google.gson.JsonObject;
24 import com.google.gson.JsonParser;
27 import java.io.IOException;
29 import java.nio.file.Files;
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;
53 @ExtendWith(SpringExtension.class)
54 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
55 public class MockPolicyAgent {
56 private static final Logger logger = LoggerFactory.getLogger(MockPolicyAgent.class);
65 PolicyTypes policyTypes;
67 static class MockApplicationConfig extends ApplicationConfig {
69 public String getLocalConfigurationFilePath() {
70 URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
76 * Overrides the BeanFactory.
79 static class TestBeanFactory {
81 private final Rics rics = new Rics();
82 private final Policies policies = new Policies();
83 private final PolicyTypes policyTypes = new PolicyTypes();
86 public ApplicationConfig getApplicationConfig() {
87 return new MockApplicationConfig();
91 public MockA1ClientFactory getA1ClientFactory() {
92 PolicyTypes ricTypes = new PolicyTypes();
94 return new MockA1ClientFactory(ricTypes);
98 public Policies getPolicies() {
103 public PolicyTypes getPolicyTypes() {
104 return this.policyTypes;
108 public Rics getRics() {
112 private static File[] getResourceFolderFiles(String folder) {
113 return getFile(folder).listFiles();
116 private static String readFile(File file) throws IOException {
117 return new String(Files.readAllBytes(file.toPath()));
120 private void loadTypes(PolicyTypes policyTypes) {
121 File[] files = getResourceFolderFiles("policy_types/");
122 for (File file : files) {
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);
132 policyTypes.put(ImmutablePolicyType.builder().name("").schema("{}").build());
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());
145 private void keepServerAlive() throws InterruptedException, IOException {
146 logger.info("Keeping server alive!");
149 synchronized (this) {
154 private static String title(String jsonSchema) {
155 JsonObject parsedSchema = (JsonObject) JsonParser.parseString(jsonSchema);
156 String title = parsedSchema.get("title").getAsString();
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()));
166 Policy policy = ImmutablePolicy.builder() //
167 .id("typelessPolicy") //
169 .ownerServiceName("MockPolicyAgent") //
171 .type(unnamedPolicyType) //
172 .lastModified("now") //
174 this.policies.put(policy);
178 @SuppressWarnings("squid:S2699") // Tests should include assertions. This test is only for keeping the server
180 // so it will only be confusing to add an assertion.
181 public void runMock() throws Exception {