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 junit.framework.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.mockito.MockitoAnnotations;
31 import org.openecomp.sdc.be.components.impl.PropertyBusinessLogic;
32 import org.openecomp.sdc.be.components.validation.UserValidations;
33 import org.openecomp.sdc.be.config.ConfigurationManager;
34 import org.openecomp.sdc.be.dao.api.ActionStatus;
35 import org.openecomp.sdc.be.impl.ComponentsUtils;
36 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
37 import org.openecomp.sdc.be.model.PropertyConstraint;
38 import org.openecomp.sdc.be.model.PropertyDefinition;
39 import org.openecomp.sdc.be.model.Resource;
40 import org.openecomp.sdc.be.model.User;
41 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
42 import org.openecomp.sdc.be.model.operations.api.IPropertyOperation;
43 import org.openecomp.sdc.be.resources.data.EntryData;
44 import org.openecomp.sdc.be.user.Role;
45 import org.openecomp.sdc.be.user.UserBusinessLogic;
46 import org.openecomp.sdc.common.api.ConfigurationSource;
47 import org.openecomp.sdc.common.api.Constants;
48 import org.openecomp.sdc.common.impl.ExternalConfiguration;
49 import org.openecomp.sdc.common.impl.FSConfigurationSource;
50 import org.openecomp.sdc.exception.ResponseFormat;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.springframework.web.context.WebApplicationContext;
55 import javax.servlet.ServletContext;
56 import java.util.ArrayList;
57 import java.util.Arrays;
58 import java.util.List;
61 import static org.junit.Assert.assertEquals;
62 import static org.junit.Assert.assertTrue;
63 import static org.mockito.Matchers.anyString;
64 import static org.mockito.Matchers.eq;
65 import static org.mockito.Mockito.when;
67 public class PropertyBusinessLogicTest {
69 private static final Logger log = LoggerFactory.getLogger(PropertyBusinessLogicTest.class);
71 private ServletContext servletContext;
73 private IPropertyOperation propertyOperation;
75 private WebAppContextWrapper webAppContextWrapper;
77 private UserBusinessLogic mockUserAdmin;
79 private WebApplicationContext webAppContext;
81 private ComponentsUtils componentsUtils;
83 private ToscaOperationFacade toscaOperationFacade;
86 private UserValidations userValidations;
89 private PropertyBusinessLogic bl = new PropertyBusinessLogic();
90 private User user = null;
91 private String resourceId = "resourceforproperty.0.1";
95 MockitoAnnotations.initMocks(this);
96 ExternalConfiguration.setAppName("catalog-be");
99 String appConfigDir = "src/test/resources/config/catalog-be";
100 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
101 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
103 // User data and management
105 user.setUserId("jh003");
106 user.setFirstName("Jimmi");
107 user.setLastName("Hendrix");
108 user.setRole(Role.ADMIN.name());
110 Either<User, ActionStatus> eitherGetUser = Either.left(user);
111 when(mockUserAdmin.getUser("jh003", false)).thenReturn(eitherGetUser);
112 when(userValidations.validateUserExists(eq("jh003"), anyString(), eq(false))).thenReturn(Either.left(user));
114 // Servlet Context attributes
115 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
116 when(servletContext.getAttribute(Constants.PROPERTY_OPERATION_MANAGER)).thenReturn(propertyOperation);
117 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
118 // when(servletContext.getAttribute(Constants.RESOURCE_OPERATION_MANAGER)).thenReturn(resourceOperation);
119 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webAppContext);
121 // Resource Operation mock methods
123 // Either<Integer, StorageOperationStatus> eitherCount = Either.left(0);
124 // when(resourceOperation.getNumberOfResourcesByName("MyResourceName".toLowerCase())).thenReturn(eitherCount);
125 // Either<Integer, StorageOperationStatus> eitherCountExist = Either.left(1);
126 // when(resourceOperation.getNumberOfResourcesByName("alreadyExist".toLowerCase())).thenReturn(eitherCountExist);
127 // Either<Integer, StorageOperationStatus> eitherCountRoot = Either.left(1);
128 // when(resourceOperation.getNumberOfResourcesByName("Root".toLowerCase())).thenReturn(eitherCountRoot);
130 // Either<Resource, StorageOperationStatus> eitherGetResource = Either.left(createResourceObject(true));
131 // when(resourceOperation.getResource(resourceId)).thenReturn(eitherGetResource);
136 public void testHappyScenario() {
138 String propertyName = "disk_size";
139 PropertyDefinition newPropertyDefinition = createPropertyObject(propertyName, resourceId);
140 Either<EntryData<String, PropertyDefinition>, ResponseFormat> either = bl.createProperty(resourceId, propertyName, newPropertyDefinition, user.getUserId());
142 if (either.isRight()) {
143 Assert.assertFalse(true);
145 Assert.assertEquals(newPropertyDefinition, either.left().value());
149 public void getProperty_propertyNotFound() throws Exception {
150 Resource resource = new Resource();
151 PropertyDefinition property1 = createPropertyObject("someProperty", "someResource");
152 PropertyDefinition property2 = createPropertyObject("someProperty2", "myResource");
153 resource.setProperties(Arrays.asList(property1, property2));
154 String resourceId = "myResource";
155 resource.setUniqueId(resourceId);
157 Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
158 Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> nonExistingProperty = bl.getProperty(resourceId, "NonExistingProperty", user.getUserId());
159 assertTrue(nonExistingProperty.isRight());
160 Mockito.verify(componentsUtils).getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, "");
164 public void getProperty_propertyNotBelongsToResource() throws Exception {
165 Resource resource = new Resource();
166 PropertyDefinition property1 = createPropertyObject("someProperty", "someResource");
167 resource.setProperties(Arrays.asList(property1));
168 String resourceId = "myResource";
169 resource.setUniqueId(resourceId);
171 Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
172 Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> notFoundProperty = bl.getProperty(resourceId, "invalidId", user.getUserId());
173 assertTrue(notFoundProperty.isRight());
174 Mockito.verify(componentsUtils).getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, "");
178 public void getProperty() throws Exception {
179 Resource resource = new Resource();
180 resource.setUniqueId(resourceId);
181 PropertyDefinition property1 = createPropertyObject("someProperty", null);
182 resource.setProperties(Arrays.asList(property1));
184 Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
185 Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> foundProperty = bl.getProperty(resourceId, property1.getUniqueId(), user.getUserId());
186 assertTrue(foundProperty.isLeft());
187 assertEquals(foundProperty.left().value().getValue().getUniqueId(), property1.getUniqueId());
190 private PropertyDefinition createPropertyObject(String propertyName, String resourceId) {
191 PropertyDefinition pd = new PropertyDefinition();
192 List<PropertyConstraint> constraints = new ArrayList<PropertyConstraint>();
193 pd.setConstraints(null);
194 pd.setDefaultValue("100");
195 pd.setDescription("Size of thasdasdasdasde local disk, in Gigabytes (GB), available to applications running on the Compute node");
196 pd.setPassword(false);
197 pd.setRequired(true);
198 pd.setType("Integer");
199 pd.setOwnerId(resourceId);
200 pd.setUniqueId(resourceId + "." + propertyName);