Aharoni, Pavel (pa0916) | ca3cb01 | 2018-10-22 15:29:57 +0300 | [diff] [blame] | 1 | /*-
|
| 2 | * ============LICENSE_START=======================================================
|
| 3 | * OSAM
|
| 4 | * ================================================================================
|
| 5 | * Copyright (C) 2018 AT&T
|
| 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 |
|
| 22 |
|
| 23 | package org.onap.osam.mso;
|
| 24 |
|
| 25 | import com.fasterxml.jackson.core.type.TypeReference;
|
| 26 | import com.fasterxml.jackson.databind.ObjectMapper;
|
| 27 | import org.mockito.ArgumentCaptor;
|
| 28 | import org.mockito.InjectMocks;
|
| 29 | import org.mockito.Mock;
|
| 30 | import org.mockito.Mockito;
|
| 31 | import org.mockito.MockitoAnnotations;
|
| 32 | import org.onap.portalsdk.core.util.SystemProperties;
|
| 33 | import org.onap.osam.controllers.MsoController;
|
| 34 | import org.onap.osam.mso.rest.RequestDetails;
|
| 35 | import org.onap.osam.mso.rest.RequestDetailsWrapper;
|
| 36 | import org.onap.osam.properties.Features;
|
| 37 | import org.springframework.test.context.ContextConfiguration;
|
| 38 | import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
|
| 39 | import org.springframework.test.context.web.WebAppConfiguration;
|
| 40 | import org.testng.Assert;
|
| 41 | import org.testng.annotations.BeforeTest;
|
| 42 | import org.testng.annotations.DataProvider;
|
| 43 | import org.testng.annotations.Test;
|
| 44 | import org.togglz.core.manager.FeatureManager;
|
| 45 |
|
| 46 | import java.io.IOException;
|
| 47 | import java.nio.file.Path;
|
| 48 | import java.nio.file.Paths;
|
| 49 |
|
| 50 | import static org.assertj.core.api.Assertions.assertThat;
|
| 51 | import static org.mockito.Matchers.eq;
|
| 52 | import static org.mockito.Mockito.verify;
|
| 53 | import static org.mockito.Mockito.when;
|
| 54 | import static org.onap.osam.controllers.MsoController.SVC_INSTANCE_ID;
|
| 55 | import static org.onap.osam.mso.MsoBusinessLogicImpl.validateEndpointPath;
|
| 56 |
|
| 57 | @ContextConfiguration(classes = {SystemProperties.class})
|
| 58 | @WebAppConfiguration
|
| 59 | public class MsoBusinessLogicImplTest extends AbstractTestNGSpringContextTests {
|
| 60 |
|
| 61 | private static final String SERVICE_INSTANCE_ID = "1";
|
| 62 | private static final String VNF_INSTANCE_ID = "1";
|
| 63 | private static final String EXPECTED_SCALE_OUT_PATH = "/serviceInstantiation/v7/serviceInstances/1/vnfs/1/vfModules/scaleOut";
|
| 64 | private static final Path PATH_TO_NOT_PROCESSED_SCALE_OUT_REQUEST = Paths.get("src", "test", "resources", "payload_jsons", "scaleOutVfModulePayload.json");
|
| 65 | private static final Path PATH_TO_FINAL_SCALE_OUT_REQUEST = Paths.get("src", "test", "resources", "payload_jsons", "scaleOutVfModulePayloadToMso.json");
|
| 66 | private static final ObjectMapper OBJECT_MAPPER=new ObjectMapper();
|
| 67 |
|
| 68 | @InjectMocks
|
| 69 | private MsoBusinessLogicImpl msoBusinessLogic;
|
| 70 |
|
| 71 | @Mock
|
| 72 | private FeatureManager featureManagerMock;
|
| 73 |
|
| 74 | @Mock
|
| 75 | private MsoInterface msoInterfaceMock;
|
| 76 |
|
| 77 |
|
| 78 | @BeforeTest
|
| 79 | public void initMocks() {
|
| 80 | MockitoAnnotations.initMocks(this);
|
| 81 | }
|
| 82 |
|
| 83 | @Test
|
| 84 | public void validateEndpointPath_endPointIsNotEmptyAndVaild_returnProperty() {
|
| 85 | System.setProperty("TestEnv", "123");
|
| 86 | String foundEndPoint = validateEndpointPath("TestEnv");
|
| 87 | Assert.assertEquals("123", foundEndPoint);
|
| 88 | }
|
| 89 |
|
| 90 | @Test(expectedExceptions = RuntimeException.class)
|
| 91 | public void validateEndpointPath_endPointIsNull_throwRuntimeException() {
|
| 92 | validateEndpointPath("NotExists");
|
| 93 | }
|
| 94 |
|
| 95 | @Test(expectedExceptions = RuntimeException.class)
|
| 96 | public void validateEndpointPath_endPointIsNotEmptyButDoesntExists_throwRuntimeException() {
|
| 97 | System.setProperty("EmptyEndPoint", "");
|
| 98 | validateEndpointPath("EmptyEndPoint");
|
| 99 | }
|
| 100 |
|
| 101 |
|
| 102 | //@Test(dataProvider = "unAssignOrDeleteParams")
|
| 103 | public void deleteSvcInstance_verifyEndPointPathConstructing_unAssignFeatureOffOrUnAssignFlagIsFalse(boolean isAssignFlag, String status) {
|
| 104 | Mockito.reset(msoInterfaceMock);
|
| 105 | String endpoint = validateEndpointPath(isAssignFlag ? MsoProperties.MSO_DELETE_OR_UNASSIGN_REST_API_SVC_INSTANCE : MsoProperties.MSO_REST_API_SVC_INSTANCE);
|
| 106 | RequestDetails requestDetails = new RequestDetails();
|
| 107 |
|
| 108 | when(featureManagerMock.isActive(Features.FLAG_UNASSIGN_SERVICE)).thenReturn(isAssignFlag);
|
| 109 |
|
| 110 | msoBusinessLogic.deleteSvcInstance(requestDetails, "tempId", status);
|
| 111 |
|
| 112 | verify(msoInterfaceMock).deleteSvcInstance(requestDetails, endpoint + "/tempId");
|
| 113 | }
|
| 114 |
|
| 115 | @DataProvider
|
| 116 | public Object[][] unAssignOrDeleteParams() {
|
| 117 | return new Object[][]{
|
| 118 | {Boolean.FALSE, "active"},
|
| 119 | {Boolean.FALSE, "created"},
|
| 120 | {Boolean.TRUE, "Active"},
|
| 121 | {Boolean.TRUE, "unexpected-status"},
|
| 122 | };
|
| 123 | }
|
| 124 |
|
| 125 | //@Test(dataProvider = "unAssignStatus")
|
| 126 | public void deleteSvcInstance_verifyEndPointPathConstructing_unAssignFeatureOnAndUnAssignFlagIsTrue(String status) {
|
| 127 | Mockito.reset(msoInterfaceMock);
|
| 128 | // in the test Features.FLAG_UNASSIGN_SERVICE is active so the endpoint should be MsoProperties.MSO_DELETE_OR_UNASSIGN_REST_API_SVC_INSTANCE
|
| 129 | String endpoint = validateEndpointPath(MsoProperties.MSO_DELETE_OR_UNASSIGN_REST_API_SVC_INSTANCE);
|
| 130 | RequestDetails requestDetails = new RequestDetails();
|
| 131 |
|
| 132 | when(featureManagerMock.isActive(Features.FLAG_UNASSIGN_SERVICE)).thenReturn(true);
|
| 133 |
|
| 134 | msoBusinessLogic.deleteSvcInstance(requestDetails, "tempId", status);
|
| 135 |
|
| 136 | verify(msoInterfaceMock).unassignSvcInstance(requestDetails, endpoint + "/tempId/unassign");
|
| 137 | }
|
| 138 |
|
| 139 | @DataProvider
|
| 140 | public Object[][] unAssignStatus() {
|
| 141 | return new Object[][]{
|
| 142 | {"Created"},
|
| 143 | {"Pendingdelete"},
|
| 144 | {"pending-Delete"},
|
| 145 | {"Assigned"}
|
| 146 | };
|
| 147 | }
|
| 148 |
|
| 149 | @Test
|
| 150 | public void deleteVnf_verifyEndPointPathConstructing() {
|
| 151 | String endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_VNF_INSTANCE);
|
| 152 | RequestDetails requestDetails = new RequestDetails();
|
| 153 |
|
| 154 | String vnf_endpoint = endpoint.replaceFirst(SVC_INSTANCE_ID, "serviceInstanceTempId");
|
| 155 |
|
| 156 | msoBusinessLogic.deleteVnf(requestDetails, "serviceInstanceTempId", "vnfInstanceTempId");
|
| 157 | verify(msoInterfaceMock).deleteVnf(requestDetails, vnf_endpoint + "/vnfInstanceTempId");
|
| 158 | }
|
| 159 |
|
| 160 | @Test
|
| 161 | public void deleteVfModule_verifyEndPointPathConstructing() {
|
| 162 | String endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE);
|
| 163 | RequestDetails requestDetails = new RequestDetails();
|
| 164 |
|
| 165 | String vf__modules_endpoint = endpoint.replaceFirst(SVC_INSTANCE_ID, "serviceInstanceTempId").replaceFirst(MsoController.VNF_INSTANCE_ID, "vnfInstanceTempId");
|
| 166 |
|
| 167 | msoBusinessLogic.deleteVfModule(requestDetails, "serviceInstanceTempId", "vnfInstanceTempId", "vfModuleTempId");
|
| 168 | verify(msoInterfaceMock).deleteVfModule(requestDetails, vf__modules_endpoint + "/vfModuleTempId");
|
| 169 | }
|
| 170 |
|
| 171 |
|
| 172 | //@Test - fails after code cutting - TBD later
|
| 173 | public void shouldSendProperScaleOutRequest() throws IOException {
|
| 174 | ArgumentCaptor<RequestDetailsWrapper> requestDetailsWrapperArgumentCaptor = ArgumentCaptor.forClass(RequestDetailsWrapper.class);
|
| 175 | RequestDetails requestDetails = getScaleOutRequest();
|
| 176 | RequestDetailsWrapper expectedRequestWrapper = getExpectedRequestWrapper();
|
| 177 |
|
| 178 | msoBusinessLogic.scaleOutVfModuleInstance(requestDetails, SERVICE_INSTANCE_ID, VNF_INSTANCE_ID);
|
| 179 |
|
| 180 | verify(msoInterfaceMock).scaleOutVFModuleInstance(requestDetailsWrapperArgumentCaptor.capture(), eq(EXPECTED_SCALE_OUT_PATH));
|
| 181 | RequestDetailsWrapper actual = requestDetailsWrapperArgumentCaptor.getAllValues().get(0);
|
| 182 |
|
| 183 | assertThat(expectedRequestWrapper.requestDetails).isEqualTo(actual.requestDetails);
|
| 184 | }
|
| 185 |
|
| 186 | private RequestDetails getScaleOutRequest() throws IOException {
|
| 187 | return OBJECT_MAPPER.readValue(PATH_TO_NOT_PROCESSED_SCALE_OUT_REQUEST.toFile(), RequestDetails.class);
|
| 188 | }
|
| 189 |
|
| 190 | private RequestDetailsWrapper getExpectedRequestWrapper() throws IOException {
|
| 191 | return OBJECT_MAPPER.readValue(PATH_TO_FINAL_SCALE_OUT_REQUEST.toFile(), new TypeReference<RequestDetailsWrapper>() {
|
| 192 | });
|
| 193 | }
|
| 194 | }
|
| 195 |
|