2  * ============LICENSE_START=======================================================
 
   3  * policy-yaml unit test
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
 
   6  * Modifications Copyright (C) 2019 Nordix Foundation.
 
   7  * ================================================================================
 
   8  * Licensed under the Apache License, Version 2.0 (the "License");
 
   9  * you may not use this file except in compliance with the License.
 
  10  * You may obtain a copy of the License at
 
  12  *      http://www.apache.org/licenses/LICENSE-2.0
 
  14  * Unless required by applicable law or agreed to in writing, software
 
  15  * distributed under the License is distributed on an "AS IS" BASIS,
 
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  17  * See the License for the specific language governing permissions and
 
  18  * limitations under the License.
 
  19  * ============LICENSE_END=========================================================
 
  22 package org.onap.policy.controlloop.policy;
 
  24 import static org.junit.Assert.assertEquals;
 
  25 import static org.junit.Assert.assertNotNull;
 
  26 import static org.junit.Assert.assertTrue;
 
  29 import java.io.FileInputStream;
 
  30 import java.io.InputStream;
 
  31 import org.junit.Test;
 
  32 import org.onap.policy.common.utils.io.Serializer;
 
  33 import org.slf4j.Logger;
 
  34 import org.slf4j.LoggerFactory;
 
  35 import org.yaml.snakeyaml.DumperOptions;
 
  36 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
 
  37 import org.yaml.snakeyaml.Yaml;
 
  38 import org.yaml.snakeyaml.constructor.Constructor;
 
  41 public class ControlLoopPolicyTest {
 
  42     private static final Logger logger = LoggerFactory.getLogger(ControlLoopPolicyTest.class);
 
  45     public void test1() throws Exception {
 
  46         this.test("src/test/resources/v1.0.0/policy_Test.yaml");
 
  50     public void testvService1() throws Exception {
 
  51         this.test("src/test/resources/v1.0.0/policy_vService.yaml");
 
  55     public void testOpenLoop() throws Exception {
 
  56         this.test("src/test/resources/v1.0.0/policy_OpenLoop.yaml");
 
  60     public void testvdns() throws Exception {
 
  61         this.test("src/test/resources/v2.0.0/policy_ONAP_demo_vDNS.yaml");
 
  65     public void testvFirewall() {
 
  66         // Chenfei to fix this.
 
  67         // this.test("src/test/resources/v2.0.0/policy_ONAP_demo_vFirewall.yaml");
 
  71     public void testvcpe() throws Exception {
 
  72         this.test("src/test/resources/v2.0.0/policy_ONAP_UseCase_vCPE.yaml");
 
  76     public void testvpci() throws Exception {
 
  77         this.test("src/test/resources/v2.0.0/policy_ONAP_UseCase_vPCI.yaml");
 
  81     public void testvolte() throws Exception {
 
  82         this.test("src/test/resources/v2.0.0/policy_ONAP_UseCase_VOLTE.yaml");
 
  86      * Does the actual test.
 
  88      * @param testFile input file
 
  89      * @throws Exception if an error occurs
 
  91     public void test(String testFile) throws Exception {
 
  92         try (InputStream is = new FileInputStream(new File(testFile))) {
 
  94             // Read the yaml into our Java Object
 
  96             Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
 
  97             Object obj = yaml.load(is);
 
  99             assertTrue(obj instanceof ControlLoopPolicy);
 
 102             // Now dump it to a yaml string
 
 104             DumperOptions options = new DumperOptions();
 
 105             options.setDefaultFlowStyle(FlowStyle.BLOCK);
 
 106             options.setPrettyFlow(true);
 
 107             yaml = new Yaml(options);
 
 108             String dumpedYaml = yaml.dump(obj);
 
 109             logger.debug(dumpedYaml);
 
 111             // Read that string back into our java object
 
 113             Object newObject = yaml.load(dumpedYaml);
 
 115             assertNotNull(newObject);
 
 116             assertTrue(newObject instanceof ControlLoopPolicy);
 
 117             assertEquals(obj, newObject);
 
 119             // test serialization
 
 120             ControlLoopPolicy policy = (ControlLoopPolicy) obj;
 
 121             ControlLoopPolicy policy2 = Serializer.roundTrip(policy);
 
 122             assertTrue(policy.equals(policy2));
 
 126     public void dump(Object obj) {
 
 127         logger.debug("Dumping ", obj.getClass().getName());
 
 128         logger.debug("{}", obj);