Support TOSCA functions in Node Filters
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / nodeFilter / ServiceFilterUtilsCIChangeTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.be.nodeFilter;
18
19 import static org.junit.jupiter.api.Assertions.assertNotNull;
20 import static org.junit.jupiter.api.Assertions.assertTrue;
21
22 import com.fasterxml.jackson.databind.DeserializationFeature;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import fj.data.Either;
25 import java.io.ByteArrayOutputStream;
26 import java.io.IOException;
27 import java.util.List;
28 import java.util.Set;
29 import org.junit.jupiter.api.Test;
30 import org.mockito.Mockito;
31 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
32 import org.openecomp.sdc.be.datatypes.elements.PropertyFilterConstraintDataDefinition;
33 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
34 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
35 import org.openecomp.sdc.be.datatypes.enums.FilterValueType;
36 import org.openecomp.sdc.be.datatypes.enums.PropertyFilterTargetType;
37 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
38 import org.openecomp.sdc.be.impl.ComponentsUtils;
39 import org.openecomp.sdc.be.impl.ServiceFilterUtils;
40 import org.openecomp.sdc.be.model.ComponentInstance;
41 import org.openecomp.sdc.be.model.User;
42 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
43 import org.openecomp.sdc.be.ui.model.UIConstraint;
44 import org.openecomp.sdc.exception.ResponseFormat;
45
46 class ServiceFilterUtilsCIChangeTest extends BaseServiceFilterUtilsTest {
47
48     @Test
49     void checkComponentInstanceIsFound() {
50         Set<String> nodesFiltersToBeDeleted = getNodeFiltersToBeDeleted(CI_NAME);
51         assertNotNull(nodesFiltersToBeDeleted);
52         assertTrue(nodesFiltersToBeDeleted.contains(CI_NAME));
53     }
54
55     private Set<String> getNodeFiltersToBeDeleted(String inCiName) {
56         final var propertyFilterConstraint = new PropertyFilterConstraintDataDefinition();
57         propertyFilterConstraint.setPropertyName("mem_size");
58         propertyFilterConstraint.setOperator(ConstraintType.EQUAL);
59         propertyFilterConstraint.setValue(createToscaGetFunction(CI_NAME, ToscaGetFunctionType.GET_PROPERTY, List.of("some static")));
60         propertyFilterConstraint.setValueType(FilterValueType.GET_PROPERTY);
61         propertyFilterConstraint.setTargetType(PropertyFilterTargetType.PROPERTY);
62         propertyFilterDataDefinition
63                 .setConstraints(List.of(propertyFilterConstraint));
64         final var componentInstance = new ComponentInstance();
65         componentInstance.setName(inCiName);
66         return ServiceFilterUtils.getNodesFiltersToBeDeleted(service, componentInstance);
67     }
68
69     @Test
70     void checkComponentInstanceIsNotFound() {
71         Set<String> nodesFiltersToBeDeleted = getNodeFiltersToBeDeleted(CI_NAME + " aaa bbb");
72         assertNotNull(nodesFiltersToBeDeleted);
73         assertTrue(nodesFiltersToBeDeleted.isEmpty());
74     }
75
76     @Test
77     void testServiceConstraintPairSerialization() throws IOException {
78         UIConstraint uiConstraint =new UIConstraint();
79         ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
80         mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
81         assertTrue(mapper.canSerialize(uiConstraint.getClass()));
82         String data;
83         try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()  ) {
84             mapper.writeValue(outputStream, uiConstraint);
85             data = outputStream.toString();
86
87         }
88         assertNotNull(data);
89         final AuditingManager mock = Mockito.mock(AuditingManager.class);
90         final Either<UIConstraint, ResponseFormat> serviceConstraintPairResponseFormatEither =
91                 new ComponentsUtils(mock)
92                         .convertJsonToObjectUsingObjectMapper(data, new User(), UIConstraint.class,
93                                 AuditingActionEnum.ADD_GROUPING, ComponentTypeEnum.SERVICE);
94         assertTrue(serviceConstraintPairResponseFormatEither.isLeft());
95
96     }
97 }