Catalog-be dead code removal
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / PropertyBusinessLogicTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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;
22
23 import fj.data.Either;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.MockitoAnnotations;
30 import org.openecomp.sdc.be.components.impl.PropertyBusinessLogic;
31 import org.openecomp.sdc.be.components.validation.UserValidations;
32 import org.openecomp.sdc.be.config.ConfigurationManager;
33 import org.openecomp.sdc.be.dao.api.ActionStatus;
34 import org.openecomp.sdc.be.impl.ComponentsUtils;
35 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
36 import org.openecomp.sdc.be.model.PropertyDefinition;
37 import org.openecomp.sdc.be.model.Resource;
38 import org.openecomp.sdc.be.model.User;
39 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
40 import org.openecomp.sdc.be.model.operations.api.IPropertyOperation;
41 import org.openecomp.sdc.be.user.Role;
42 import org.openecomp.sdc.be.user.UserBusinessLogic;
43 import org.openecomp.sdc.common.api.ConfigurationSource;
44 import org.openecomp.sdc.common.api.Constants;
45 import org.openecomp.sdc.common.impl.ExternalConfiguration;
46 import org.openecomp.sdc.common.impl.FSConfigurationSource;
47 import org.openecomp.sdc.exception.ResponseFormat;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.web.context.WebApplicationContext;
51
52 import javax.servlet.ServletContext;
53 import java.util.Arrays;
54 import java.util.Map;
55
56 import static org.junit.Assert.assertEquals;
57 import static org.junit.Assert.assertTrue;
58 import static org.mockito.ArgumentMatchers.anyString;
59 import static org.mockito.ArgumentMatchers.eq;
60 import static org.mockito.Mockito.when;
61
62 public class PropertyBusinessLogicTest {
63
64     private static final Logger log = LoggerFactory.getLogger(PropertyBusinessLogicTest.class);
65     @Mock
66     private ServletContext servletContext;
67     @Mock
68     private IPropertyOperation propertyOperation;
69     @Mock
70     private WebAppContextWrapper webAppContextWrapper;
71     @Mock
72     private UserBusinessLogic mockUserAdmin;
73     @Mock
74     private WebApplicationContext webAppContext;
75     @Mock
76     private ComponentsUtils componentsUtils;
77     @Mock
78     private ToscaOperationFacade toscaOperationFacade;
79
80     @Mock
81     private UserValidations userValidations;
82
83     @InjectMocks
84     private PropertyBusinessLogic bl = new PropertyBusinessLogic();
85     private User user = null;
86     private String resourceId = "resourceforproperty.0.1";
87
88     @Before
89     public void setup() {
90         MockitoAnnotations.initMocks(this);
91         ExternalConfiguration.setAppName("catalog-be");
92
93         // init Configuration
94         String appConfigDir = "src/test/resources/config/catalog-be";
95         ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
96         ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
97
98         // User data and management
99         user = new User();
100         user.setUserId("jh003");
101         user.setFirstName("Jimmi");
102         user.setLastName("Hendrix");
103         user.setRole(Role.ADMIN.name());
104
105         Either<User, ActionStatus> eitherGetUser = Either.left(user);
106         when(mockUserAdmin.getUser("jh003", false)).thenReturn(eitherGetUser);
107         when(userValidations.validateUserExists(eq("jh003"), anyString(), eq(false))).thenReturn(Either.left(user));
108
109         // Servlet Context attributes
110         when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
111         when(servletContext.getAttribute(Constants.PROPERTY_OPERATION_MANAGER)).thenReturn(propertyOperation);
112         when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
113         when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webAppContext);
114
115
116     }
117
118     @Test
119     public void getProperty_propertyNotFound() throws Exception {
120         Resource resource = new Resource();
121         PropertyDefinition property1 = createPropertyObject("someProperty", "someResource");
122         PropertyDefinition property2 = createPropertyObject("someProperty2", "myResource");
123         resource.setProperties(Arrays.asList(property1, property2));
124         String resourceId = "myResource";
125         resource.setUniqueId(resourceId);
126
127         Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
128         Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> nonExistingProperty = bl.getProperty(resourceId, "NonExistingProperty", user.getUserId());
129         assertTrue(nonExistingProperty.isRight());
130         Mockito.verify(componentsUtils).getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, "");
131     }
132
133     @Test
134     public void getProperty_propertyNotBelongsToResource() throws Exception {
135         Resource resource = new Resource();
136         PropertyDefinition property1 = createPropertyObject("someProperty", "someResource");
137         resource.setProperties(Arrays.asList(property1));
138         String resourceId = "myResource";
139         resource.setUniqueId(resourceId);
140
141         Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
142         Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> notFoundProperty = bl.getProperty(resourceId, "invalidId", user.getUserId());
143         assertTrue(notFoundProperty.isRight());
144         Mockito.verify(componentsUtils).getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, "");
145     }
146
147     @Test
148     public void getProperty() throws Exception {
149         Resource resource = new Resource();
150         resource.setUniqueId(resourceId);
151         PropertyDefinition property1 = createPropertyObject("someProperty", null);
152         resource.setProperties(Arrays.asList(property1));
153
154         Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
155         Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> foundProperty = bl.getProperty(resourceId, property1.getUniqueId(), user.getUserId());
156         assertTrue(foundProperty.isLeft());
157         assertEquals(foundProperty.left().value().getValue().getUniqueId(), property1.getUniqueId());
158     }
159
160     private PropertyDefinition createPropertyObject(String propertyName, String resourceId) {
161         PropertyDefinition pd = new PropertyDefinition();
162         pd.setConstraints(null);
163         pd.setDefaultValue("100");
164         pd.setDescription("Size of thasdasdasdasde local disk, in Gigabytes (GB), available to applications running on the Compute node");
165         pd.setPassword(false);
166         pd.setRequired(true);
167         pd.setType("Integer");
168         pd.setOwnerId(resourceId);
169         pd.setUniqueId(resourceId + "." + propertyName);
170         return pd;
171     }
172 }