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