aboutsummaryrefslogtreecommitdiffstats
path: root/common/onap-tosca-datatype/src/test/java/org/onap/sdc/tosca/datatypes/model/TriggerTest.java
blob: 60886b9b42dc3b6bb58f075f3ae348f1d821f7f0 (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
/*
 * Copyright © 2016-2018 European Support Limited
 *
 * 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.sdc.tosca.datatypes.model;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;


public class TriggerTest {

    public static final String TRIGGER_WF_NAME_ACTION = "/mock/trigger/wfNameAction.yaml";
    public static final String TARGET_REQ = "reqA";
    public static final String ACTION_WORKFLOW_VAL = "deployment_workflow";
    public static final String POLICY_DEF_A = "policyA";
    public static final String TRIGGER_A = "triggerA";
    public static final String NODE_A = "nodeA";
    public static final String TRIGGER_OPERATION_ACTION = "/mock/trigger/operationAction.yaml";
    public static final String TARGET_CAP = "capA";
    public static final String OPERATION_ACTION_KEY = "operationAction";
    public static final String IMPLEMENTATION = "implementation";
    ToscaExtensionYamlUtil toscaExtYamlUtil = new ToscaExtensionYamlUtil();

    @Test
    public void getPolicyTriggerActionWf() throws IOException {
        String inputFile = TRIGGER_WF_NAME_ACTION;
        ServiceTemplate serviceTemplate = getServiceTemplate(inputFile);

        Map<String, PolicyDefinition> policies = serviceTemplate.getTopology_template().getPolicies();
        Trigger trigger = policyCheck(policies);
        Assert.assertEquals(TARGET_REQ, trigger.getTarget_filter().getRequirement());
        Object action = trigger.getAction();
        Assert.assertNotNull(action);
        Assert.assertEquals(true, action instanceof String);
        Assert.assertEquals(ACTION_WORKFLOW_VAL, action);
    }

    private ServiceTemplate getServiceTemplate(String inputPath) throws IOException {
        try (InputStream yamlFile = toscaExtYamlUtil.loadYamlFileIs(inputPath)) {
            return toscaExtYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
        }
    }

    private Trigger policyCheck(Map<String, PolicyDefinition> policies) {
        Assert.assertNotNull(policies);
        PolicyDefinition policyDefinition = policies.get(POLICY_DEF_A);
        Assert.assertNotNull(policyDefinition);
        Map<String, Trigger> triggers = policyDefinition.getTriggers();
        Assert.assertNotNull(triggers);
        Trigger trigger = triggers.get(TRIGGER_A);
        Assert.assertNotNull(trigger);
        EventFilter targetFilter = trigger.getTarget_filter();
        Assert.assertNotNull(targetFilter);
        Assert.assertEquals(NODE_A, targetFilter.getNode());
        return trigger;
    }

    @Test
    public void getPolicyTriggerActionOperation() throws IOException {
        String inputFile = TRIGGER_OPERATION_ACTION;
        ServiceTemplate serviceTemplate = getServiceTemplate(inputFile);

        Map<String, PolicyDefinition> policies = serviceTemplate.getTopology_template().getPolicies();
        Trigger trigger = policyCheck(policies);
        Assert.assertEquals(TARGET_CAP, trigger.getTarget_filter().getCapability());
        Object action = trigger.getAction();
        Assert.assertNotNull(action);
        Assert.assertEquals(true, action instanceof Map);
        Object operationAction = ((Map) action).get(OPERATION_ACTION_KEY);
        Assert.assertNotNull(operationAction);
        Assert.assertEquals(true, operationAction instanceof Map);
        Assert.assertNotNull( ((Map)operationAction).get(IMPLEMENTATION));

    }

}