2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.sdc.tosca.datatypes.model;
19 import java.io.IOException;
20 import java.io.InputStream;
22 import org.junit.Assert;
23 import org.junit.Test;
24 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
27 public class TriggerTest {
29 public static final String TRIGGER_WF_NAME_ACTION = "/mock/trigger/wfNameAction.yaml";
30 public static final String TARGET_REQ = "reqA";
31 public static final String ACTION_WORKFLOW_VAL = "deployment_workflow";
32 public static final String POLICY_DEF_A = "policyA";
33 public static final String TRIGGER_A = "triggerA";
34 public static final String NODE_A = "nodeA";
35 public static final String TRIGGER_OPERATION_ACTION = "/mock/trigger/operationAction.yaml";
36 public static final String TARGET_CAP = "capA";
37 public static final String OPERATION_ACTION_KEY = "operationAction";
38 public static final String IMPLEMENTATION = "implementation";
39 ToscaExtensionYamlUtil toscaExtYamlUtil = new ToscaExtensionYamlUtil();
42 public void getPolicyTriggerActionWf() throws IOException {
43 String inputFile = TRIGGER_WF_NAME_ACTION;
44 ServiceTemplate serviceTemplate = getServiceTemplate(inputFile);
46 Map<String, PolicyDefinition> policies = serviceTemplate.getTopology_template().getPolicies();
47 Trigger trigger = policyCheck(policies);
48 Assert.assertEquals(TARGET_REQ, trigger.getTarget_filter().getRequirement());
49 Object action = trigger.getAction();
50 Assert.assertNotNull(action);
51 Assert.assertEquals(true, action instanceof String);
52 Assert.assertEquals(ACTION_WORKFLOW_VAL, action);
55 private ServiceTemplate getServiceTemplate(String inputPath) throws IOException {
56 try (InputStream yamlFile = toscaExtYamlUtil.loadYamlFileIs(inputPath)) {
57 return toscaExtYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
61 private Trigger policyCheck(Map<String, PolicyDefinition> policies) {
62 Assert.assertNotNull(policies);
63 PolicyDefinition policyDefinition = policies.get(POLICY_DEF_A);
64 Assert.assertNotNull(policyDefinition);
65 Map<String, Trigger> triggers = policyDefinition.getTriggers();
66 Assert.assertNotNull(triggers);
67 Trigger trigger = triggers.get(TRIGGER_A);
68 Assert.assertNotNull(trigger);
69 EventFilter targetFilter = trigger.getTarget_filter();
70 Assert.assertNotNull(targetFilter);
71 Assert.assertEquals(NODE_A, targetFilter.getNode());
76 public void getPolicyTriggerActionOperation() throws IOException {
77 String inputFile = TRIGGER_OPERATION_ACTION;
78 ServiceTemplate serviceTemplate = getServiceTemplate(inputFile);
80 Map<String, PolicyDefinition> policies = serviceTemplate.getTopology_template().getPolicies();
81 Trigger trigger = policyCheck(policies);
82 Assert.assertEquals(TARGET_CAP, trigger.getTarget_filter().getCapability());
83 Object action = trigger.getAction();
84 Assert.assertNotNull(action);
85 Assert.assertEquals(true, action instanceof Map);
86 Object operationAction = ((Map) action).get(OPERATION_ACTION_KEY);
87 Assert.assertNotNull(operationAction);
88 Assert.assertEquals(true, operationAction instanceof Map);
89 Assert.assertNotNull( ((Map)operationAction).get(IMPLEMENTATION));