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=1636418106fac341ec69d5055337d4e4457871e7;hb=ba50f8809edc7d49a74021e25b4094f4c3174b26;hp=cdf614c2a31f671a925168bf8c70c3dad927603d;hpb=e2a037745508a3c1ada650ea3571ca57f0a90851;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 cdf614c2..16364181 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; @@ -31,10 +33,13 @@ import java.nio.file.Files; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; 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.MockA1ClientFactory; import org.slf4j.Logger; @@ -46,6 +51,7 @@ 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.junit.jupiter.SpringExtension; +import org.springframework.util.StringUtils; @ExtendWith(SpringExtension.class) @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT) @@ -55,6 +61,12 @@ public class MockPolicyAgent { @Autowired Rics rics; + @Autowired + Policies policies; + + @Autowired + PolicyTypes policyTypes; + static class MockApplicationConfig extends ApplicationConfig { @Override public String getLocalConfigurationFilePath() { @@ -101,10 +113,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 { @@ -127,25 +136,66 @@ public class MockPolicyAgent { } } + 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() throws InterruptedException { + 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 { + 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) 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 - @SuppressWarnings("squid:S2699") // Tests should include assertions. This test is only for keeping the server - // alive, + @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. public void runMock() throws Exception { keepServerAlive();