8004d2ca2030e6f4e7fb5e75379dd13cdb701ee5
[ccsdk/cds.git] /
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.",
60                 ResourceDictionaryConstants.SOURCE_DEFAULT,
61                 resourceAssignment.getDictionarySource());
62
63         // To Check Assigned Source
64         resourceAssignment.setDictionarySource(ResourceDictionaryConstants.PROCESSOR_DB);
65         ResourceDictionaryUtils.populateSourceMapping(resourceAssignment, resourceDefinition);
66         Assert.assertEquals("Expected Assigned source DB, but.", ResourceDictionaryConstants.PROCESSOR_DB, resourceAssignment.getDictionarySource());
67
68     }
69
70     @Test
71     public void testFindFirstSource() {
72         //To check if Empty Source
73         Map<String, NodeTemplate> sources = new HashMap<>();
74         String firstSource = ResourceDictionaryUtils.findFirstSource(sources);
75         Assert.assertNull("Source populated, which is not expected.", firstSource);
76
77         // TO check the first Source
78         sources.put(ResourceDictionaryConstants.SOURCE_INPUT, new NodeTemplate());
79         String inputFirstSource = ResourceDictionaryUtils.findFirstSource(sources);
80         Assert.assertEquals("Expected source Input, but.", ResourceDictionaryConstants.SOURCE_INPUT, inputFirstSource);
81
82         // TO check the multiple Source
83         sources.put(ResourceDictionaryConstants.PROCESSOR_DB, new NodeTemplate());
84         String multipleFirstSource = ResourceDictionaryUtils.findFirstSource(sources);
85         Assert.assertEquals("Expected source Input, but.", ResourceDictionaryConstants.SOURCE_INPUT, multipleFirstSource);
86
87     }
88
89     @Test
90     public void testAssignInputs() {
91         JsonNode data = JacksonUtils.Companion.jsonNodeFromClassPathFile("data/resource-assignment-input.json");
92         Map<String, Object> context = new HashMap<>();
93         ResourceDictionaryUtils.assignInputs(data, context);
94         String path = BluePrintConstants.PATH_INPUTS.concat(BluePrintConstants.PATH_DIVIDER).concat("mapValue");
95         log.info("populated context {}", context);
96         Assert.assertTrue(String.format("failed to get variable : %s", path), context.containsKey(path));
97
98     }
99
100 }