Improve testing stability
[sdc.git] / openecomp-be / lib / openecomp-sdc-versioning-lib / openecomp-sdc-versioning-core / src / test / java / org / openecomp / sdc / versioning / impl / ItemManagerImplTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.versioning.impl;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.mockito.Mockito.verify;
21
22 import org.hamcrest.Description;
23 import org.hamcrest.TypeSafeMatcher;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.openecomp.sdc.common.errors.CoreException;
33 import org.openecomp.sdc.itempermissions.PermissionsServices;
34 import org.openecomp.sdc.notification.services.SubscriptionService;
35 import org.openecomp.sdc.versioning.dao.ItemDao;
36 import org.openecomp.sdc.versioning.types.Item;
37 import org.openecomp.sdc.versioning.types.ItemStatus;
38
39 public class ItemManagerImplTest {
40
41     @Rule
42     public ExpectedException expectedException = ExpectedException.none();
43
44     private static final String ITEM_ID = "item1";
45     private static final String ITEM_NAME = "item 1 name";
46     private static final String ITEM_TYPE_A = "A";
47     private static final String ITEM_TYPE_B = "B";
48     @Mock
49     private ItemDao itemDao;
50     @Mock
51     private PermissionsServices permissionsServices;
52     @Mock
53     private SubscriptionService subscriptionService;
54     @InjectMocks
55     private ItemManagerImpl itemManager;
56
57     @Before
58     public void setUp() throws Exception {
59         MockitoAnnotations.openMocks(this);
60     }
61
62     @After
63     public void tearDown(){
64         itemManager = null;
65     }
66
67     @Test
68     public void archiveTest(){
69
70         Item item  = createItem(ITEM_ID,ITEM_NAME,ITEM_TYPE_A);
71         itemManager.archive(item);
72
73         verify(itemDao).update(item);
74         assertEquals(item.getStatus(), ItemStatus.ARCHIVED);
75     }
76
77     @Test
78     public void archiveTestNegative(){
79
80         expectedException.expect(CoreException.class);
81         expectedException.expectMessage(new RegexMatcher("Archive item failed, item .* is already Archived"));
82
83         Item item  = createItem(ITEM_ID,ITEM_NAME,ITEM_TYPE_B);
84         item.setStatus(ItemStatus.ARCHIVED);
85         itemManager.archive(item);
86
87     }
88
89     @Test
90     public void restoreTest(){
91
92         Item item  = createItem(ITEM_ID,ITEM_NAME,ITEM_TYPE_A);
93         item.setStatus(ItemStatus.ARCHIVED);
94         itemManager.restore(item);
95
96         verify(itemDao).update(item);
97         assertEquals(item.getStatus(), ItemStatus.ACTIVE);
98     }
99
100     @Test
101     public void restoreTestNegative(){
102
103         expectedException.expect(CoreException.class);
104         expectedException.expectMessage(new RegexMatcher("Restore item failed, item .* is already Active"));
105
106         Item item  = createItem(ITEM_ID,ITEM_NAME,ITEM_TYPE_B);
107         item.setStatus(ItemStatus.ACTIVE);
108         itemManager.restore(item);
109     }
110
111     private Item createItem(String id, String name, String type) {
112         Item item = new Item();
113         item.setId(id);
114         item.setName(name);
115         item.setType(type);
116         return item;
117     }
118
119     private static class RegexMatcher extends TypeSafeMatcher<String> {
120
121         private final String regex;
122
123         private RegexMatcher(String regex) {
124             this.regex = regex;
125         }
126
127         @Override
128         protected boolean matchesSafely(String s) {
129             return s.matches(regex);
130         }
131
132         @Override
133         public void describeTo(Description description) {
134             description.appendText("Regular expression: " + regex);
135         }
136     }
137 }