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 com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import fj.data.Either;
26 import junit.framework.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.mockito.MockitoAnnotations;
33 import org.openecomp.sdc.be.components.impl.PropertyBusinessLogic;
34 import org.openecomp.sdc.be.config.ConfigurationManager;
35 import org.openecomp.sdc.be.dao.api.ActionStatus;
36 import org.openecomp.sdc.be.impl.ComponentsUtils;
37 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
38 import org.openecomp.sdc.be.model.PropertyConstraint;
39 import org.openecomp.sdc.be.model.PropertyDefinition;
40 import org.openecomp.sdc.be.model.Resource;
41 import org.openecomp.sdc.be.model.User;
42 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
43 import org.openecomp.sdc.be.model.operations.api.IPropertyOperation;
44 import org.openecomp.sdc.be.model.operations.api.IResourceOperation;
45 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
46 import org.openecomp.sdc.be.resources.data.EntryData;
47 import org.openecomp.sdc.be.user.Role;
48 import org.openecomp.sdc.be.user.UserBusinessLogic;
49 import org.openecomp.sdc.common.api.ConfigurationSource;
50 import org.openecomp.sdc.common.api.Constants;
51 import org.openecomp.sdc.common.impl.ExternalConfiguration;
52 import org.openecomp.sdc.common.impl.FSConfigurationSource;
53 import org.openecomp.sdc.exception.ResponseFormat;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.springframework.web.context.WebApplicationContext;
58 import javax.servlet.ServletContext;
59 import java.util.ArrayList;
60 import java.util.Arrays;
61 import java.util.List;
64 import static org.junit.Assert.assertEquals;
65 import static org.junit.Assert.assertTrue;
66 import static org.mockito.Mockito.mock;
67 import static org.mockito.Mockito.when;
69 public class PropertyBusinessLogicTest {
71 private static Logger log = LoggerFactory.getLogger(PropertyBusinessLogicTest.class.getName());
73 private ServletContext servletContext;
75 private IPropertyOperation propertyOperation;
77 private IResourceOperation resourceOperation;
79 private WebAppContextWrapper webAppContextWrapper;
81 private UserBusinessLogic mockUserAdmin;
83 private WebApplicationContext webAppContext;
85 private ComponentsUtils componentsUtils;
87 private ToscaOperationFacade toscaOperationFacade;
90 private PropertyBusinessLogic bl = new PropertyBusinessLogic();
91 private User user = null;
92 private String resourceId = "resourceforproperty.0.1";
96 MockitoAnnotations.initMocks(this);
97 ExternalConfiguration.setAppName("catalog-be");
100 String appConfigDir = "src/test/resources/config/catalog-be";
101 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
102 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
104 // User data and management
106 user.setUserId("jh003");
107 user.setFirstName("Jimmi");
108 user.setLastName("Hendrix");
109 user.setRole(Role.ADMIN.name());
111 Either<User, ActionStatus> eitherGetUser = Either.left(user);
112 when(mockUserAdmin.getUser("jh003", false)).thenReturn(eitherGetUser);
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);
135 private Resource createResourceObject(boolean afterCreate) {
136 Resource resource = new Resource();
137 resource.setName("MyResourceName");
138 resource.addCategory("Generic", "VoIP");
139 resource.setDescription("My short description");
140 List<String> tgs = new ArrayList<String>();
142 resource.setTags(tgs);
143 List<String> template = new ArrayList<String>();
144 template.add("Root");
145 resource.setDerivedFrom(template);
146 resource.setVendorName("Motorola");
147 resource.setVendorRelease("1.0.0");
148 resource.setContactId("yavivi");
149 resource.setIcon("MyIcon.jpg");
152 resource.setName(resource.getName().toLowerCase());
153 resource.setVersion("0.1");
155 resource.setUniqueId(resourceId);
156 resource.setCreatorUserId(user.getUserId());
157 resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
159 Gson gson = new GsonBuilder().setPrettyPrinting().create();
160 log.debug(gson.toJson(resource));
165 public void testHappyScenario() {
167 String propertyName = "disk_size";
168 PropertyDefinition newPropertyDefinition = createPropertyObject(propertyName, resourceId);
169 Either<EntryData<String, PropertyDefinition>, ResponseFormat> either = bl.createProperty(resourceId, propertyName, newPropertyDefinition, user.getUserId());
171 if (either.isRight()) {
172 Assert.assertFalse(true);
174 Assert.assertEquals(newPropertyDefinition, either.left().value());
178 public void getProperty_propertyNotFound() throws Exception {
179 Resource resource = new Resource();
180 PropertyDefinition property1 = createPropertyObject("someProperty", "someResource");
181 PropertyDefinition property2 = createPropertyObject("someProperty2", "myResource");
182 resource.setProperties(Arrays.asList(property1, property2));
183 String resourceId = "myResource";
184 resource.setUniqueId(resourceId);
186 Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
187 Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> nonExistingProperty = bl.getProperty(resourceId, "NonExistingProperty", user.getUserId());
188 assertTrue(nonExistingProperty.isRight());
189 Mockito.verify(componentsUtils).getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, "");
193 public void getProperty_propertyNotBelongsToResource() throws Exception {
194 Resource resource = new Resource();
195 PropertyDefinition property1 = createPropertyObject("someProperty", "someResource");
196 resource.setProperties(Arrays.asList(property1));
197 String resourceId = "myResource";
198 resource.setUniqueId(resourceId);
200 Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
201 Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> notFoundProperty = bl.getProperty(resourceId, property1.getUniqueId(), user.getUserId());
202 assertTrue(notFoundProperty.isRight());
203 Mockito.verify(componentsUtils).getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, "");
207 public void getProperty() throws Exception {
208 Resource resource = new Resource();
209 resource.setUniqueId(resourceId);
210 PropertyDefinition property1 = createPropertyObject("someProperty", null);
211 resource.setProperties(Arrays.asList(property1));
213 Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
214 Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> foundProperty = bl.getProperty(resourceId, property1.getUniqueId(), user.getUserId());
215 assertTrue(foundProperty.isLeft());
216 assertEquals(foundProperty.left().value().getValue().getUniqueId(), property1.getUniqueId());
219 private PropertyDefinition createPropertyObject(String propertyName, String resourceId) {
220 PropertyDefinition pd = new PropertyDefinition();
221 List<PropertyConstraint> constraints = new ArrayList<PropertyConstraint>();
222 pd.setConstraints(null);
223 pd.setDefaultValue("100");
224 pd.setDescription("Size of thasdasdasdasde local disk, in Gigabytes (GB), available to applications running on the Compute node");
225 pd.setPassword(false);
226 pd.setRequired(true);
227 pd.setType("Integer");
228 pd.setOwnerId(resourceId);
229 pd.setUniqueId(resourceId + "." + propertyName);