2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.versioning.impl;
19 import static org.junit.Assert.assertEquals;
20 import static org.mockito.Mockito.verify;
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;
39 public class ItemManagerImplTest {
42 public ExpectedException expectedException = ExpectedException.none();
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";
49 private ItemDao itemDao;
51 private PermissionsServices permissionsServices;
53 private SubscriptionService subscriptionService;
55 private ItemManagerImpl itemManager;
58 public void setUp() throws Exception {
59 MockitoAnnotations.openMocks(this);
63 public void tearDown(){
68 public void archiveTest(){
70 Item item = createItem(ITEM_ID,ITEM_NAME,ITEM_TYPE_A);
71 itemManager.archive(item);
73 verify(itemDao).update(item);
74 assertEquals(item.getStatus(), ItemStatus.ARCHIVED);
78 public void archiveTestNegative(){
80 expectedException.expect(CoreException.class);
81 expectedException.expectMessage(new RegexMatcher("Archive item failed, item .* is already Archived"));
83 Item item = createItem(ITEM_ID,ITEM_NAME,ITEM_TYPE_B);
84 item.setStatus(ItemStatus.ARCHIVED);
85 itemManager.archive(item);
90 public void restoreTest(){
92 Item item = createItem(ITEM_ID,ITEM_NAME,ITEM_TYPE_A);
93 item.setStatus(ItemStatus.ARCHIVED);
94 itemManager.restore(item);
96 verify(itemDao).update(item);
97 assertEquals(item.getStatus(), ItemStatus.ACTIVE);
101 public void restoreTestNegative(){
103 expectedException.expect(CoreException.class);
104 expectedException.expectMessage(new RegexMatcher("Restore item failed, item .* is already Active"));
106 Item item = createItem(ITEM_ID,ITEM_NAME,ITEM_TYPE_B);
107 item.setStatus(ItemStatus.ACTIVE);
108 itemManager.restore(item);
111 private Item createItem(String id, String name, String type) {
112 Item item = new Item();
119 private static class RegexMatcher extends TypeSafeMatcher<String> {
121 private final String regex;
123 private RegexMatcher(String regex) {
128 protected boolean matchesSafely(String s) {
129 return s.matches(regex);
133 public void describeTo(Description description) {
134 description.appendText("Regular expression: " + regex);