2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.be.components.path;
19 import com.google.common.collect.Lists;
20 import com.google.common.collect.Sets;
21 import fj.data.Either;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
25 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
26 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
27 import org.openecomp.sdc.be.datatypes.enums.ComponentFieldsEnum;
28 import org.openecomp.sdc.be.model.Service;
29 import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
30 import org.openecomp.sdc.be.ui.model.UiServiceDataTransfer;
31 import org.openecomp.sdc.exception.ResponseFormat;
32 import org.springframework.test.context.ContextConfiguration;
33 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
35 import java.util.HashMap;
39 import static org.junit.Assert.assertEquals;
40 import static org.junit.Assert.assertNotNull;
41 import static org.junit.Assert.assertTrue;
43 @RunWith(SpringJUnit4ClassRunner.class)
44 @ContextConfiguration(locations = "/paths/path-context.xml")
45 public class ForwardingPathBusinessLogicTest extends BaseForwardingPathTest {
47 @Test(expected = ComponentException.class)
48 public void shouldFailToUpdateForwardingPathSincePathDoesNotExist() {
49 Service service = initForwardPath();
50 bl.updateForwardingPath(FORWARDING_PATH_ID, service, user, true);
53 @Test(expected = ComponentException.class)
54 public void shouldFailToDeleteForwardingPathSincePathDoesNotExist() {
56 bl.deleteForwardingPaths("delete_forward_test", Sets.newHashSet(FORWARDING_PATH_ID), user, true);
60 public void shouldSucceedCreateAndDeleteForwardingPath() {
61 Service createdService = createService();
62 Service service = initForwardPath();
63 assertNotNull(service);
64 Service serviceResponseFormatEither = bl.createForwardingPath(createdService.getUniqueId(), service, user, true);
65 assertTrue(serviceResponseFormatEither != null);
66 Map<String, ForwardingPathDataDefinition> forwardingPathsMap = serviceResponseFormatEither.getForwardingPaths();
67 Set<String> pathIds = forwardingPathsMap.keySet();
68 assertEquals(1, pathIds.size());
69 String toscaResourceName = forwardingPathsMap.values().iterator().next().getToscaResourceName();
71 // should return the created path
72 Either<UiComponentDataTransfer, ResponseFormat> uiResaponse = bl.getComponentDataFilteredByParams(createdService.getUniqueId(), user, Lists.newArrayList(ComponentFieldsEnum.FORWARDING_PATHS.getValue()));
73 assertTrue(uiResaponse.isLeft());
74 UiServiceDataTransfer uiServiceDataTransfer = (UiServiceDataTransfer) uiResaponse.left().value();
75 Map<String, ForwardingPathDataDefinition> forwardingPaths = uiServiceDataTransfer.getForwardingPaths();
76 assertEquals(forwardingPaths.keySet(), pathIds);
77 Map<String, ForwardingPathDataDefinition> updatedForwardingPaths = new HashMap<>(forwardingPaths);
78 String newProtocol = "https";
79 ForwardingPathDataDefinition forwardingPathDataDefinition = updatedForwardingPaths.values().stream().findAny().get();
80 assertEquals(forwardingPathDataDefinition.getProtocol(), HTTP_PROTOCOL);
81 assertEquals(toscaResourceName, forwardingPathDataDefinition.getToscaResourceName());
82 ForwardingPathDataDefinition forwardingPathDataDefinitionUpdate = updatedForwardingPaths.values().iterator().next();
84 forwardingPathDataDefinitionUpdate.setProtocol(newProtocol);
85 forwardingPathDataDefinitionUpdate.setPathElements(new ListDataDefinition<>());
87 // should update value
88 service.getForwardingPaths().clear();
89 service.getForwardingPaths().put(forwardingPathDataDefinitionUpdate.getUniqueId(), forwardingPathDataDefinitionUpdate);
90 serviceResponseFormatEither = bl.updateForwardingPath(createdService.getUniqueId(), service, user, true);
91 assertTrue(serviceResponseFormatEither != null);
93 // make sure changes were applied
94 uiResaponse = bl.getComponentDataFilteredByParams(createdService.getUniqueId(), user, Lists.newArrayList(ComponentFieldsEnum.FORWARDING_PATHS.getValue()));
95 assertTrue(uiResaponse.isLeft());
96 uiServiceDataTransfer = (UiServiceDataTransfer) uiResaponse.left().value();
97 Map<String, ForwardingPathDataDefinition> forwardingPathsUpdated = uiServiceDataTransfer.getForwardingPaths();
98 ForwardingPathDataDefinition updatedData = forwardingPathsUpdated.values().iterator().next();
99 assertEquals(newProtocol, updatedData.getProtocol());
100 assertTrue(updatedData.getPathElements().isEmpty());
102 Service createdData = serviceResponseFormatEither;
103 Set<String> paths = createdData.getForwardingPaths().keySet();
104 Set<String> setResponseFormatEither = bl.deleteForwardingPaths(createdService.getUniqueId(), paths, user, true);
105 assertTrue(setResponseFormatEither != null);
107 // nothing to return now
108 uiResaponse = bl.getComponentDataFilteredByParams(createdService.getUniqueId(), user, Lists.newArrayList(ComponentFieldsEnum.COMPONENT_INSTANCES.getValue(),ComponentFieldsEnum.FORWARDING_PATHS.getValue()));
109 assertTrue(uiResaponse.isLeft());
110 uiServiceDataTransfer = (UiServiceDataTransfer) uiResaponse.left().value();
111 forwardingPaths = uiServiceDataTransfer.getForwardingPaths();
112 assertTrue(forwardingPaths == null || forwardingPaths.isEmpty());