2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components;
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;
52 import javax.servlet.ServletContext;
53 import java.util.Arrays;
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;
62 public class PropertyBusinessLogicTest {
64 private static final Logger log = LoggerFactory.getLogger(PropertyBusinessLogicTest.class);
66 private ServletContext servletContext;
68 private IPropertyOperation propertyOperation;
70 private WebAppContextWrapper webAppContextWrapper;
72 private UserBusinessLogic mockUserAdmin;
74 private WebApplicationContext webAppContext;
76 private ComponentsUtils componentsUtils;
78 private ToscaOperationFacade toscaOperationFacade;
81 private UserValidations userValidations;
84 private PropertyBusinessLogic bl = new PropertyBusinessLogic();
85 private User user = null;
86 private String resourceId = "resourceforproperty.0.1";
90 MockitoAnnotations.initMocks(this);
91 ExternalConfiguration.setAppName("catalog-be");
94 String appConfigDir = "src/test/resources/config/catalog-be";
95 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
96 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
98 // User data and management
100 user.setUserId("jh003");
101 user.setFirstName("Jimmi");
102 user.setLastName("Hendrix");
103 user.setRole(Role.ADMIN.name());
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));
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);
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);
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, "");
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);
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, "");
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));
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());
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);