Remove Sonar warnings
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / MockPolicyAgent.java
index 7bdd796..d37a2be 100644 (file)
@@ -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();
     }