3 * Copyright © 2017-2018 European Support Limited
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 package org.openecomp.core.util;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.MockitoAnnotations;
25 import org.openecomp.core.dao.UniqueValueDao;
26 import org.openecomp.core.dao.types.UniqueValueEntity;
27 import org.openecomp.sdc.common.errors.CoreException;
28 import org.testng.Assert;
29 import org.testng.annotations.BeforeMethod;
30 import org.testng.annotations.Test;
32 public class UniqueValueUtilTest {
34 private static final String ENTITLEMENT_POOL_NAME = "Entitlement Pool name";
35 private static final String ORIGINAL_ENTITY_NAME = "originalEntityName";
38 private UniqueValueDao uniqueValueDao;
40 private UniqueValueUtil uniqueValueUtil;
44 MockitoAnnotations.initMocks(this);
45 uniqueValueUtil = new UniqueValueUtil(uniqueValueDao);
49 public void testCreateUniqueValue() {
50 Mockito.when(uniqueValueDao.get(Mockito.any())).thenReturn(null);
51 uniqueValueUtil.createUniqueValue(ENTITLEMENT_POOL_NAME, ORIGINAL_ENTITY_NAME);
53 Mockito.verify(uniqueValueDao, Mockito.times(1)).create(Mockito.any());
56 @Test(expectedExceptions = CoreException.class)
57 public void testCreateUniqueValueNotUnique() {
58 Mockito.when(uniqueValueDao.get(Mockito.any())).thenReturn(new UniqueValueEntity());
59 uniqueValueUtil.createUniqueValue(ENTITLEMENT_POOL_NAME, ORIGINAL_ENTITY_NAME);
61 Mockito.verify(uniqueValueDao, Mockito.times(1)).create(Mockito.any());
65 public void testDeleteUniqueValue() {
66 Mockito.doNothing().when(uniqueValueDao).delete(Mockito.any());
67 uniqueValueUtil.deleteUniqueValue(ENTITLEMENT_POOL_NAME, ORIGINAL_ENTITY_NAME);
69 Mockito.verify(uniqueValueDao, Mockito.times(1)).delete(Mockito.any());
73 public void testDeleteUniqueValueNoValue() {
74 uniqueValueUtil.deleteUniqueValue(ENTITLEMENT_POOL_NAME);
75 Mockito.verify(uniqueValueDao, Mockito.times(0)).delete(Mockito.any());
79 public void testUpdateUniqueValue() {
80 Mockito.when(uniqueValueDao.get(Mockito.any())).thenReturn(null);
81 Mockito.doNothing().when(uniqueValueDao).delete(Mockito.any());
83 uniqueValueUtil.updateUniqueValue(ENTITLEMENT_POOL_NAME, "oldName", "newName", "uniqueContext");
85 Mockito.verify(uniqueValueDao, Mockito.times(1)).create(Mockito.any());
86 Mockito.verify(uniqueValueDao, Mockito.times(1)).delete(Mockito.any());
90 public void testValidateUniqueValue() {
91 Mockito.when(uniqueValueDao.get(Mockito.any())).thenReturn(null);
92 uniqueValueUtil.validateUniqueValue(ENTITLEMENT_POOL_NAME, ORIGINAL_ENTITY_NAME);
94 Mockito.verify(uniqueValueDao, Mockito.times(1)).get(Mockito.any());
97 @Test(expectedExceptions = CoreException.class)
98 public void testValidateUniqueValueNotUnique() {
99 Mockito.when(uniqueValueDao.get(Mockito.any())).thenReturn(new UniqueValueEntity());
100 uniqueValueUtil.createUniqueValue(ENTITLEMENT_POOL_NAME, ORIGINAL_ENTITY_NAME);
102 Mockito.verify(uniqueValueDao, Mockito.times(1)).get(Mockito.any());
106 public void testIsUniqueValueOccupied() {
107 Mockito.when(uniqueValueDao.get(Mockito.any())).thenReturn(new UniqueValueEntity());
108 Assert.assertTrue(uniqueValueUtil.isUniqueValueOccupied(ENTITLEMENT_POOL_NAME, ORIGINAL_ENTITY_NAME));
112 public void testIsUniqueValueOccupiedFalse() {
113 Mockito.when(uniqueValueDao.get(Mockito.any())).thenReturn(null);
114 Assert.assertFalse(uniqueValueUtil.isUniqueValueOccupied(ENTITLEMENT_POOL_NAME, ORIGINAL_ENTITY_NAME));