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-2020 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;
 
  27 import java.io.FileInputStream;
 
  28 import java.io.InputStreamReader;
 
  29 import org.junit.Test;
 
  30 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
 
  31 import org.slf4j.Logger;
 
  32 import org.slf4j.LoggerFactory;
 
  34 public class ControlLoopPolicyTest {
 
  35     private static final Logger logger = LoggerFactory.getLogger(ControlLoopPolicyTest.class);
 
  38     public void test1() throws Exception {
 
  39         this.test("src/test/resources/v1.0.0/policy_Test.yaml");
 
  43     public void testvService1() throws Exception {
 
  44         this.test("src/test/resources/v1.0.0/policy_vService.yaml");
 
  48     public void testOpenLoop() throws Exception {
 
  49         this.test("src/test/resources/v1.0.0/policy_OpenLoop.yaml");
 
  53     public void testvdns() throws Exception {
 
  54         this.test("src/test/resources/v2.0.0/policy_ONAP_demo_vDNS.yaml");
 
  58     public void testvFirewall() throws Exception {
 
  59         this.test("src/test/resources/v2.0.0/policy_ONAP_demo_vFirewall.yaml");
 
  63     public void testvcpe() throws Exception {
 
  64         this.test("src/test/resources/v2.0.0/policy_ONAP_UseCase_vCPE.yaml");
 
  68     public void testvpci() throws Exception {
 
  69         this.test("src/test/resources/v2.0.0/policy_ONAP_UseCase_vPCI.yaml");
 
  73     public void testvolte() throws Exception {
 
  74         this.test("src/test/resources/v2.0.0/policy_ONAP_UseCase_VOLTE.yaml");
 
  78      * Does the actual test.
 
  80      * @param testFile input file
 
  81      * @throws Exception if an error occurs
 
  83     public void test(String testFile) throws Exception {
 
  84         try (InputStreamReader fileInputStream = new InputStreamReader(new FileInputStream(testFile))) {
 
  86             // Read the yaml into our Java Object
 
  88             ControlLoopPolicy controlLoopPolicy1 =
 
  89                 new YamlJsonTranslator().fromYaml(fileInputStream, ControlLoopPolicy.class);
 
  90             assertNotNull(controlLoopPolicy1);
 
  91             dump(controlLoopPolicy1);
 
  94             // Now dump it to a yaml string
 
  96             String dumpedYaml = new YamlJsonTranslator().toYaml(controlLoopPolicy1);
 
  97             logger.debug(dumpedYaml);
 
  99             // Read that string back into our java object
 
 101             ControlLoopPolicy controlLoopPolicy2 =
 
 102                 new YamlJsonTranslator().fromYaml(dumpedYaml, ControlLoopPolicy.class);
 
 103             assertNotNull(controlLoopPolicy2);
 
 104             dump(controlLoopPolicy2);
 
 106             // test serialization
 
 107             assertEquals(controlLoopPolicy1, controlLoopPolicy2);
 
 111     public void dump(Object obj) {
 
 112         logger.debug("Dumping ", obj.getClass().getName());
 
 113         logger.debug("{}", obj);