2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Ericsson
6 * =============================================================================
7 * Modifications Copyright (C) 2019 AT&T Intellectual Property
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.dg.dependencymanager.helper;
26 import java.io.IOException;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mockito;
34 import org.mockito.runners.MockitoJUnitRunner;
35 import org.onap.appc.dg.flowbuilder.exception.InvalidDependencyModelException;
36 import org.onap.appc.dg.objects.VnfcDependencyModel;
37 import org.onap.appc.dg.objects.Node;
38 import org.onap.appc.domainmodel.Vnfc;
39 import com.fasterxml.jackson.core.JsonProcessingException;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
42 import com.fasterxml.jackson.databind.node.ObjectNode;
44 @RunWith(MockitoJUnitRunner.class)
45 public class DependencyModelParserTest {
48 public ExpectedException expectedEx = ExpectedException.none();
50 DependencyModelParser parser = Mockito.spy(new DependencyModelParser());
51 ObjectMapper mockMapper;
54 public void setup() throws JsonProcessingException, IOException {
55 mockMapper = Mockito.mock(ObjectMapper.class);
56 ObjectNode mockNode = Mockito.mock(ObjectNode.class);
57 Mockito.doReturn(mockMapper).when(parser).getMapper();
58 Mockito.doReturn(mockNode).when(mockMapper).readTree("VNF_MODEL");
62 public void testGenerateDependencyModel()
63 throws InvalidDependencyModelException, JsonProcessingException, IOException {
64 ObjectNode topologyTemplate = new ObjectNode(JsonNodeFactory.instance);
65 topologyTemplate = (ObjectNode) new ObjectMapper().readTree(jsonString);
66 Mockito.doReturn(topologyTemplate).when(mockMapper).readTree("VNF_MODEL");
67 VnfcDependencyModel model = parser.generateDependencyModel("VNF_MODEL", "VNF_TYPE");
68 Assert.assertEquals(2, model.getDependencies().size());
72 public void testGenerateDependencyModelWithNode()
73 throws InvalidDependencyModelException, JsonProcessingException, IOException {
74 ObjectNode topologyTemplate = new ObjectNode(JsonNodeFactory.instance);
75 topologyTemplate = (ObjectNode) new ObjectMapper().readTree(jsonString);
76 Node<Vnfc> vnfc = new Node(new Vnfc());
77 Mockito.doReturn(vnfc).when(parser).getNode(Mockito.anySet(), Mockito.anyString());
78 Mockito.doReturn(topologyTemplate).when(mockMapper).readTree("VNF_MODEL");
79 VnfcDependencyModel model = parser.generateDependencyModel("VNF_MODEL", "VNF_TYPE");
80 Assert.assertEquals(0, model.getDependencies().size());
84 public void testGenerateDependencyModelExceptionFlow() throws InvalidDependencyModelException {
85 expectedEx.expect(InvalidDependencyModelException.class);
86 expectedEx.expectMessage("Dependency model is missing 'topology_template' or 'node_templates' elements");
87 VnfcDependencyModel model = parser.generateDependencyModel("VNF_MODEL", "VNF_TYPE");
90 private String jsonString =
91 "{\"topology_template\": {"
92 + " \"node_templates\": {"
93 + " \"Property Definition_Template\": {"
94 + " \"type\": \"org.onap.resource.vfc.vnf_type.abstract.nodes.property definition\","
95 + " \"properties\": {"
96 + " \"mandatory\": \"true\","
97 + " \"high_availability\": \"Active-Passive\""
98 + " },\"requirements\": ["
100 + " \"dependency\": {"
101 + " \"capability\": \"tosca.capabilities.Node\","
102 + " \"node\": \"tosca.nodes.Root\","
103 + " \"relationship\": \"tosca.relationships.DependsOn\","
104 + " \"occurrences\": ["
111 + " },\"tosca.nodes.Root\": {\"type\": \"VNFC_NAME\"}"