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