Added oparent to sdc main
[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.After;
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.openecomp.sdc.be.dao.config.JanusGraphSpringConfig;
34 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
35 import org.openecomp.sdc.be.dao.jsongraph.HealingJanusGraphDao;
36 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
37 import org.openecomp.sdc.be.model.Component;
38 import org.openecomp.sdc.be.model.ComponentParametersView;
39 import org.openecomp.sdc.be.model.GroupDefinition;
40 import org.openecomp.sdc.be.model.ModelTestBase;
41 import org.openecomp.sdc.be.model.Resource;
42 import org.openecomp.sdc.be.model.config.ModelOperationsSpringConfig;
43 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.test.context.ContextConfiguration;
46 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
47
48
49 @RunWith(SpringJUnit4ClassRunner.class)
50 @ContextConfiguration(classes = {JanusGraphSpringConfig.class, ModelOperationsSpringConfig.class})
51 public class GroupsOperationTest extends ModelTestBase {
52
53     @Autowired
54     private GroupsOperation groupsOperation;
55
56     @Autowired
57     HealingJanusGraphDao janusGraphDao;
58
59     @Autowired
60     private ToscaOperationFacade toscaOperationFacade;
61     private Component container;
62
63     @BeforeClass
64     public static void initClass() {
65         ModelTestBase.init();
66     }
67
68     @Before
69     public void setUp() throws Exception {
70         container = new Resource();
71         container.setUniqueId(CONTAINER_ID);
72         Either<GraphVertex, JanusGraphOperationStatus> createdCmpt = janusGraphDao.createVertex(createBasicContainerGraphVertex());
73         assertThat(createdCmpt.isLeft()).isTrue();
74
75     }
76
77     @After
78     public void tearDown() throws Exception {
79         janusGraphDao.rollback();
80     }
81
82     @Test
83     public void addGroups_whenContainerHasNoGroups_associateContainerWithGroup() {
84         GroupDefinition g1 = createGroupDefinition("g1");
85         GroupDefinition g2 = createGroupDefinition("g2");
86         Either<List<GroupDefinition>, StorageOperationStatus> createGroups = groupsOperation.addGroups(container, asList(g1, g2));
87         assertThat(createGroups.isLeft()).isTrue();
88
89         ComponentParametersView getGroupsFilter = new ComponentParametersView(true);
90         getGroupsFilter.setIgnoreGroups(false);
91         Component cmptWithGroups = toscaOperationFacade.getToscaElement(CONTAINER_ID, getGroupsFilter).left().value();
92         assertThat(cmptWithGroups.getGroups())
93                 .usingElementComparatorOnFields("name", "uniqueId")
94                 .containsExactlyInAnyOrder(g1, g2);
95     }
96
97     @Test
98     public void addGroups_whenContainerHasGroups_addTheGivenGroupsToTheGroupsList() {
99         GroupDefinition g1 = createGroupDefinition("g1");
100         GroupDefinition g2 = createGroupDefinition("g2");
101         groupsOperation.addGroups(container, asList(g1, g2)).left().value();
102
103         GroupDefinition g3 = createGroupDefinition("g3");
104         GroupDefinition g4 = createGroupDefinition("g4");
105
106         groupsOperation.addGroups(container, asList(g3, g4)).left().value();
107
108         ComponentParametersView getGroupsFilter = new ComponentParametersView(true);
109         getGroupsFilter.setIgnoreGroups(false);
110         Component cmptWithGroups = toscaOperationFacade.getToscaElement(CONTAINER_ID, getGroupsFilter).left().value();
111         assertThat(cmptWithGroups.getGroups())
112                 .usingElementComparatorOnFields("name", "uniqueId")
113                 .containsExactlyInAnyOrder(g1, g2, g3, g4);
114
115     }
116
117     private GroupDefinition createGroupDefinition(String id) {
118         GroupDefinition groupDefinition = new GroupDefinition();
119         groupDefinition.setUniqueId(id);
120         groupDefinition.setName("name" + id);
121         return groupDefinition;
122     }
123
124
125 }