eeae2f75b00c4b5ce16b842ffccf2802c8b43566
[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.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;
31
32 public class UniqueValueUtilTest {
33
34     private static final String ENTITLEMENT_POOL_NAME = "Entitlement Pool name";
35     private static final String ORIGINAL_ENTITY_NAME = "originalEntityName";
36
37     @Mock
38     private UniqueValueDao uniqueValueDao;
39
40     private UniqueValueUtil uniqueValueUtil;
41
42     @BeforeMethod
43     public void setUp() {
44         MockitoAnnotations.initMocks(this);
45         uniqueValueUtil = new UniqueValueUtil(uniqueValueDao);
46     }
47
48     @Test
49     public void testCreateUniqueValue() {
50         Mockito.when(uniqueValueDao.get(Mockito.any())).thenReturn(null);
51         uniqueValueUtil.createUniqueValue(ENTITLEMENT_POOL_NAME, ORIGINAL_ENTITY_NAME);
52
53         Mockito.verify(uniqueValueDao, Mockito.times(1)).create(Mockito.any());
54     }
55
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);
60
61         Mockito.verify(uniqueValueDao, Mockito.times(1)).create(Mockito.any());
62     }
63
64     @Test
65     public void testDeleteUniqueValue() {
66         Mockito.doNothing().when(uniqueValueDao).delete(Mockito.any());
67         uniqueValueUtil.deleteUniqueValue(ENTITLEMENT_POOL_NAME, ORIGINAL_ENTITY_NAME);
68
69         Mockito.verify(uniqueValueDao, Mockito.times(1)).delete(Mockito.any());
70     }
71
72     @Test
73     public void testDeleteUniqueValueNoValue() {
74         uniqueValueUtil.deleteUniqueValue(ENTITLEMENT_POOL_NAME);
75         Mockito.verify(uniqueValueDao, Mockito.times(0)).delete(Mockito.any());
76     }
77
78     @Test
79     public void testUpdateUniqueValue() {
80         Mockito.when(uniqueValueDao.get(Mockito.any())).thenReturn(null);
81         Mockito.doNothing().when(uniqueValueDao).delete(Mockito.any());
82
83         uniqueValueUtil.updateUniqueValue(ENTITLEMENT_POOL_NAME, "oldName", "newName", "uniqueContext");
84
85         Mockito.verify(uniqueValueDao, Mockito.times(1)).create(Mockito.any());
86         Mockito.verify(uniqueValueDao, Mockito.times(1)).delete(Mockito.any());
87     }
88
89     @Test
90     public void testValidateUniqueValue() {
91         Mockito.when(uniqueValueDao.get(Mockito.any())).thenReturn(null);
92         uniqueValueUtil.validateUniqueValue(ENTITLEMENT_POOL_NAME, ORIGINAL_ENTITY_NAME);
93
94         Mockito.verify(uniqueValueDao, Mockito.times(1)).get(Mockito.any());
95     }
96
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);
101
102         Mockito.verify(uniqueValueDao, Mockito.times(1)).get(Mockito.any());
103     }
104
105     @Test
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));
109     }
110
111     @Test
112     public void testIsUniqueValueOccupiedFalse() {
113         Mockito.when(uniqueValueDao.get(Mockito.any())).thenReturn(null);
114         Assert.assertFalse(uniqueValueUtil.isUniqueValueOccupied(ENTITLEMENT_POOL_NAME, ORIGINAL_ENTITY_NAME));
115     }
116 }