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=d37a2be4eca701f7a61a56fd96c3c5ff4d688d02;hb=2310d1c6a458bd12b2d1ff805f1bd12dcd536cfa;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..d37a2be4 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,10 +51,11 @@ 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) -public class MockPolicyAgent { +class MockPolicyAgent { private static final Logger logger = LoggerFactory.getLogger(MockPolicyAgent.class); @Autowired @@ -64,6 +67,9 @@ public class MockPolicyAgent { @Autowired PolicyTypes policyTypes; + @Autowired + ApplicationConfig applicationConfig; + static class MockApplicationConfig extends ApplicationConfig { @Override public String getLocalConfigurationFilePath() { @@ -143,14 +149,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 +177,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,15 +186,21 @@ 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, - // so it will only be confusing to add an assertion. - public void runMock() throws Exception { + // alive, so it will only be confusing to add an assertion. + void runMock() throws Exception { keepServerAlive(); }