57b95e2fbed79e2da2765df4be93c6798728bef1
[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 com.fasterxml.jackson.databind.DeserializationFeature;
20 import com.fasterxml.jackson.databind.ObjectMapper;
21 import fj.data.Either;
22 import org.junit.Test;
23 import org.mockito.Mockito;
24 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
25 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
26 import org.openecomp.sdc.be.impl.ComponentsUtils;
27 import org.openecomp.sdc.be.impl.ServiceFilterUtils;
28 import org.openecomp.sdc.be.model.ComponentInstance;
29 import org.openecomp.sdc.be.model.User;
30 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
31 import org.openecomp.sdc.be.ui.model.UIConstraint;
32 import org.openecomp.sdc.exception.ResponseFormat;
33
34 import java.io.ByteArrayOutputStream;
35 import java.io.IOException;
36 import java.util.Arrays;
37 import java.util.Set;
38
39 import static org.junit.Assert.assertFalse;
40 import static org.junit.Assert.assertNotNull;
41 import static org.junit.Assert.assertTrue;
42
43 public class ServiceFilterUtilsCIChangeTest extends BaseServiceFilterUtilsTest {
44
45
46     @Test
47     public void checkComponentInstanceIsFound() {
48         Set<String> nodesFiltersToBeDeleted = getNodeFiltersToBeDeleted(CI_NAME);
49         assertNotNull(nodesFiltersToBeDeleted);
50         assertTrue(nodesFiltersToBeDeleted.contains(CI_NAME));
51     }
52
53     private Set<String> getNodeFiltersToBeDeleted(String inCiName) {
54         requirementNodeFilterPropertyDataDefinition
55                 .setConstraints(Arrays.asList("mem_size:\n" + "  equal:\n" + "    get_property: ["+CI_NAME+", some static]\n"));
56         ComponentInstance ci = new ComponentInstance();
57         ci.setName(inCiName);
58         return ServiceFilterUtils.getNodesFiltersToBeDeleted(service, ci);
59     }
60
61     @Test
62     public void checkComponentInstanceIsNotFound() {
63         Set<String> nodesFiltersToBeDeleted = getNodeFiltersToBeDeleted(CI_NAME + " aaa bbb");
64         assertNotNull(nodesFiltersToBeDeleted);
65         assertTrue(nodesFiltersToBeDeleted.isEmpty());
66         assertFalse(nodesFiltersToBeDeleted.contains(CI_NAME));
67     }
68
69     @Test
70     public void testServiceConstraintPairSerialization() throws IOException {
71         UIConstraint uiConstraint =new UIConstraint();
72         ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
73         mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
74         assertTrue(mapper.canSerialize(uiConstraint.getClass()));
75         String data;
76         try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()  ) {
77             mapper.writeValue(outputStream, uiConstraint);
78             data = outputStream.toString();
79
80         }
81         assertNotNull(data);
82         final AuditingManager mock = Mockito.mock(AuditingManager.class);
83         final Either<UIConstraint, ResponseFormat> serviceConstraintPairResponseFormatEither =
84                 new ComponentsUtils(mock)
85                         .convertJsonToObjectUsingObjectMapper(data, new User(), UIConstraint.class,
86                                 AuditingActionEnum.ADD_GROUPING, ComponentTypeEnum.SERVICE);
87         assertTrue(serviceConstraintPairResponseFormatEither.isLeft());
88
89     }
90 }