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=b7ea7dd1cc2af01de2de15199bcc46234e5c1901;hpb=4a112834cf7ea69f230fde864856093ecadb9cfe;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 b7ea7dd1..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; @@ -26,95 +27,44 @@ import java.io.File; import java.io.IOException; import java.net.URL; import java.nio.file.Files; -import java.util.Vector; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.oransc.policyagent.clients.A1Client; +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 reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; +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(); } } - static class A1ClientMock implements A1Client { - private final Policies policies; - private final PolicyTypes policyTypes; - - A1ClientMock(Policies policies, PolicyTypes policyTypes) { - this.policies = policies; - this.policyTypes = policyTypes; - } - - @Override - public Flux getAllPolicyTypes(String nearRtRicUrl) { - Vector result = new Vector<>(); - for (PolicyType p : this.policyTypes.getAll()) { - result.add(p.name()); - } - return Flux.fromIterable(result); - } - - @Override - public Flux getPoliciesForType(String nearRtRicUrl, String policyTypeId) { - return Flux.empty(); - } - - @Override - public Mono getPolicy(String nearRtRicUrl, String policyId) { - try { - return Mono.just(this.policies.get(policyId).json()); - } catch (Exception e) { - return Mono.error(e); - } - } - - @Override - public Mono putPolicy(String nearRtRicUrl, String policyId, String policyString) { - return Mono.just("OK"); - } - - @Override - public Mono deletePolicy(String nearRtRicUrl, String policyId) { - return Mono.error(new Exception("TODO We cannot use Void like this")); // TODO We cannot use Void like this - } - - } - /** - * overrides the BeanFactory + * Overrides the BeanFactory. */ @TestConfiguration static class TestBeanFactory { @@ -129,8 +79,10 @@ public class MockPolicyAgent { } @Bean - A1Client getA1Client() { - return new A1ClientMock(this.policies, this.policyTypes); + public MockA1ClientFactory getA1ClientFactory() { + PolicyTypes ricTypes = new PolicyTypes(); + loadTypes(ricTypes); + return new MockA1ClientFactory(ricTypes); } @Bean @@ -148,56 +100,54 @@ public class MockPolicyAgent { 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(); + } - @LocalServerPort - private int port; + 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(); }