aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/modules/blueprints/resource-dict/src/test/java/org/onap/ccsdk/cds/controllerblueprints/resource/dict/utils/ResourceDictionaryUtilsTest.java
blob: 79f21cfdfc9d562422c51db4c2da2068a28d8bcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * Copyright © 2017-2018 AT&T Intellectual Property.
 * Modifications Copyright © 2018 IBM.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.ccsdk.cds.controllerblueprints.resource.dict.utils;

import com.fasterxml.jackson.databind.JsonNode;
import org.junit.Assert;
import org.junit.Test;
import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants;
import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate;
import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils;
import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment;
import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition;
import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDictionaryConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * ResourceDictionaryUtilsTest.
 *
 * @author Brinda Santh
 */
public class ResourceDictionaryUtilsTest {
    private static final Logger log = LoggerFactory.getLogger(ResourceDictionaryUtilsTest.class);

    @Test
    public void testPopulateSourceMapping() {

        ResourceAssignment resourceAssignment = new ResourceAssignment();
        resourceAssignment.setName("sample-assignment");
        ResourceDefinition resourceDefinition = new ResourceDefinition();
        Map<String, NodeTemplate> sources = new HashMap<>();
        resourceDefinition.setSources(sources);
        // To Check Empty Source
        ResourceDictionaryUtils.populateSourceMapping(resourceAssignment, resourceDefinition);
        Assert.assertEquals("Expected Empty source Input, but.", ResourceDictionaryConstants.SOURCE_INPUT,
                resourceAssignment.getDictionarySource());

        // To Check First Source
        resourceAssignment.setDictionarySource(null);
        sources.put(ResourceDictionaryConstants.SOURCE_DEFAULT, new NodeTemplate());
        ResourceDictionaryUtils.populateSourceMapping(resourceAssignment, resourceDefinition);
        Assert.assertEquals("Expected First source Default, but.", ResourceDictionaryConstants.SOURCE_DEFAULT,
                resourceAssignment.getDictionarySource());

        // To Check Assigned Source
        resourceAssignment.setDictionarySource(ResourceDictionaryConstants.PROCESSOR_DB);
        ResourceDictionaryUtils.populateSourceMapping(resourceAssignment, resourceDefinition);
        Assert.assertEquals("Expected Assigned source DB, but.", ResourceDictionaryConstants.PROCESSOR_DB,
                resourceAssignment.getDictionarySource());

    }

    @Test
    public void testFindFirstSource() {
        // To check if Empty Source
        Map<String, NodeTemplate> sources = new HashMap<>();
        String firstSource = ResourceDictionaryUtils.findFirstSource(sources);
        Assert.assertNull("Source populated, which is not expected.", firstSource);

        // TO check the first Source
        sources.put(ResourceDictionaryConstants.SOURCE_INPUT, new NodeTemplate());
        String inputFirstSource = ResourceDictionaryUtils.findFirstSource(sources);
        Assert.assertEquals("Expected source Input, but.", ResourceDictionaryConstants.SOURCE_INPUT, inputFirstSource);

        // TO check the multiple Source
        sources.put(ResourceDictionaryConstants.PROCESSOR_DB, new NodeTemplate());
        String multipleFirstSource = ResourceDictionaryUtils.findFirstSource(sources);
        Assert.assertEquals("Expected source Input, but.", ResourceDictionaryConstants.SOURCE_INPUT,
                multipleFirstSource);

    }

    @Test
    public void testAssignInputs() {
        JsonNode data = JacksonUtils.Companion.jsonNodeFromClassPathFile("data/resource-assignment-input.json");
        Map<String, Object> context = new HashMap<>();
        ResourceDictionaryUtils.assignInputs(data, context);
        String path = BlueprintConstants.PATH_INPUTS.concat(BlueprintConstants.PATH_DIVIDER).concat("mapValue");
        log.info("populated context {}", context);
        Assert.assertTrue(String.format("failed to get variable : %s", path), context.containsKey(path));

    }

}