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=dd57710cd02f37ea1b1f5434989fc02a6c012d82;hb=637540bc28fbf337e0c4c58c051a6b4f7ceb321d;hp=d02522c527a061597207e44d6c8ebb50b9328836;hpb=8831a02bce715562f3cacce1691bf4d9d3af206b;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..dd57710c 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,9 +27,14 @@ import java.io.File; import java.io.IOException; import java.net.URL; import java.nio.file.Files; - -import org.junit.Test; -import org.junit.runner.RunWith; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Vector; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.oransc.policyagent.clients.A1Client; import org.oransc.policyagent.configuration.ApplicationConfig; import org.oransc.policyagent.repository.ImmutablePolicyType; import org.oransc.policyagent.repository.Policies; @@ -41,9 +47,10 @@ 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; +import reactor.core.publisher.Mono; -@RunWith(SpringRunner.class) +@ExtendWith(SpringExtension.class) @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT) public class MockPolicyAgent { @@ -57,6 +64,7 @@ public class MockPolicyAgent { private PolicyTypes policyTypes; static class MockApplicationConfig extends ApplicationConfig { + @Override public void initialize() { URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json"); @@ -64,22 +72,140 @@ public class MockPolicyAgent { } } + private static class RicPolicyDatabase { + private Map> policies = new HashMap<>(); + + public void putPolicy(String nearRtRicUrl, String policyId, String policyString) { + getPolicies(nearRtRicUrl).put(policyId, policyString); + } + + public Collection getPolicyIdentities(String nearRtRicUrl) { + return getPolicies(nearRtRicUrl).keySet(); + } + + public void deletePolicy(String nearRtRicUrl, String policyId) { + getPolicies(nearRtRicUrl).remove(policyId); + } + + private Map getPolicies(String nearRtRicUrl) { + if (!policies.containsKey(nearRtRicUrl)) { + policies.put(nearRtRicUrl, new HashMap<>()); + } + return policies.get(nearRtRicUrl); + } + } + + static class A1ClientMock implements A1Client { + + private final RicPolicyDatabase policies = new RicPolicyDatabase(); + private final PolicyTypes policyTypes = new PolicyTypes(); + + A1ClientMock() { + loadTypes(this.policyTypes); + } + + @Override + public Mono> getPolicyTypeIdentities(String nearRtRicUrl) { + Vector result = new Vector<>(); + for (PolicyType p : this.policyTypes.getAll()) { + result.add(p.name()); + } + return Mono.just(result); + } + + @Override + public Mono> getPolicyIdentities(String nearRtRicUrl) { + Collection result = policies.getPolicyIdentities(nearRtRicUrl); + return Mono.just(result); + } + + @Override + public Mono getPolicyType(String nearRtRicUrl, String policyTypeId) { + try { + return Mono.just(this.policyTypes.getType(policyTypeId).schema()); + } catch (Exception e) { + return Mono.error(e); + } + } + + @Override + public Mono putPolicy(String nearRtRicUrl, String policyId, String policyString) { + policies.putPolicy(nearRtRicUrl, policyId, policyString); + return Mono.just("OK"); + } + + @Override + public Mono deletePolicy(String nearRtRicUrl, String policyId) { + policies.deletePolicy(nearRtRicUrl, policyId); + return Mono.just("OK"); + } + + 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())); + } + + 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) { + System.out.println("Could not load json schema " + e); + } + } + } + } + /** * 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(); } + + @Bean + A1Client getA1Client() { + return new A1ClientMock(); + } + + @Bean + public Policies getPolicies() { + return this.policies; + } + + @Bean + public PolicyTypes getPolicyTypes() { + return this.policyTypes; + } + + @Bean + public Rics getRics() { + return this.rics; + } } @LocalServerPort private int port; - public void keepServerAlive() { + private void keepServerAlive() { System.out.println("Keeping server alive!"); try { synchronized (this) { @@ -90,40 +216,14 @@ public class MockPolicyAgent { } } - 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())); - } - private static String title(String jsonSchema) { JsonObject parsedSchema = (JsonObject) new JsonParser().parse(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 public void runMock() throws Exception { - loadTypes(this.policyTypes); keepServerAlive(); }