aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/DistributionMonitoringBusinessLogicTest.java
blob: 7b111ba52fb57ec532a5c11436755a077698e452 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * -
 *  * ============LICENSE_START=======================================================
 *  *  Copyright (C) 2019  Nordix Foundation.
 *  * ================================================================================
 *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  * you may not use this file except in compliance with the License.
 *  * You may obtain a copy of the License at
 *  *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 *  *
 *  * SPDX-License-Identifier: Apache-2.0
 *  * ============LICENSE_END=========================================================
 *
 */

package org.openecomp.sdc.be.components.impl;

import fj.data.Either;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.openecomp.sdc.be.components.validation.UserValidations;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.dao.cassandra.AuditCassandraDao;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.info.DistributionStatusListResponse;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.resources.data.auditing.AuditingGenericEvent;
import org.openecomp.sdc.be.resources.data.auditing.DistributionStatusEvent;
import org.openecomp.sdc.exception.ResponseFormat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

public class DistributionMonitoringBusinessLogicTest extends BaseBusinessLogicMock {

    private String uId;
    private User user;
    private String ditributionId;
    private String serviceId;

    private DistributionMonitoringBusinessLogic businessLogic;

    @Mock
    private AuditCassandraDao cassandraDao;

    @Mock
    private UserValidations userValidations;

    @Mock
    private ComponentsUtils componentsUtils;

    @Before
    public void setUp() {
        MockitoAnnotations.openMocks(this);
        businessLogic = new DistributionMonitoringBusinessLogic(elementDao, groupOperation, groupInstanceOperation,
            groupTypeOperation, interfaceOperation, interfaceLifecycleTypeOperation,
            cassandraDao, artifactToscaOperation);
        businessLogic.setUserValidations(userValidations);
        businessLogic.setComponentsUtils(componentsUtils);

        user = new User();
        uId = "userId";
        ditributionId = "did";
        serviceId = "serviceId";

        when(userValidations.validateUserExists(Mockito.eq(uId)))
                .thenReturn(user);
    }

    @Test
    public void testGetListOfDistribution_givenInvalidDistributionId_thenReturnsError() {
        when(cassandraDao.getListOfDistributionStatuses(ditributionId))
                .thenReturn(Either.right(ActionStatus.DISTRIBUTION_NOT_FOUND));

        assertTrue(businessLogic.getListOfDistributionStatus(ditributionId, uId).isRight());
    }

    @Test
    public void testGetListOfDistribution_givenValidDistributionId_thenReturnsSuccessful() {

        List<DistributionStatusEvent> distributionEvents = new ArrayList<>();
        DistributionStatusEvent event = new DistributionStatusEvent();
        distributionEvents.add(event);

        when(cassandraDao.getListOfDistributionStatuses(ditributionId))
                .thenReturn(Either.left(distributionEvents));

        Either<DistributionStatusListResponse, ResponseFormat> result = businessLogic.getListOfDistributionStatus(ditributionId, uId);

        assertTrue(result.isLeft());
        assertEquals(1, result.left().value().getDistributionStatusList().size());
    }

    @Test
    public void testGetDistributionServiceStatus_givenInvalidServiceIdId_thenReturnsError() {

        when(cassandraDao.getServiceDistributionStatusesList(serviceId))
                .thenReturn(Either.right(ActionStatus.DISTRIBUTION_NOT_FOUND));
        assertTrue(businessLogic.getListOfDistributionServiceStatus(serviceId, uId).isRight());
    }

    @Test
    public void testGetDistributionServiceStatus_givenValidServiceId_thenReturnsSuccessful() {

        List<AuditingGenericEvent> serviceEvents = new ArrayList<>();
        AuditingGenericEvent event1 = new AuditingGenericEvent();
        AuditingGenericEvent event2 = new AuditingGenericEvent();

        Map<String, Object> event1Fields = new HashMap<>();
        Map<String, Object> event2Fields = new HashMap<>();

        event1Fields.put("DID", "event1");
        event1Fields.put("ACTION", "DRequest");
        event1Fields.put("STATUS", "200");
        event1Fields.put("MODIFIER", uId);

        event2Fields.put("DID", "event2");
        event2Fields.put("ACTION", "DNotify");
        event2Fields.put("STATUS", "200");
        event2Fields.put("MODIFIER", uId);

        event1.setFields(event1Fields);
        event2.setFields(event2Fields);

        serviceEvents.add(event1);
        serviceEvents.add(event2);

        when(cassandraDao.getServiceDistributionStatusesList(serviceId))
                .thenReturn(Either.left(serviceEvents));

        assertTrue(businessLogic.getListOfDistributionServiceStatus(serviceId, uId).isLeft());
    }
}