2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2023 Nordix Foundation. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24 import static org.mockito.Mockito.CALLS_REAL_METHODS;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.mock;
28 import fj.data.Either;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
34 import org.janusgraph.core.JanusGraphVertex;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.extension.ExtendWith;
38 import org.mockito.Mock;
39 import org.mockito.junit.jupiter.MockitoExtension;
40 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
41 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
42 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
43 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
44 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
45 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
46 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
47 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
48 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
49 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
50 import org.openecomp.sdc.be.model.LifecycleStateEnum;
51 import org.openecomp.sdc.be.model.Service;
52 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
54 @ExtendWith(MockitoExtension.class)
55 public class ForwardingPathOperationTest {
58 private JanusGraphDao janusGraphDao;
61 private JanusGraphVertex vertex;
63 private ForwardingPathOperation test = mock(ForwardingPathOperation.class, CALLS_REAL_METHODS);
66 public void setUp() throws Exception {
67 test.setJanusGraphDao(janusGraphDao);
71 public void deleteForwardingPath() {
72 Service service = new Service();
73 Set<String> paths = Set.of("path1", "path2");
74 String uniqueId = "uniqueId";
75 service.setUniqueId(uniqueId);
76 Map<GraphPropertyEnum, Object> hasProps1 = new HashMap<>();
77 hasProps1.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
78 hasProps1.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
79 List<GraphVertex> list = new ArrayList<>();
80 GraphVertex graphVertex = new GraphVertex();
81 graphVertex.setVertex(vertex);
82 graphVertex.setUniqueId(uniqueId);
83 graphVertex.setMetadataProperties(hasProps1);
84 list.add(graphVertex);
85 doReturn(Either.left(graphVertex)).when(janusGraphDao).getVertexById(service.getUniqueId(), JsonParseFlagEnum.NoParse);
86 doReturn(StorageOperationStatus.OK).when(test).deleteToscaDataElements(graphVertex, EdgeLabelEnum.FORWARDING_PATH,
87 new ArrayList<>(paths));
88 Either<Set<String>, StorageOperationStatus> ret = test.deleteForwardingPath(service, paths);
89 assertTrue(ret.isLeft());
93 public void deleteForwardingPath_NotFound() {
94 Service service = new Service();
95 Set<String> paths = Set.of("path1", "path2");
96 String uniqueId = "uniqueId";
97 service.setUniqueId(uniqueId);
98 Map<GraphPropertyEnum, Object> hasProps1 = new HashMap<>();
99 hasProps1.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
100 hasProps1.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
101 List<GraphVertex> list = new ArrayList<>();
102 GraphVertex graphVertex = new GraphVertex();
103 graphVertex.setVertex(vertex);
104 graphVertex.setUniqueId(uniqueId);
105 graphVertex.setMetadataProperties(hasProps1);
106 list.add(graphVertex);
107 doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(janusGraphDao).getVertexById(service.getUniqueId(),
108 JsonParseFlagEnum.NoParse);
109 Either<Set<String>, StorageOperationStatus> ret = test.deleteForwardingPath(service, paths);
110 assertTrue(ret.isRight());
114 public void deleteForwardingPath_ErrorDeleting() {
115 Service service = new Service();
116 Set<String> paths = Set.of("path1", "path2");
117 String uniqueId = "uniqueId";
118 service.setUniqueId(uniqueId);
119 Map<GraphPropertyEnum, Object> hasProps1 = new HashMap<>();
120 hasProps1.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
121 hasProps1.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
122 List<GraphVertex> list = new ArrayList<>();
123 GraphVertex graphVertex = new GraphVertex();
124 graphVertex.setVertex(vertex);
125 graphVertex.setUniqueId(uniqueId);
126 graphVertex.setMetadataProperties(hasProps1);
127 list.add(graphVertex);
128 doReturn(Either.left(graphVertex)).when(janusGraphDao).getVertexById(service.getUniqueId(), JsonParseFlagEnum.NoParse);
129 doReturn(StorageOperationStatus.CANNOT_UPDATE_EXISTING_ENTITY).when(test).deleteToscaDataElements(graphVertex, EdgeLabelEnum.FORWARDING_PATH,
130 new ArrayList<>(paths));
131 Either<Set<String>, StorageOperationStatus> ret = test.deleteForwardingPath(service, paths);
132 assertTrue(ret.isRight());
136 public void addForwardingPath() {
137 Service service = new Service();
138 String uniqueId = "uniqueId";
139 service.setUniqueId(uniqueId);
140 Map<GraphPropertyEnum, Object> hasProps1 = new HashMap<>();
141 hasProps1.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
142 hasProps1.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
143 GraphVertex graphVertex = new GraphVertex();
144 graphVertex.setVertex(vertex);
145 graphVertex.setUniqueId(uniqueId);
146 graphVertex.setMetadataProperties(hasProps1);
147 ForwardingPathDataDefinition forwardingPathDataDefinition = new ForwardingPathDataDefinition("forwardingPath");
148 doReturn(Either.left(graphVertex)).when(janusGraphDao).getVertexById(service.getUniqueId(), JsonParseFlagEnum.NoParse);
149 doReturn(StorageOperationStatus.OK).when(test).addToscaDataToToscaElement(graphVertex, EdgeLabelEnum.FORWARDING_PATH,
150 VertexTypeEnum.FORWARDING_PATH, List.of(forwardingPathDataDefinition), JsonPresentationFields.UNIQUE_ID);
151 Either<ForwardingPathDataDefinition, StorageOperationStatus> ret = test.addForwardingPath(service.getUniqueId(), forwardingPathDataDefinition);
152 assertTrue(ret.isLeft());
156 public void addForwardingPath_NotFound() {
157 Service service = new Service();
158 String uniqueId = "uniqueId";
159 service.setUniqueId(uniqueId);
160 Map<GraphPropertyEnum, Object> hasProps1 = new HashMap<>();
161 hasProps1.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
162 hasProps1.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
163 GraphVertex graphVertex = new GraphVertex();
164 graphVertex.setVertex(vertex);
165 graphVertex.setUniqueId(uniqueId);
166 graphVertex.setMetadataProperties(hasProps1);
167 ForwardingPathDataDefinition forwardingPathDataDefinition = new ForwardingPathDataDefinition("forwardingPath");
168 doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(janusGraphDao).getVertexById(service.getUniqueId(), JsonParseFlagEnum.NoParse);
169 Either<ForwardingPathDataDefinition, StorageOperationStatus> ret = test.addForwardingPath(service.getUniqueId(), forwardingPathDataDefinition);
170 assertTrue(ret.isRight());
174 public void updateForwardingPath() {
175 Service service = new Service();
176 String uniqueId = "uniqueId";
177 service.setUniqueId(uniqueId);
178 Map<GraphPropertyEnum, Object> hasProps1 = new HashMap<>();
179 hasProps1.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
180 hasProps1.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
181 GraphVertex graphVertex = new GraphVertex();
182 graphVertex.setVertex(vertex);
183 graphVertex.setUniqueId(uniqueId);
184 graphVertex.setMetadataProperties(hasProps1);
185 ForwardingPathDataDefinition forwardingPathDataDefinition = new ForwardingPathDataDefinition("forwardingPath");
186 doReturn(Either.left(graphVertex)).when(janusGraphDao).getVertexById(service.getUniqueId(), JsonParseFlagEnum.NoParse);
187 doReturn(StorageOperationStatus.OK).when(test).updateToscaDataOfToscaElement(graphVertex, EdgeLabelEnum.FORWARDING_PATH,
188 VertexTypeEnum.FORWARDING_PATH, List.of(forwardingPathDataDefinition), JsonPresentationFields.UNIQUE_ID);
189 Either<ForwardingPathDataDefinition, StorageOperationStatus> ret = test.updateForwardingPath(service.getUniqueId(), forwardingPathDataDefinition);
190 assertTrue(ret.isLeft());
194 public void updateForwardingPath_Error() {
195 Service service = new Service();
196 String uniqueId = "uniqueId";
197 service.setUniqueId(uniqueId);
198 Map<GraphPropertyEnum, Object> hasProps1 = new HashMap<>();
199 hasProps1.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
200 hasProps1.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
201 GraphVertex graphVertex = new GraphVertex();
202 graphVertex.setVertex(vertex);
203 graphVertex.setUniqueId(uniqueId);
204 graphVertex.setMetadataProperties(hasProps1);
205 ForwardingPathDataDefinition forwardingPathDataDefinition = new ForwardingPathDataDefinition("forwardingPath");
206 doReturn(Either.left(graphVertex)).when(janusGraphDao).getVertexById(service.getUniqueId(), JsonParseFlagEnum.NoParse);
207 doReturn(StorageOperationStatus.ARTIFACT_NOT_FOUND).when(test).updateToscaDataOfToscaElement(graphVertex, EdgeLabelEnum.FORWARDING_PATH,
208 VertexTypeEnum.FORWARDING_PATH, List.of(forwardingPathDataDefinition), JsonPresentationFields.UNIQUE_ID);
209 Either<ForwardingPathDataDefinition, StorageOperationStatus> ret = test.updateForwardingPath(service.getUniqueId(), forwardingPathDataDefinition);
210 assertTrue(ret.isRight());