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.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.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.boot.test.context.SpringBootTest;
42 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
43 import org.springframework.boot.test.context.TestConfiguration;
44 import org.springframework.boot.web.server.LocalServerPort;
45 import org.springframework.context.annotation.Bean;
46 import org.springframework.test.context.junit.jupiter.SpringExtension;
48 @ExtendWith(SpringExtension.class)
49 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
50 public class MockPolicyAgent {
55 static class MockApplicationConfig extends ApplicationConfig {
57 public String getLocalConfigurationFilePath() {
58 URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
64 * Overrides the BeanFactory.
67 static class TestBeanFactory {
69 private final Rics rics = new Rics();
70 private final Policies policies = new Policies();
71 private final PolicyTypes policyTypes = new PolicyTypes();
74 public ApplicationConfig getApplicationConfig() {
75 return new MockApplicationConfig();
79 public MockA1ClientFactory getA1ClientFactory() {
80 PolicyTypes ricTypes = new PolicyTypes();
82 return new MockA1ClientFactory(ricTypes);
86 public Policies getPolicies() {
91 public PolicyTypes getPolicyTypes() {
92 return this.policyTypes;
96 public Rics getRics() {
100 private static File[] getResourceFolderFiles(String folder) {
101 ClassLoader loader = Thread.currentThread().getContextClassLoader();
102 URL url = loader.getResource(folder);
103 String path = url.getPath();
104 return new File(path).listFiles();
107 private static String readFile(File file) throws IOException {
108 return new String(Files.readAllBytes(file.toPath()));
111 private void loadTypes(PolicyTypes policyTypes) {
112 File[] files = getResourceFolderFiles("policy_types/");
113 for (File file : files) {
115 String schema = readFile(file);
116 String typeName = title(schema);
117 PolicyType type = ImmutablePolicyType.builder().name(typeName).schema(schema).build();
118 policyTypes.put(type);
119 } catch (Exception e) {
120 System.out.println("Could not load json schema " + e);
129 private void keepServerAlive() {
130 System.out.println("Keeping server alive!");
132 synchronized (this) {
135 } catch (Exception ex) {
136 System.out.println("Unexpected: " + ex.toString());
140 private static String title(String jsonSchema) {
141 JsonObject parsedSchema = (JsonObject) JsonParser.parseString(jsonSchema);
142 String title = parsedSchema.get("title").getAsString();
147 public void runMock() throws Exception {