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=cdf614c2a31f671a925168bf8c70c3dad927603d;hb=81bdaffd323c941da910c258487b7efb78615d6d;hp=d02522c527a061597207e44d6c8ebb50b9328836;hpb=f0273617b916cdc8633382291b9986e33cc13fa1;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 d02522c5..cdf614c2 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/MockPolicyAgent.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/MockPolicyAgent.java @@ -17,6 +17,7 @@ * limitations under the License. * ========================LICENSE_END=================================== */ + package org.oransc.policyagent; import com.google.gson.JsonObject; @@ -27,103 +28,126 @@ import java.io.IOException; import java.net.URL; import java.nio.file.Files; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.oransc.policyagent.configuration.ApplicationConfig; import org.oransc.policyagent.repository.ImmutablePolicyType; import org.oransc.policyagent.repository.Policies; import org.oransc.policyagent.repository.PolicyType; import org.oransc.policyagent.repository.PolicyTypes; import org.oransc.policyagent.repository.Rics; +import org.oransc.policyagent.utils.MockA1ClientFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; 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.junit4.SpringRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; -@RunWith(SpringRunner.class) +@ExtendWith(SpringExtension.class) @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT) public class MockPolicyAgent { + private static final Logger logger = LoggerFactory.getLogger(MockPolicyAgent.class); @Autowired - private Rics rics; - - @Autowired - private Policies policies; - - @Autowired - private PolicyTypes policyTypes; + Rics rics; static class MockApplicationConfig extends ApplicationConfig { @Override - public void initialize() { + public String getLocalConfigurationFilePath() { URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json"); - loadConfigurationFromFile(url.getFile()); + return url.getFile(); } } /** - * overrides the BeanFactory + * Overrides the BeanFactory. */ @TestConfiguration static class TestBeanFactory { + private final Rics rics = new Rics(); + private final Policies policies = new Policies(); + private final PolicyTypes policyTypes = new PolicyTypes(); + @Bean public ApplicationConfig getApplicationConfig() { return new MockApplicationConfig(); } - } - @LocalServerPort - private int port; + @Bean + public MockA1ClientFactory getA1ClientFactory() { + PolicyTypes ricTypes = new PolicyTypes(); + loadTypes(ricTypes); + return new MockA1ClientFactory(ricTypes); + } + + @Bean + public Policies getPolicies() { + return this.policies; + } + + @Bean + public PolicyTypes getPolicyTypes() { + return this.policyTypes; + } + + @Bean + public Rics getRics() { + return this.rics; + } + + 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(); + } + + private static String readFile(File file) throws IOException { + return new String(Files.readAllBytes(file.toPath())); + } - public void keepServerAlive() { - System.out.println("Keeping server alive!"); - try { - synchronized (this) { - this.wait(); + private void loadTypes(PolicyTypes policyTypes) { + File[] files = getResourceFolderFiles("policy_types/"); + for (File file : files) { + try { + String schema = readFile(file); + String typeName = title(schema); + PolicyType type = ImmutablePolicyType.builder().name(typeName).schema(schema).build(); + policyTypes.put(type); + } catch (Exception e) { + logger.error("Could not load json schema ", e); + } } - } catch (Exception ex) { - System.out.println("Unexpected: " + ex.toString()); + policyTypes.put(ImmutablePolicyType.builder().name("").schema("{}").build()); } } - 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(); - } + @LocalServerPort + private int port; - private static String readFile(File file) throws IOException { - return new String(Files.readAllBytes(file.toPath())); + private void keepServerAlive() throws InterruptedException { + logger.info("Keeping server alive!"); + synchronized (this) { + this.wait(); + } } private static String title(String jsonSchema) { - JsonObject parsedSchema = (JsonObject) new JsonParser().parse(jsonSchema); + JsonObject parsedSchema = (JsonObject) JsonParser.parseString(jsonSchema); String title = parsedSchema.get("title").getAsString(); return title; } - private static void loadTypes(PolicyTypes policyTypes) { - File[] files = getResourceFolderFiles("policy_types/"); - for (File file : files) { - try { - String schema = readFile(file); - String typeName = title(schema); - PolicyType type = ImmutablePolicyType.builder().name(typeName).jsonSchema(schema).build(); - policyTypes.put(type); - } catch (Exception e) { - System.out.println("Could not load json schema " + e); - } - } - } - @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 { - loadTypes(this.policyTypes); keepServerAlive(); }