16b02d86cbdb2d6c5e5dfe3912dd9109932976fd
[ccsdk/cds.git] / ms / controllerblueprints / modules / resource-dict / src / test / java / org / onap / ccsdk / cds / controllerblueprints / resource / dict / utils / ResourceDictionaryUtilsTest.java
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2018 IBM.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.cds.controllerblueprints.resource.dict.utils;
19
20 import com.fasterxml.jackson.databind.JsonNode;
21 import org.junit.Assert;
22 import org.junit.Test;
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants;
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate;
25 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils;
26 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment;
27 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition;
28 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDictionaryConstants;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import java.util.HashMap;
33 import java.util.Map;
34
35 /**
36  * ResourceDictionaryUtilsTest.
37  *
38  * @author Brinda Santh
39  */
40 public class ResourceDictionaryUtilsTest {
41     private static final Logger log = LoggerFactory.getLogger(ResourceDictionaryUtilsTest.class);
42
43     @Test
44     public void testPopulateSourceMapping() {
45
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());
54
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());
60
61         // To Check Assigned Source
62         resourceAssignment.setDictionarySource(ResourceDictionaryConstants.PROCESSOR_DB);
63         ResourceDictionaryUtils.populateSourceMapping(resourceAssignment, resourceDefinition);
64         Assert.assertEquals("Expected Assigned source DB, but.", ResourceDictionaryConstants.PROCESSOR_DB, resourceAssignment.getDictionarySource());
65
66     }
67
68     @Test
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);
74
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);
79
80         // TO check the multiple Source
81         sources.put(ResourceDictionaryConstants.PROCESSOR_DB, new NodeTemplate());
82         String multipleFirstSource = ResourceDictionaryUtils.findFirstSource(sources);
83         Assert.assertEquals("Expected source Input, but.", ResourceDictionaryConstants.SOURCE_INPUT, multipleFirstSource);
84
85     }
86
87     @Test
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));
95
96     }
97
98
99 }