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