[sdc] update code of sdc
[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 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;
57
58 import javax.servlet.ServletContext;
59 import java.util.ArrayList;
60 import java.util.Arrays;
61 import java.util.List;
62 import java.util.Map;
63
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;
68
69 public class PropertyBusinessLogicTest {
70
71         private static Logger log = LoggerFactory.getLogger(PropertyBusinessLogicTest.class.getName());
72         @Mock
73         private ServletContext servletContext;
74         @Mock
75         private IPropertyOperation propertyOperation;
76         @Mock
77         private IResourceOperation resourceOperation;
78         @Mock
79         private WebAppContextWrapper webAppContextWrapper;
80         @Mock
81         private UserBusinessLogic mockUserAdmin;
82         @Mock
83         private WebApplicationContext webAppContext;
84         @Mock
85         private ComponentsUtils componentsUtils;
86         @Mock
87         private ToscaOperationFacade toscaOperationFacade;
88
89         @InjectMocks
90         private PropertyBusinessLogic bl = new PropertyBusinessLogic();
91         private User user = null;
92         private String resourceId = "resourceforproperty.0.1";
93
94         @Before
95         public void setup() {
96                 MockitoAnnotations.initMocks(this);
97                 ExternalConfiguration.setAppName("catalog-be");
98
99                 // Init Configuration
100                 String appConfigDir = "src/test/resources/config/catalog-be";
101                 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
102                 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
103
104                 // User data and management
105                 user = new User();
106                 user.setUserId("jh003");
107                 user.setFirstName("Jimmi");
108                 user.setLastName("Hendrix");
109                 user.setRole(Role.ADMIN.name());
110
111                 Either<User, ActionStatus> eitherGetUser = Either.left(user);
112                 when(mockUserAdmin.getUser("jh003", false)).thenReturn(eitherGetUser);
113
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);
120
121                 // Resource Operation mock methods
122                 // getCount
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);
129
130                 Either<Resource, StorageOperationStatus> eitherGetResource = Either.left(createResourceObject(true));
131                 when(resourceOperation.getResource(resourceId)).thenReturn(eitherGetResource);
132
133         }
134
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>();
141                 tgs.add("test");
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");
150
151                 if (afterCreate) {
152                         resource.setName(resource.getName().toLowerCase());
153                         resource.setVersion("0.1");
154                         ;
155                         resource.setUniqueId(resourceId);
156                         resource.setCreatorUserId(user.getUserId());
157                         resource.setCreatorFullName(user.getFirstName() + " " + user.getLastName());
158                 }
159                 Gson gson = new GsonBuilder().setPrettyPrinting().create();
160                 log.debug(gson.toJson(resource));
161                 return resource;
162         }
163
164         // @Test
165         public void testHappyScenario() {
166
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());
170
171                 if (either.isRight()) {
172                         Assert.assertFalse(true);
173                 }
174                 Assert.assertEquals(newPropertyDefinition, either.left().value());
175         }
176
177         @Test
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);
185
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, "");
190         }
191
192         @Test
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);
199
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, "");
204         }
205
206         @Test
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));
212
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());
217         }
218
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);
230                 return pd;
231         }
232 }