5f0282b7d68ef1b83c756bb29678e1476d54ea3d
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / path / ForwardingPathBusinessLogicTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.be.components.path;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22
23 import com.google.common.collect.Lists;
24 import com.google.common.collect.Sets;
25 import fj.data.Either;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Set;
29 import org.junit.jupiter.api.Assertions;
30 import org.junit.jupiter.api.BeforeAll;
31 import org.junit.jupiter.api.Test;
32 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
33 import org.openecomp.sdc.be.config.ConfigurationManager;
34 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
35 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
36 import org.openecomp.sdc.be.datatypes.enums.ComponentFieldsEnum;
37 import org.openecomp.sdc.be.model.Service;
38 import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
39 import org.openecomp.sdc.be.ui.model.UiServiceDataTransfer;
40 import org.openecomp.sdc.common.impl.ExternalConfiguration;
41 import org.openecomp.sdc.common.impl.FSConfigurationSource;
42 import org.openecomp.sdc.exception.ResponseFormat;
43 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
44
45 @SpringJUnitConfig(locations = "classpath:paths/path-context.xml")
46 public class ForwardingPathBusinessLogicTest extends BaseForwardingPathTest {
47
48     @BeforeAll
49     private static void setup() {
50         configurationManager =
51             new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
52     }
53
54     @Test
55     public void shouldFailToUpdateForwardingPathSincePathDoesNotExist() {
56         Assertions.assertThrows(ComponentException.class, () -> {
57             Service service = initForwardPath();
58             bl.updateForwardingPath(FORWARDING_PATH_ID, service, user, true);
59         });
60     }
61
62     @Test
63     public void shouldFailToDeleteForwardingPathSincePathDoesNotExist() {
64         Assertions.assertThrows(ComponentException.class, () -> {
65             initForwardPath();
66             bl.deleteForwardingPaths("delete_forward_test", Sets.newHashSet(FORWARDING_PATH_ID), user, true);
67         });
68     }
69
70     @Test
71     public void shouldSucceedCreateAndDeleteForwardingPath() {
72         Service createdService = createService();
73         Service service = initForwardPath();
74         assertNotNull(service);
75         Service serviceResponseFormatEither = bl.createForwardingPath(createdService.getUniqueId(), service, user, true);
76         assertTrue(serviceResponseFormatEither != null);
77         Map<String, ForwardingPathDataDefinition> forwardingPathsMap = serviceResponseFormatEither.getForwardingPaths();
78         Set<String> pathIds = forwardingPathsMap.keySet();
79         assertEquals(1, pathIds.size());
80         String toscaResourceName = forwardingPathsMap.values().iterator().next().getToscaResourceName();
81
82         // should return the created path
83         Either<UiComponentDataTransfer, ResponseFormat> uiResaponse = bl.getComponentDataFilteredByParams(createdService.getUniqueId(), user,
84             Lists.newArrayList(ComponentFieldsEnum.FORWARDING_PATHS.getValue()));
85         assertTrue(uiResaponse.isLeft());
86         UiServiceDataTransfer uiServiceDataTransfer = (UiServiceDataTransfer) uiResaponse.left().value();
87         Map<String, ForwardingPathDataDefinition> forwardingPaths = uiServiceDataTransfer.getForwardingPaths();
88         assertEquals(forwardingPaths.keySet(), pathIds);
89         Map<String, ForwardingPathDataDefinition> updatedForwardingPaths = new HashMap<>(forwardingPaths);
90         String newProtocol = "https";
91         ForwardingPathDataDefinition forwardingPathDataDefinition = updatedForwardingPaths.values().stream().findAny().get();
92         assertEquals(forwardingPathDataDefinition.getProtocol(), HTTP_PROTOCOL);
93         assertEquals(toscaResourceName, forwardingPathDataDefinition.getToscaResourceName());
94         ForwardingPathDataDefinition forwardingPathDataDefinitionUpdate = updatedForwardingPaths.values().iterator().next();
95         // updated values
96         forwardingPathDataDefinitionUpdate.setProtocol(newProtocol);
97         forwardingPathDataDefinitionUpdate.setPathElements(new ListDataDefinition<>());
98
99         // should update value
100         service.getForwardingPaths().clear();
101         service.getForwardingPaths().put(forwardingPathDataDefinitionUpdate.getUniqueId(), forwardingPathDataDefinitionUpdate);
102         serviceResponseFormatEither = bl.updateForwardingPath(createdService.getUniqueId(), service, user, true);
103         assertTrue(serviceResponseFormatEither != null);
104
105         // make sure changes were applied
106         uiResaponse = bl.getComponentDataFilteredByParams(createdService.getUniqueId(), user,
107             Lists.newArrayList(ComponentFieldsEnum.FORWARDING_PATHS.getValue()));
108         assertTrue(uiResaponse.isLeft());
109         uiServiceDataTransfer = (UiServiceDataTransfer) uiResaponse.left().value();
110         Map<String, ForwardingPathDataDefinition> forwardingPathsUpdated = uiServiceDataTransfer.getForwardingPaths();
111         ForwardingPathDataDefinition updatedData = forwardingPathsUpdated.values().iterator().next();
112         assertEquals(newProtocol, updatedData.getProtocol());
113         assertTrue(updatedData.getPathElements().isEmpty());
114
115         Service createdData = serviceResponseFormatEither;
116         Set<String> paths = createdData.getForwardingPaths().keySet();
117         Set<String> setResponseFormatEither = bl.deleteForwardingPaths(createdService.getUniqueId(), paths, user, true);
118         assertTrue(setResponseFormatEither != null);
119
120         // nothing to return now
121         uiResaponse = bl.getComponentDataFilteredByParams(createdService.getUniqueId(), user,
122             Lists.newArrayList(ComponentFieldsEnum.COMPONENT_INSTANCES.getValue(), ComponentFieldsEnum.FORWARDING_PATHS.getValue()));
123         assertTrue(uiResaponse.isLeft());
124         uiServiceDataTransfer = (UiServiceDataTransfer) uiResaponse.left().value();
125         forwardingPaths = uiServiceDataTransfer.getForwardingPaths();
126         assertTrue(forwardingPaths == null || forwardingPaths.isEmpty());
127
128     }
129
130 }