72ae4e7227b108287c64ee6dbae124e9d1a042cb
[sdc.git] /
1 /*
2  *
3  *  Copyright © 2017-2018 European Support Limited
4  *
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
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  * /
17  *
18  */
19
20 package org.openecomp.core.util;
21
22 import org.junit.jupiter.api.Assertions;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.junit.jupiter.MockitoExtension;
29 import org.openecomp.core.dao.UniqueValueDao;
30 import org.openecomp.core.dao.types.UniqueValueEntity;
31 import org.openecomp.sdc.common.errors.CoreException;
32 import org.testng.Assert;
33
34 @ExtendWith(MockitoExtension.class)
35 class UniqueValueUtilTest {
36
37     private static final String ENTITLEMENT_POOL_NAME = "Entitlement Pool name";
38     private static final String ORIGINAL_ENTITY_NAME = "originalEntityName";
39
40     @Mock
41     private UniqueValueDao uniqueValueDao;
42
43     private UniqueValueUtil uniqueValueUtil;
44
45     @BeforeEach
46     public void setUp() {
47         uniqueValueUtil = new UniqueValueUtil(uniqueValueDao);
48     }
49
50     @Test
51     void testCreateUniqueValue() {
52         Mockito.when(uniqueValueDao.get(Mockito.any())).thenReturn(null);
53         uniqueValueUtil.createUniqueValue(ENTITLEMENT_POOL_NAME, ORIGINAL_ENTITY_NAME);
54
55         Mockito.verify(uniqueValueDao, Mockito.times(1)).create(Mockito.any());
56     }
57
58     @Test
59     void testCreateUniqueValueNotUnique() {
60         Mockito.when(uniqueValueDao.get(Mockito.any())).thenReturn(new UniqueValueEntity());
61         Assertions.assertThrows(CoreException.class, () -> {
62             uniqueValueUtil.createUniqueValue(ENTITLEMENT_POOL_NAME, ORIGINAL_ENTITY_NAME);
63         });
64
65         Mockito.verify(uniqueValueDao, Mockito.times(1)).get(Mockito.any());
66     }
67
68     @Test
69     void testDeleteUniqueValue() {
70         Mockito.doNothing().when(uniqueValueDao).delete(Mockito.any());
71         uniqueValueUtil.deleteUniqueValue(ENTITLEMENT_POOL_NAME, ORIGINAL_ENTITY_NAME);
72
73         Mockito.verify(uniqueValueDao, Mockito.times(1)).delete(Mockito.any());
74     }
75
76     @Test
77     void testDeleteUniqueValueNoValue() {
78         uniqueValueUtil.deleteUniqueValue(ENTITLEMENT_POOL_NAME);
79         Mockito.verify(uniqueValueDao, Mockito.times(0)).delete(Mockito.any());
80     }
81
82     @Test
83     void testUpdateUniqueValue() {
84         Mockito.when(uniqueValueDao.get(Mockito.any())).thenReturn(null);
85         Mockito.doNothing().when(uniqueValueDao).delete(Mockito.any());
86
87         uniqueValueUtil.updateUniqueValue(ENTITLEMENT_POOL_NAME, "oldName", "newName", "uniqueContext");
88
89         Mockito.verify(uniqueValueDao, Mockito.times(1)).create(Mockito.any());
90         Mockito.verify(uniqueValueDao, Mockito.times(1)).delete(Mockito.any());
91     }
92
93     @Test
94     void testValidateUniqueValue() {
95         Mockito.when(uniqueValueDao.get(Mockito.any())).thenReturn(null);
96         uniqueValueUtil.validateUniqueValue(ENTITLEMENT_POOL_NAME, ORIGINAL_ENTITY_NAME);
97
98         Mockito.verify(uniqueValueDao, Mockito.times(1)).get(Mockito.any());
99     }
100
101     @Test
102     void testValidateUniqueValueNotUnique() {
103         Mockito.when(uniqueValueDao.get(Mockito.any())).thenReturn(new UniqueValueEntity());
104         Assertions.assertThrows(CoreException.class, () -> {
105             uniqueValueUtil.createUniqueValue(ENTITLEMENT_POOL_NAME, ORIGINAL_ENTITY_NAME);
106         });
107
108         Mockito.verify(uniqueValueDao, Mockito.times(1)).get(Mockito.any());
109     }
110
111     @Test
112     void testIsUniqueValueOccupied() {
113         Mockito.when(uniqueValueDao.get(Mockito.any())).thenReturn(new UniqueValueEntity());
114         Assert.assertTrue(uniqueValueUtil.isUniqueValueOccupied(ENTITLEMENT_POOL_NAME, ORIGINAL_ENTITY_NAME));
115     }
116
117     @Test
118     void testIsUniqueValueOccupiedFalse() {
119         Mockito.when(uniqueValueDao.get(Mockito.any())).thenReturn(null);
120         Assert.assertFalse(uniqueValueUtil.isUniqueValueOccupied(ENTITLEMENT_POOL_NAME, ORIGINAL_ENTITY_NAME));
121     }
122 }