Fix test cases failing incorrectly
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / jsonjanusgraph / operations / GroupsOperationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
22
23 import static java.util.Arrays.asList;
24 import static org.assertj.core.api.Assertions.assertThat;
25
26 import fj.data.Either;
27 import java.util.List;
28 import org.junit.jupiter.api.AfterEach;
29 import org.junit.jupiter.api.BeforeAll;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.openecomp.sdc.be.dao.config.JanusGraphSpringConfig;
33 import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphDao;
34 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
35 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
36 import org.openecomp.sdc.be.model.Component;
37 import org.openecomp.sdc.be.model.ComponentParametersView;
38 import org.openecomp.sdc.be.model.GroupDefinition;
39 import org.openecomp.sdc.be.model.ModelTestBase;
40 import org.openecomp.sdc.be.model.Resource;
41 import org.openecomp.sdc.be.model.config.ModelOperationsSpringConfig;
42 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
45
46
47 @SpringJUnitConfig(classes = {JanusGraphSpringConfig.class, ModelOperationsSpringConfig.class})
48 public class GroupsOperationTest extends ModelTestBase {
49
50     @Autowired
51     private HealingJanusGraphDao janusGraphDao;
52     @Autowired
53     private GroupsOperation groupsOperation;
54     @Autowired
55     private ToscaOperationFacade toscaOperationFacade;
56     private Component container;
57
58     @BeforeAll
59     public static void initClass() {
60         ModelTestBase.init();
61     }
62
63     @BeforeEach
64     public void setUp() throws Exception {
65         container = new Resource();
66         container.setUniqueId(CONTAINER_ID);
67         Either<GraphVertex, JanusGraphOperationStatus> createdCmpt = janusGraphDao.createVertex(createBasicContainerGraphVertex());
68         assertThat(createdCmpt.isLeft()).isTrue();
69
70     }
71
72     @AfterEach
73     public void tearDown() throws Exception {
74         janusGraphDao.rollback();
75     }
76
77     @Test
78     public void addGroups_whenContainerHasNoGroups_associateContainerWithGroup() {
79         GroupDefinition g1 = createGroupDefinition("g1");
80         GroupDefinition g2 = createGroupDefinition("g2");
81         Either<List<GroupDefinition>, StorageOperationStatus> createGroups = groupsOperation.addGroups(container, asList(g1, g2));
82         assertThat(createGroups.isLeft()).isTrue();
83
84         ComponentParametersView getGroupsFilter = new ComponentParametersView(true);
85         getGroupsFilter.setIgnoreGroups(false);
86         Component cmptWithGroups = toscaOperationFacade.getToscaElement(CONTAINER_ID, getGroupsFilter).left().value();
87         assertThat(cmptWithGroups.getGroups())
88             .usingElementComparatorOnFields("name", "uniqueId")
89             .containsExactlyInAnyOrder(g1, g2);
90     }
91
92     @Test
93     public void addGroups_whenContainerHasGroups_addTheGivenGroupsToTheGroupsList() {
94         GroupDefinition g1 = createGroupDefinition("g1");
95         GroupDefinition g2 = createGroupDefinition("g2");
96         groupsOperation.addGroups(container, asList(g1, g2)).left().value();
97
98         GroupDefinition g3 = createGroupDefinition("g3");
99         GroupDefinition g4 = createGroupDefinition("g4");
100
101         groupsOperation.addGroups(container, asList(g3, g4)).left().value();
102
103         ComponentParametersView getGroupsFilter = new ComponentParametersView(true);
104         getGroupsFilter.setIgnoreGroups(false);
105         Component cmptWithGroups = toscaOperationFacade.getToscaElement(CONTAINER_ID, getGroupsFilter).left().value();
106         assertThat(cmptWithGroups.getGroups())
107             .usingElementComparatorOnFields("name", "uniqueId")
108             .containsExactlyInAnyOrder(g1, g2, g3, g4);
109
110     }
111
112     private GroupDefinition createGroupDefinition(String id) {
113         GroupDefinition groupDefinition = new GroupDefinition();
114         groupDefinition.setUniqueId(id);
115         groupDefinition.setInvariantName("name" + id);
116         return groupDefinition;
117     }
118
119
120 }