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.ArrayList;
54 import java.util.Arrays;
55 import java.util.List;
58 import static org.assertj.core.api.Assertions.assertThat;
59 import static org.junit.Assert.assertEquals;
60 import static org.junit.Assert.assertTrue;
61 import static org.mockito.ArgumentMatchers.anyString;
62 import static org.mockito.ArgumentMatchers.eq;
63 import static org.mockito.Mockito.when;
65 public class PropertyBusinessLogicTest {
67 private static final Logger log = LoggerFactory.getLogger(PropertyBusinessLogicTest.class);
69 private ServletContext servletContext;
71 private IPropertyOperation propertyOperation;
73 private WebAppContextWrapper webAppContextWrapper;
75 private UserBusinessLogic mockUserAdmin;
77 private WebApplicationContext webAppContext;
79 private ComponentsUtils componentsUtils;
81 private ToscaOperationFacade toscaOperationFacade;
84 private UserValidations userValidations;
87 private PropertyBusinessLogic bl = new PropertyBusinessLogic();
88 private User user = null;
89 private String resourceId = "resourceforproperty.0.1";
93 MockitoAnnotations.initMocks(this);
94 ExternalConfiguration.setAppName("catalog-be");
97 String appConfigDir = "src/test/resources/config/catalog-be";
98 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
99 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
101 // User data and management
103 user.setUserId("jh003");
104 user.setFirstName("Jimmi");
105 user.setLastName("Hendrix");
106 user.setRole(Role.ADMIN.name());
108 Either<User, ActionStatus> eitherGetUser = Either.left(user);
109 when(mockUserAdmin.getUser("jh003", false)).thenReturn(eitherGetUser);
110 when(userValidations.validateUserExists(eq("jh003"), anyString(), eq(false))).thenReturn(user);
112 // Servlet Context attributes
113 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
114 when(servletContext.getAttribute(Constants.PROPERTY_OPERATION_MANAGER)).thenReturn(propertyOperation);
115 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
116 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webAppContext);
122 public void getProperty_propertyNotFound() throws Exception {
123 Resource resource = new Resource();
124 PropertyDefinition property1 = createPropertyObject("someProperty", "someResource");
125 PropertyDefinition property2 = createPropertyObject("someProperty2", "myResource");
126 resource.setProperties(Arrays.asList(property1, property2));
127 String resourceId = "myResource";
128 resource.setUniqueId(resourceId);
130 Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
131 Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> nonExistingProperty = bl.getProperty(resourceId, "NonExistingProperty", user.getUserId());
132 assertTrue(nonExistingProperty.isRight());
133 Mockito.verify(componentsUtils).getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, "");
137 public void getProperty_propertyNotBelongsToResource() throws Exception {
138 Resource resource = new Resource();
139 PropertyDefinition property1 = createPropertyObject("someProperty", "someResource");
140 resource.setProperties(Arrays.asList(property1));
141 String resourceId = "myResource";
142 resource.setUniqueId(resourceId);
144 Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
145 Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> notFoundProperty = bl.getProperty(resourceId, "invalidId", user.getUserId());
146 assertTrue(notFoundProperty.isRight());
147 Mockito.verify(componentsUtils).getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, "");
151 public void getProperty() throws Exception {
152 Resource resource = new Resource();
153 resource.setUniqueId(resourceId);
154 PropertyDefinition property1 = createPropertyObject("someProperty", null);
155 resource.setProperties(Arrays.asList(property1));
157 Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
158 Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> foundProperty = bl.getProperty(resourceId, property1.getUniqueId(), user.getUserId());
159 assertTrue(foundProperty.isLeft());
160 assertEquals(foundProperty.left().value().getValue().getUniqueId(), property1.getUniqueId());
163 private PropertyDefinition createPropertyObject(String propertyName, String resourceId) {
164 PropertyDefinition pd = new PropertyDefinition();
165 pd.setConstraints(null);
166 pd.setDefaultValue("100");
167 pd.setDescription("Size of thasdasdasdasde local disk, in Gigabytes (GB), available to applications running on the Compute node");
168 pd.setPassword(false);
169 pd.setRequired(true);
170 pd.setType("Integer");
171 pd.setOwnerId(resourceId);
172 pd.setUniqueId(resourceId + "." + propertyName);