Added oparent to sdc main
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / ArchiveBusinessLogicTest.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.components.impl;
22
23 import com.google.common.collect.ImmutableList;
24 import fj.data.Either;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.openecomp.sdc.be.dao.api.ActionStatus;
31 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
32 import org.openecomp.sdc.be.impl.ComponentsUtils;
33 import org.openecomp.sdc.be.model.Component;
34 import org.openecomp.sdc.be.model.ComponentParametersView;
35 import org.openecomp.sdc.be.model.User;
36 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArchiveOperation;
37 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
38 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
39 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
40 import org.openecomp.sdc.exception.ResponseFormat;
41
42 import java.util.Arrays;
43 import java.util.List;
44
45 import static org.mockito.ArgumentMatchers.any;
46 import static org.mockito.ArgumentMatchers.eq;
47 import static org.mockito.Mockito.*;
48
49 @RunWith(MockitoJUnitRunner.class)
50 public class ArchiveBusinessLogicTest {
51     @Mock
52     private ComponentsUtils componentsUtils;
53     @Mock
54     private ToscaOperationFacade toscaOperationFacade;
55     @Mock
56     private User user;
57     @Mock
58     private ResponseFormat responseFormat;
59     @Mock
60     private Component component;
61
62     @InjectMocks
63     private ArchiveBusinessLogic archiveBusinessLogic;
64
65     @Test
66     public void auditLastComponentVersionOnlyAndIgnorePreviousVersions() {
67         List<String> archivedCompIds = Arrays.asList("1", "2", "3", "4", "5");
68         when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class)))
69                 .thenReturn(Either.left(component));
70         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
71         when(component.getUUID())
72                 .thenReturn("1")
73                 .thenReturn("1")
74                 .thenReturn("2")
75                 .thenReturn("3")
76                 .thenReturn("2");
77
78         archiveBusinessLogic.auditAction(ArchiveOperation.Action.ARCHIVE, archivedCompIds, user, ComponentTypeEnum.RESOURCE_PARAM_NAME);
79         verify(componentsUtils, times(3)).auditComponentAdmin(eq(responseFormat), eq(user), eq(component),
80                 eq(AuditingActionEnum.ARCHIVE_COMPONENT), eq(ComponentTypeEnum.RESOURCE), any(String.class));
81     }
82
83
84     @Test
85     public void auditLastComponentVersionOnly() {
86         List<String> archivedCompIds = Arrays.asList("1", "2", "3", "4", "5");
87         when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class)))
88                 .thenReturn(Either.left(component));
89         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
90         when(component.getUUID())
91                 .thenReturn("1")
92                 .thenReturn("2")
93                 .thenReturn("4")
94                 .thenReturn("5")
95                 .thenReturn("3");
96
97         archiveBusinessLogic.auditAction(ArchiveOperation.Action.RESTORE, archivedCompIds, user, ComponentTypeEnum.RESOURCE_PARAM_NAME);
98         verify(componentsUtils, times(5)).auditComponentAdmin(eq(responseFormat), eq(user), eq(component),
99                 eq(AuditingActionEnum.RESTORE_COMPONENT), eq(ComponentTypeEnum.RESOURCE), any(String.class));
100     }
101
102
103     @Test
104     public void noAuditDoneForEmptyList() {
105         List<String> archivedCompIds = ImmutableList.of();
106         archiveBusinessLogic.auditAction(ArchiveOperation.Action.RESTORE, archivedCompIds, user, ComponentTypeEnum.RESOURCE_PARAM_NAME);
107         verify(componentsUtils, times(0)).auditComponentAdmin(any(ResponseFormat.class), any(User.class), any(Component.class),
108                 any(AuditingActionEnum.class), any(ComponentTypeEnum.class), any(String.class));
109     }
110
111
112     @Test
113     public void noAuditOnErrorGetElementResponse() {
114         List<String> archivedCompIds = Arrays.asList("1", "2", "3", "4", "5");
115         when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class)))
116                 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
117         archiveBusinessLogic.auditAction(ArchiveOperation.Action.RESTORE, archivedCompIds, user, ComponentTypeEnum.RESOURCE_PARAM_NAME);
118         verify(componentsUtils, times(0)).auditComponentAdmin(any(ResponseFormat.class), any(User.class), any(Component.class),
119                 any(AuditingActionEnum.class), any(ComponentTypeEnum.class), any(String.class));
120     }
121
122
123
124 }