X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=policy-agent%2Fsrc%2Ftest%2Fjava%2Forg%2Foransc%2Fpolicyagent%2FMockPolicyAgent.java;h=1636418106fac341ec69d5055337d4e4457871e7;hb=4db5e7d262aaa8ccf18feaa4bd93a6a925801333;hp=7bdd79686de48e75aaea4bb92b9bed91cac73c17;hpb=3723b92ed3bda4f4741c155e2d65f1425ab24495;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 7bdd7968..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; @@ -49,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) @@ -143,14 +146,25 @@ public class MockPolicyAgent { private int port; private void keepServerAlive() throws InterruptedException, IOException { - logger.info("Keeping server alive!"); - Thread.sleep(1000); + 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(); @@ -160,8 +174,7 @@ public class MockPolicyAgent { private void loadInstances() throws IOException { PolicyType unnamedPolicyType = policyTypes.get(""); Ric ric = rics.get("ric1"); - File jsonFile = getFile("test_application_configuration.json"); - String json = new String(Files.readAllBytes(jsonFile.toPath())); + String json = getConfigJsonFromFile(); Policy policy = ImmutablePolicy.builder() // .id("typelessPolicy") // @@ -170,13 +183,19 @@ public class 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();