package org.oransc.policyagent;
+import static org.awaitility.Awaitility.await;
+
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
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);
@Autowired
@Autowired
PolicyTypes policyTypes;
+ @Autowired
+ ApplicationConfig applicationConfig;
+
static class MockApplicationConfig extends ApplicationConfig {
@Override
public String getLocalConfigurationFilePath() {
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();
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") //
.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();
}