X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=policy-agent%2Fsrc%2Ftest%2Fjava%2Forg%2Foransc%2Fpolicyagent%2FMockPolicyAgent.java;h=f42a631f81bba16dd08974293e118d4520f665d1;hb=6a39814272307d0207222c9229b0d765ac062bf0;hp=ecb4661ed61f0a31f61ac0848a7d65b52d688e4c;hpb=fb4bc7967a4733d10775351440a3af14327d5f20;p=nonrtric.git diff --git a/policy-agent/src/test/java/org/oransc/policyagent/MockPolicyAgent.java b/policy-agent/src/test/java/org/oransc/policyagent/MockPolicyAgent.java index ecb4661e..f42a631f 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/MockPolicyAgent.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/MockPolicyAgent.java @@ -20,6 +20,8 @@ package org.oransc.policyagent; +import static org.awaitility.Awaitility.await; + import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -30,37 +32,59 @@ import java.nio.file.Files; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.oransc.policyagent.clients.A1Client; import org.oransc.policyagent.configuration.ApplicationConfig; +import org.oransc.policyagent.repository.ImmutablePolicy; import org.oransc.policyagent.repository.ImmutablePolicyType; import org.oransc.policyagent.repository.Policies; +import org.oransc.policyagent.repository.Policy; import org.oransc.policyagent.repository.PolicyType; import org.oransc.policyagent.repository.PolicyTypes; +import org.oransc.policyagent.repository.Ric; import org.oransc.policyagent.repository.Rics; -import org.oransc.policyagent.utils.MockA1Client; +import org.oransc.policyagent.utils.MockA1ClientFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.context.annotation.Bean; +import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.util.StringUtils; @ExtendWith(SpringExtension.class) @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT) -public class MockPolicyAgent { +@TestPropertySource( + properties = { // + "server.ssl.key-store=./config/keystore.jks", // + "app.webclient.trust-store=./config/truststore.jks"}) +class MockPolicyAgent { + private static final Logger logger = LoggerFactory.getLogger(MockPolicyAgent.class); - static class MockApplicationConfig extends ApplicationConfig { + @Autowired + Rics rics; + + @Autowired + Policies policies; + + @Autowired + PolicyTypes policyTypes; + @Autowired + ApplicationConfig applicationConfig; + + static class MockApplicationConfig extends ApplicationConfig { @Override - protected String getLocalConfigurationFilePath() { + public String getLocalConfigurationFilePath() { URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json"); return url.getFile(); } - } /** - * overrides the BeanFactory + * Overrides the BeanFactory. */ @TestConfiguration static class TestBeanFactory { @@ -75,11 +99,10 @@ public class MockPolicyAgent { } @Bean - public A1Client getA1Client() { + public MockA1ClientFactory getA1ClientFactory() { PolicyTypes ricTypes = new PolicyTypes(); loadTypes(ricTypes); - A1Client client = new MockA1Client(ricTypes); - return client; + return new MockA1ClientFactory(ricTypes); } @Bean @@ -98,10 +121,7 @@ public class MockPolicyAgent { } private static File[] getResourceFolderFiles(String folder) { - ClassLoader loader = Thread.currentThread().getContextClassLoader(); - URL url = loader.getResource(folder); - String path = url.getPath(); - return new File(path).listFiles(); + return getFile(folder).listFiles(); } private static String readFile(File file) throws IOException { @@ -117,35 +137,75 @@ public class MockPolicyAgent { PolicyType type = ImmutablePolicyType.builder().name(typeName).schema(schema).build(); policyTypes.put(type); } catch (Exception e) { - System.out.println("Could not load json schema " + e); + logger.error("Could not load json schema ", e); } } + policyTypes.put(ImmutablePolicyType.builder().name("").schema("{}").build()); } + } + private static File getFile(String path) { + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + URL url = loader.getResource(path); + return new File(url.getPath()); } @LocalServerPort private int port; - private void keepServerAlive() { - System.out.println("Keeping server alive!"); + private void keepServerAlive() throws InterruptedException, IOException { + waitForConfigurationToBeLoaded(); + loadInstances(); + logger.info("Keeping server alive!"); + synchronized (this) { + this.wait(); + } + } + + private void waitForConfigurationToBeLoaded() throws IOException { + String json = getConfigJsonFromFile(); try { - synchronized (this) { - this.wait(); - } - } catch (Exception ex) { - System.out.println("Unexpected: " + ex.toString()); + int noOfRicsInConfigFile = StringUtils.countOccurrencesOf(json, "baseUrl"); + await().until(() -> rics.size() == noOfRicsInConfigFile); + } catch (Exception e) { + logger.info("Loaded rics: {}, and no of rics in config file: {} never matched!", rics.size(), + StringUtils.countOccurrencesOf(json, "baseUrl")); } } private static String title(String jsonSchema) { - JsonObject parsedSchema = (JsonObject) new JsonParser().parse(jsonSchema); + JsonObject parsedSchema = (JsonObject) JsonParser.parseString(jsonSchema); String title = parsedSchema.get("title").getAsString(); return title; } + private void loadInstances() throws IOException { + PolicyType unnamedPolicyType = policyTypes.get(""); + Ric ric = rics.get("ric1"); + String json = getConfigJsonFromFile(); + + Policy policy = ImmutablePolicy.builder() // + .id("typelessPolicy") // + .json(json) // + .ownerServiceName("MockPolicyAgent") // + .ric(ric) // + .type(unnamedPolicyType) // + .lastModified("now") // + .isTransient(false) // + .build(); + this.policies.put(policy); + } + + private String getConfigJsonFromFile() throws IOException { + File jsonFile = getFile("test_application_configuration.json"); + String json = new String(Files.readAllBytes(jsonFile.toPath())); + return json; + } + @Test - public void runMock() throws Exception { + @SuppressWarnings("squid:S2699") // Tests should include assertions. This test is only for keeping the server + // alive, so it will only be confusing to add an assertion. + void runMock() throws Exception { keepServerAlive(); }