Catalog alignment
[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.times;
48 import static org.mockito.Mockito.verify;
49 import static org.mockito.Mockito.when;
50
51 @RunWith(MockitoJUnitRunner.class)
52 public class ArchiveBusinessLogicTest {
53     @Mock
54     private ComponentsUtils componentsUtils;
55     @Mock
56     private ToscaOperationFacade toscaOperationFacade;
57     @Mock
58     private User user;
59     @Mock
60     private ResponseFormat responseFormat;
61     @Mock
62     private Component component;
63
64     @InjectMocks
65     private ArchiveBusinessLogic archiveBusinessLogic;
66
67     @Test
68     public void auditLastComponentVersionOnlyAndIgnorePreviousVersions() {
69         List<String> archivedCompIds = Arrays.asList("1", "2", "3", "4", "5");
70         when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class)))
71                 .thenReturn(Either.left(component));
72         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
73         when(component.getUUID())
74                 .thenReturn("1")
75                 .thenReturn("1")
76                 .thenReturn("2")
77                 .thenReturn("3")
78                 .thenReturn("2");
79
80         archiveBusinessLogic.auditAction(ArchiveOperation.Action.ARCHIVE, archivedCompIds, user, ComponentTypeEnum.RESOURCE_PARAM_NAME);
81         verify(componentsUtils, times(3)).auditComponentAdmin(eq(responseFormat), eq(user), eq(component),
82                 eq(AuditingActionEnum.ARCHIVE_COMPONENT), eq(ComponentTypeEnum.RESOURCE), any(String.class));
83     }
84
85
86     @Test
87     public void auditLastComponentVersionOnly() {
88         List<String> archivedCompIds = Arrays.asList("1", "2", "3", "4", "5");
89         when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class)))
90                 .thenReturn(Either.left(component));
91         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
92         when(component.getUUID())
93                 .thenReturn("1")
94                 .thenReturn("2")
95                 .thenReturn("4")
96                 .thenReturn("5")
97                 .thenReturn("3");
98
99         archiveBusinessLogic.auditAction(ArchiveOperation.Action.RESTORE, archivedCompIds, user, ComponentTypeEnum.RESOURCE_PARAM_NAME);
100         verify(componentsUtils, times(5)).auditComponentAdmin(eq(responseFormat), eq(user), eq(component),
101                 eq(AuditingActionEnum.RESTORE_COMPONENT), eq(ComponentTypeEnum.RESOURCE), any(String.class));
102     }
103
104
105     @Test
106     public void noAuditDoneForEmptyList() {
107         List<String> archivedCompIds = ImmutableList.of();
108         archiveBusinessLogic.auditAction(ArchiveOperation.Action.RESTORE, archivedCompIds, user, ComponentTypeEnum.RESOURCE_PARAM_NAME);
109         verify(componentsUtils, times(0)).auditComponentAdmin(any(ResponseFormat.class), any(User.class), any(Component.class),
110                 any(AuditingActionEnum.class), any(ComponentTypeEnum.class), any(String.class));
111     }
112
113
114     @Test
115     public void noAuditOnErrorGetElementResponse() {
116         List<String> archivedCompIds = Arrays.asList("1", "2", "3", "4", "5");
117         when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class)))
118                 .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
119         archiveBusinessLogic.auditAction(ArchiveOperation.Action.RESTORE, archivedCompIds, user, ComponentTypeEnum.RESOURCE_PARAM_NAME);
120         verify(componentsUtils, times(0)).auditComponentAdmin(any(ResponseFormat.class), any(User.class), any(Component.class),
121                 any(AuditingActionEnum.class), any(ComponentTypeEnum.class), any(String.class));
122     }
123
124
125
126 }