Fix test cases failing incorrectly
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / path / ForwardingPathValidatorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
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 package org.openecomp.sdc.be.components.path;
22
23
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.when;
26
27 import com.google.common.collect.Sets;
28 import fj.data.Either;
29 import java.util.Collection;
30 import java.util.Set;
31 import org.junit.jupiter.api.Assertions;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.MockitoAnnotations;
38 import org.openecomp.sdc.be.components.impl.ResponseFormatManager;
39 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
40 import org.openecomp.sdc.be.config.ConfigurationManager;
41 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
42 import org.openecomp.sdc.be.model.Component;
43 import org.openecomp.sdc.be.model.ComponentParametersView;
44 import org.openecomp.sdc.be.model.Service;
45 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
46 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
47 import org.openecomp.sdc.common.impl.ExternalConfiguration;
48 import org.openecomp.sdc.common.impl.FSConfigurationSource;
49 import org.openecomp.sdc.exception.ResponseFormat;
50
51 public class ForwardingPathValidatorTest implements ForwardingPathTestUtils {
52
53     private static final String SERVICE_ID = "serviceid1";
54     ResponseFormatManager mock;
55     @Mock
56     ToscaOperationFacade toscaOperationFacade;
57     @InjectMocks
58     ForwardingPathValidationUtilTest test = new ForwardingPathValidationUtilTest();
59     private Service service = (Service) getToscaFullElement().left().value();
60
61     @BeforeEach
62     public void init() {
63         MockitoAnnotations.initMocks(this);
64         mock = Mockito.mock(ResponseFormatManager.class);
65         when(toscaOperationFacade.getToscaElement(any(), any(ComponentParametersView.class))).thenReturn(Either.left(service));
66         when(mock.getResponseFormat(any())).thenReturn(new ResponseFormat());
67         when(mock.getResponseFormat(any(), any())).thenReturn(new ResponseFormat());
68         when(mock.getResponseFormat(any(), any(), any())).thenReturn(new ResponseFormat());
69
70         new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
71     }
72
73     @Test
74     public void testValidForwardingPathName() {
75         Collection<ForwardingPathDataDefinition> paths = createData("pathName", "http", "8285", "pathName");
76         test.validateForwardingPaths(paths, SERVICE_ID, false);
77     }
78
79     @Test
80     public void testEmptyForwardingPathName() {
81         Assertions.assertThrows(ComponentException.class, () -> {
82             Collection<ForwardingPathDataDefinition> paths = createData("", "protocol", "8285", "name1");
83             test.validateForwardingPaths(paths, SERVICE_ID, false);
84         });
85     }
86
87     @Test
88     public void testLongForwardingPathName() {
89         Assertions.assertThrows(ComponentException.class, () -> {
90             String pathName = "Failed to execute goal on project catalog-be: Could not resolve dependencies for project \n" +
91                 "org.openecomp.sdc:catalog-be:war:1.1.0-SNAPSHOT: Failed to collect dependencies at \n" +
92                 "org.openecomp.sdc.common:openecomp-sdc-artifact-generator-api:jar:1802.0.1.167: ";
93             Collection<ForwardingPathDataDefinition> paths = createData(pathName,
94                 "http", "port", "name1");
95             test.validateForwardingPaths(paths, SERVICE_ID, false);
96         });
97     }
98
99     @Test
100     public void testUniqueForwardingPathNameUpdateName() {
101         Collection<ForwardingPathDataDefinition> paths = createData("pathName4", "httpfd", "82df85", "name1");
102         test.validateForwardingPaths(paths, SERVICE_ID, true);
103     }
104
105     @Test
106     public void testUniqueForwardingPathNameUpdatePort() {
107         Collection<ForwardingPathDataDefinition> paths = createData("pathName3", "httpfd", "82df85", "name1");
108         test.validateForwardingPaths(paths, SERVICE_ID, true);
109     }
110
111     @Test
112     public void testLongForwardingPathPortNumber() {
113         Assertions.assertThrows(ComponentException.class, () -> {
114             String port = "Failed to execute goal on project catalog-be: Could not resolve dependencies for project \n" +
115                 "org.openecomp.sdc:catalog-be:war:1.1.0-SNAPSHOT: Failed to collect dependencies at \n" +
116                 "org.openecomp.sdc.common:openecomp-sdc-artifact-generator-api:jar:1802.0.1.167: ";
117             Collection<ForwardingPathDataDefinition> paths = createData("pathName",
118                 "http", port, "name1");
119             test.validateForwardingPaths(paths, SERVICE_ID, false);
120         });
121     }
122
123     @Test
124     public void testLongForwardingPathProtocol() {
125         Assertions.assertThrows(ComponentException.class, () -> {
126             String protocol = "Failed to execute goal on project catalog-be: Could not resolve dependencies for project \n" +
127                 "org.openecomp.sdc:catalog-be:war:1.1.0-SNAPSHOT: Failed to collect dependencies at \n" +
128                 "org.openecomp.sdc.common:openecomp-sdc-artifact-generator-api:jar:1802.0.1.167: ";
129             Collection<ForwardingPathDataDefinition> paths = createData("pathName",
130                 protocol, "port", "name1");
131             test.validateForwardingPaths(paths, SERVICE_ID, false);
132         });
133     }
134
135     private Set<ForwardingPathDataDefinition> createData(String pathName, String protocol, String ports, String uniqueId) {
136
137         return Sets.newHashSet(createPath(pathName, protocol, ports, uniqueId));
138     }
139
140
141     private <T extends Component> Either<T, StorageOperationStatus> getToscaFullElement() {
142
143         return Either.left((T) setUpServiceMcok());
144     }
145
146     private Service setUpServiceMcok() {
147         Service service = new Service();
148         service.addForwardingPath(createPath("pathName3", "http", "8285", "name1"));
149         return service;
150     }
151
152     private class ForwardingPathValidationUtilTest extends ForwardingPathValidator {
153
154         protected ResponseFormatManager getResponseFormatManager() {
155             return mock;
156         }
157     }
158
159 }