2  * Copyright © 2017-2018 AT&T Intellectual Property.
 
   3  * Modifications Copyright © 2018 IBM.
 
   5  * Licensed under the Apache License, Version 2.0 (the "License");
 
   6  * you may not use this file except in compliance with the License.
 
   7  * You may obtain a copy of the License at
 
   9  *     http://www.apache.org/licenses/LICENSE-2.0
 
  11  * Unless required by applicable law or agreed to in writing, software
 
  12  * distributed under the License is distributed on an "AS IS" BASIS,
 
  13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  14  * See the License for the specific language governing permissions and
 
  15  * limitations under the License.
 
  18 package org.onap.ccsdk.apps.controllerblueprints.resource.dict.utils;
 
  21 import com.fasterxml.jackson.databind.JsonNode;
 
  22 import org.junit.Assert;
 
  23 import org.junit.Test;
 
  24 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants;
 
  25 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeTemplate;
 
  26 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils;
 
  27 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment;
 
  28 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition;
 
  29 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDictionaryConstants;
 
  30 import com.att.eelf.configuration.EELFLogger;
 
  31 import com.att.eelf.configuration.EELFManager;
 
  33 import java.util.HashMap;
 
  36  * ResourceDictionaryUtilsTest.
 
  38  * @author Brinda Santh
 
  40 public class ResourceDictionaryUtilsTest {
 
  41     private static final EELFLogger log = EELFManager.getInstance().getLogger(ResourceDictionaryUtilsTest.class);
 
  44     public void testPopulateSourceMapping() {
 
  46         ResourceAssignment resourceAssignment = new ResourceAssignment();
 
  47         resourceAssignment.setName("sample-assignment");
 
  48         ResourceDefinition resourceDefinition = new ResourceDefinition();
 
  49         Map<String, NodeTemplate> sources = new HashMap<>();
 
  50         resourceDefinition.setSources(sources);
 
  51         // To Check Empty Source
 
  52         ResourceDictionaryUtils.populateSourceMapping(resourceAssignment, resourceDefinition);
 
  53         Assert.assertEquals("Expected Empty source Input, but.", ResourceDictionaryConstants.SOURCE_INPUT, resourceAssignment.getDictionarySource());
 
  55         // To Check First Source
 
  56         resourceAssignment.setDictionarySource(null);
 
  57         sources.put(ResourceDictionaryConstants.SOURCE_DEFAULT, new NodeTemplate());
 
  58         ResourceDictionaryUtils.populateSourceMapping(resourceAssignment, resourceDefinition);
 
  59         Assert.assertEquals("Expected First source Default, but.", ResourceDictionaryConstants.SOURCE_DEFAULT, resourceAssignment.getDictionarySource());
 
  61         // To Check Assigned Source
 
  62         resourceAssignment.setDictionarySource(ResourceDictionaryConstants.SOURCE_PRIMARY_DB);
 
  63         ResourceDictionaryUtils.populateSourceMapping(resourceAssignment, resourceDefinition);
 
  64         Assert.assertEquals("Expected Assigned source DB, but.", ResourceDictionaryConstants.SOURCE_PRIMARY_DB, resourceAssignment.getDictionarySource());
 
  69     public void testFindFirstSource() {
 
  70         //To check if Empty Source
 
  71         Map<String, NodeTemplate> sources = new HashMap<>();
 
  72         String firstSource = ResourceDictionaryUtils.findFirstSource(sources);
 
  73         Assert.assertNull("Source populated, which is not expected.", firstSource);
 
  75         // TO check the first Source
 
  76         sources.put(ResourceDictionaryConstants.SOURCE_INPUT, new NodeTemplate());
 
  77         String inputFirstSource = ResourceDictionaryUtils.findFirstSource(sources);
 
  78         Assert.assertEquals("Expected source Input, but.", ResourceDictionaryConstants.SOURCE_INPUT, inputFirstSource);
 
  80         // TO check the multiple Source
 
  81         sources.put(ResourceDictionaryConstants.SOURCE_PRIMARY_DB, new NodeTemplate());
 
  82         String multipleFirstSource = ResourceDictionaryUtils.findFirstSource(sources);
 
  83         Assert.assertEquals("Expected source Input, but.", ResourceDictionaryConstants.SOURCE_INPUT, multipleFirstSource);
 
  88     public void testAssignInputs() {
 
  89         JsonNode data = JacksonUtils.Companion.jsonNodeFromClassPathFile("data/resource-assignment-input.json");
 
  90         Map<String, Object> context = new HashMap<>();
 
  91         ResourceDictionaryUtils.assignInputs(data, context);
 
  92         String path = BluePrintConstants.PATH_INPUTS.concat(BluePrintConstants.PATH_DIVIDER).concat("mapValue");
 
  93         log.info("populated context {}", context);
 
  94         Assert.assertTrue(String.format("failed to get variable : %s",path),context.containsKey(path));