2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
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
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.onap.ccsdk.oran.a1policymanagementservice;
23 import static org.awaitility.Awaitility.await;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParser;
29 import java.io.IOException;
30 import java.lang.invoke.MethodHandles;
32 import java.nio.file.Files;
33 import java.time.Instant;
35 import org.junit.jupiter.api.Test;
36 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
37 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
38 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
39 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
40 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
41 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
42 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
43 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
49 import org.springframework.boot.test.context.TestConfiguration;
50 import org.springframework.boot.web.server.LocalServerPort;
51 import org.springframework.context.annotation.Bean;
52 import org.springframework.test.context.TestPropertySource;
53 import org.springframework.util.StringUtils;
55 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
56 @TestPropertySource(properties = { //
57 "server.ssl.key-store=./config/keystore.jks", //
58 "app.webclient.trust-store=./config/truststore.jks", //
59 "app.vardata-directory=./target", //
60 "app.config-file-schema-path=/application_configuration_schema.json"})
61 @SuppressWarnings("java:S3577") // Class name should start or end with Test. This is not a test class per se,
64 class MockPolicyManagementService {
65 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
74 PolicyTypes policyTypes;
77 ApplicationConfig applicationConfig;
79 static class MockApplicationConfig extends ApplicationConfig {
81 public String getLocalConfigurationFilePath() {
82 URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
88 * Overrides the BeanFactory.
91 static class TestBeanFactory {
93 private final ApplicationConfig applicationConfig = new MockApplicationConfig();
94 private final Rics rics = new Rics();
97 public ApplicationConfig getApplicationConfig() {
98 return new MockApplicationConfig();
102 public MockA1ClientFactory getA1ClientFactory(@Autowired ApplicationConfig appConfig) {
103 PolicyTypes ricTypes = new PolicyTypes(applicationConfig);
105 return new MockA1ClientFactory(appConfig, ricTypes);
109 public Rics getRics() {
113 private static File[] getResourceFolderFiles(String folder) {
114 return getFile(folder).listFiles();
117 private static String readFile(File file) throws IOException {
118 return new String(Files.readAllBytes(file.toPath()));
121 private void loadTypes(PolicyTypes policyTypes) {
122 File[] files = getResourceFolderFiles("policy_types/");
123 for (File file : files) {
125 String schema = readFile(file);
126 String typeName = title(schema);
127 PolicyType type = PolicyType.builder().id(typeName).schema(schema).build();
128 policyTypes.put(type);
129 } catch (Exception e) {
130 logger.error("Could not load json schema ", e);
133 policyTypes.put(PolicyType.builder().id("").schema("{}").build());
137 private static File getFile(String path) {
138 ClassLoader loader = Thread.currentThread().getContextClassLoader();
139 URL url = loader.getResource(path);
140 return new File(url.getPath());
146 private void keepServerAlive() throws InterruptedException, IOException {
147 waitForConfigurationToBeLoaded();
149 logger.info("Keeping server alive! Listening on port: {}", port);
150 synchronized (this) {
155 private void waitForConfigurationToBeLoaded() throws IOException {
156 String json = getConfigJsonFromFile();
158 int noOfRicsInConfigFile = StringUtils.countOccurrencesOf(json, "baseUrl");
159 await().until(() -> rics.size() == noOfRicsInConfigFile);
160 } catch (Exception e) {
161 logger.info("Loaded rics: {}, and no of rics in config file: {} never matched!", rics.size(),
162 StringUtils.countOccurrencesOf(json, "baseUrl"));
166 private static String title(String jsonSchema) {
167 JsonObject parsedSchema = (JsonObject) JsonParser.parseString(jsonSchema);
168 String title = parsedSchema.get("title").getAsString();
172 private void loadInstances() throws IOException {
173 PolicyType unnamedPolicyType = policyTypes.get("");
174 Ric ric = rics.get("ric1");
175 String json = getConfigJsonFromFile();
177 Policy policy = Policy.builder() //
178 .id("typelessPolicy") //
180 .ownerServiceId("MockPolicyManagementService") //
182 .type(unnamedPolicyType) //
183 .lastModified(Instant.now()) //
184 .isTransient(false) //
185 .statusNotificationUri("statusNotificationUri") //
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
198 // alive, so it will only be confusing to add an assertion.
199 void runMock() throws Exception {