/* * - * * ============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.common.datastructure.AuditingFieldsKey; 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 userId; private User user; private String distributionId; 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(); userId = "userId"; distributionId = "did"; serviceId = "serviceId"; when(userValidations.validateUserExists(Mockito.eq(userId))) .thenReturn(user); } @Test public void testGetListOfDistribution_givenInvalidDistributionId_thenReturnsError() { when(cassandraDao.getListOfDistributionStatuses(distributionId)) .thenReturn(Either.right(ActionStatus.DISTRIBUTION_NOT_FOUND)); assertTrue(businessLogic.getListOfDistributionStatus(distributionId, userId).isRight()); } @Test public void testGetListOfDistribution_givenValidDistributionId_thenReturnsSuccessful() { List distributionEvents = new ArrayList<>(); DistributionStatusEvent event = new DistributionStatusEvent(); distributionEvents.add(event); when(cassandraDao.getListOfDistributionStatuses(distributionId)) .thenReturn(Either.left(distributionEvents)); Either result = businessLogic.getListOfDistributionStatus(distributionId, userId); 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, userId).isRight()); } @Test public void testGetDistributionServiceStatus_givenValidServiceId_thenReturnsSuccessful() { List serviceEvents = new ArrayList<>(); AuditingGenericEvent event1 = new AuditingGenericEvent(); AuditingGenericEvent event2 = new AuditingGenericEvent(); Map event1Fields = new HashMap<>(); Map event2Fields = new HashMap<>(); event1Fields.put("DID", "event1"); event1Fields.put("ACTION", "DRequest"); event1Fields.put("STATUS", "200"); event1Fields.put("MODIFIER", userId); event2Fields.put("DID", "event2"); event2Fields.put("ACTION", "DNotify"); event2Fields.put("STATUS", "200"); event2Fields.put("MODIFIER", userId); event1.setFields(event1Fields); event2.setFields(event2Fields); serviceEvents.add(event1); serviceEvents.add(event2); when(cassandraDao.getServiceDistributionStatusesList(serviceId)) .thenReturn(Either.left(serviceEvents)); assertTrue(businessLogic.getListOfDistributionServiceStatus(serviceId, userId).isLeft()); } @Test public void testGetDistributionServiceStatus_givenLongTimeStamp_thenReturnsSuccessful() { Map event1Fields = new HashMap<>(); event1Fields.put("DID", "event1"); event1Fields.put("ACTION", "DRequest"); event1Fields.put("STATUS", "200"); event1Fields.put("MODIFIER", userId); event1Fields.put(AuditingFieldsKey.AUDIT_TIMESTAMP.getDisplayName(), "2024-04-05 07:29:10.735 UTC"); Map event2Fields = new HashMap<>(); event2Fields.put("DID", "event2"); event2Fields.put("ACTION", "DNotify"); event2Fields.put("STATUS", "200"); event2Fields.put("MODIFIER", userId); event1Fields.put(AuditingFieldsKey.AUDIT_TIMESTAMP.getDisplayName(), 1712645750L); List serviceEvents = new ArrayList<>(); AuditingGenericEvent event1 = new AuditingGenericEvent(); AuditingGenericEvent event2 = new AuditingGenericEvent(); event1.setFields(event1Fields); event2.setFields(event2Fields); serviceEvents.add(event1); serviceEvents.add(event2); when(cassandraDao.getServiceDistributionStatusesList(serviceId)) .thenReturn(Either.left(serviceEvents)); assertTrue(businessLogic.getListOfDistributionServiceStatus(serviceId, userId).isLeft()); } }