Skip to content

Commit dcd6739

Browse files
author
Ramgopal Nagaboina
committed
engine-schema: unit test SnapshotDao.findByVolumeIdAndNameNotInStatus
Covers the finder added for duplicate snapshot name rejection: it returns the matching snapshot when one exists and null when none does, and queries by volume id and name.
1 parent ca6075e commit dcd6739

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. 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,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.storage.dao;
18+
19+
import java.lang.reflect.Field;
20+
21+
import org.junit.Assert;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.mockito.Mock;
26+
import org.mockito.Mockito;
27+
import org.mockito.Spy;
28+
import org.mockito.junit.MockitoJUnitRunner;
29+
30+
import com.cloud.storage.Snapshot;
31+
import com.cloud.storage.SnapshotVO;
32+
import com.cloud.utils.db.SearchBuilder;
33+
import com.cloud.utils.db.SearchCriteria;
34+
35+
@RunWith(MockitoJUnitRunner.class)
36+
public class SnapshotDaoImplTest {
37+
38+
@Mock
39+
SearchBuilder<SnapshotVO> volumeIdNameNotInStatusSearchMock;
40+
41+
@Mock
42+
SearchCriteria<SnapshotVO> searchCriteriaMock;
43+
44+
@Spy
45+
SnapshotDaoImpl snapshotDao = new SnapshotDaoImpl();
46+
47+
@Before
48+
public void setUp() throws Exception {
49+
// init() wires up several search builders that join other DAOs, which are not available in a
50+
// unit test; inject only the search builder this method uses and stub it to return a criteria.
51+
Field searchField = SnapshotDaoImpl.class.getDeclaredField("volumeIdNameNotInStatusSearch");
52+
searchField.setAccessible(true);
53+
searchField.set(snapshotDao, volumeIdNameNotInStatusSearchMock);
54+
Mockito.lenient().doReturn(searchCriteriaMock).when(volumeIdNameNotInStatusSearchMock).create();
55+
}
56+
57+
@Test
58+
public void testFindByVolumeIdAndNameNotInStatusReturnsTheMatch() {
59+
SnapshotVO expected = Mockito.mock(SnapshotVO.class);
60+
Mockito.doReturn(expected).when(snapshotDao).findOneBy(Mockito.any(SearchCriteria.class));
61+
62+
SnapshotVO result = snapshotDao.findByVolumeIdAndNameNotInStatus(5L, "snap-1", Snapshot.State.Destroyed);
63+
64+
Assert.assertSame(expected, result);
65+
Mockito.verify(searchCriteriaMock).setParameters("volumeId", 5L);
66+
Mockito.verify(searchCriteriaMock).setParameters("name", "snap-1");
67+
Mockito.verify(snapshotDao).findOneBy(searchCriteriaMock);
68+
}
69+
70+
@Test
71+
public void testFindByVolumeIdAndNameNotInStatusReturnsNullWhenNoMatch() {
72+
Mockito.doReturn(null).when(snapshotDao).findOneBy(Mockito.any(SearchCriteria.class));
73+
74+
SnapshotVO result = snapshotDao.findByVolumeIdAndNameNotInStatus(5L, "snap-1");
75+
76+
Assert.assertNull(result);
77+
Mockito.verify(searchCriteriaMock).setParameters("volumeId", 5L);
78+
Mockito.verify(snapshotDao).findOneBy(searchCriteriaMock);
79+
}
80+
}

0 commit comments

Comments
 (0)