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 .api .query .dao ;
18+
19+ import static org .junit .Assert .assertEquals ;
20+ import static org .junit .Assert .assertNotNull ;
21+ import static org .junit .Assert .assertNull ;
22+ import static org .mockito .Mockito .doReturn ;
23+ import static org .mockito .Mockito .mock ;
24+ import static org .mockito .Mockito .never ;
25+ import static org .mockito .Mockito .verify ;
26+ import static org .mockito .Mockito .when ;
27+
28+ import java .util .Arrays ;
29+ import java .util .Collections ;
30+ import java .util .List ;
31+
32+ import org .junit .Before ;
33+ import org .junit .Test ;
34+ import org .junit .runner .RunWith ;
35+ import org .mockito .InjectMocks ;
36+ import org .mockito .Mock ;
37+ import org .mockito .Spy ;
38+ import org .mockito .junit .MockitoJUnitRunner ;
39+
40+ import com .cloud .api .query .vo .SnapshotJoinVO ;
41+ import com .cloud .utils .db .SearchBuilder ;
42+ import com .cloud .utils .db .SearchCriteria ;
43+
44+ @ RunWith (MockitoJUnitRunner .class )
45+ public class SnapshotJoinDaoImplTest {
46+
47+ @ Spy
48+ @ InjectMocks
49+ SnapshotJoinDaoImpl snapshotJoinDao = new SnapshotJoinDaoImpl ();
50+
51+ @ Mock
52+ SearchCriteria <SnapshotJoinVO > mockSearchCriteria ;
53+
54+ @ Before
55+ public void setUp () {
56+ SnapshotJoinVO mockSnap = mock (SnapshotJoinVO .class );
57+ SearchBuilder <SnapshotJoinVO > mockSearchBuilder = mock (SearchBuilder .class );
58+ when (mockSearchBuilder .entity ()).thenReturn (mockSnap );
59+ doReturn (mockSearchBuilder ).when (snapshotJoinDao ).createSearchBuilder ();
60+ when (mockSearchBuilder .create ()).thenReturn (mockSearchCriteria );
61+ }
62+
63+ @ Test
64+ public void testFindById_WithNullZoneId_EmptyResult () {
65+ Long zoneId = null ;
66+ long id = 1L ;
67+ doReturn (Collections .emptyList ()).when (snapshotJoinDao ).search (mockSearchCriteria , null );
68+ List <SnapshotJoinVO > result = snapshotJoinDao .findById (zoneId , id );
69+ assertNull (result );
70+ verify (mockSearchCriteria ).setParameters ("id" , id );
71+ verify (mockSearchCriteria , never ()).setParameters ("zoneId" , zoneId );
72+ }
73+
74+ @ Test
75+ public void testFindById_WithValidZoneId_EmptyResult () {
76+ Long zoneId = 1L ;
77+ long id = 1L ;
78+ doReturn (Collections .emptyList ()).when (snapshotJoinDao ).search (mockSearchCriteria , null );
79+ List <SnapshotJoinVO > result = snapshotJoinDao .findById (zoneId , id );
80+ assertNull (result );
81+ verify (mockSearchCriteria ).setParameters ("id" , id );
82+ verify (mockSearchCriteria ).setParameters ("zoneId" , zoneId );
83+ }
84+
85+ @ Test
86+ public void testFindById_WithValidResults () {
87+ Long zoneId = 1L ;
88+ long id = 1L ;
89+ SnapshotJoinVO snapshot1 = mock (SnapshotJoinVO .class );
90+ when (snapshot1 .getSnapshotStorePair ()).thenReturn ("Primary_1" );
91+ SnapshotJoinVO snapshot2 = mock (SnapshotJoinVO .class );
92+ when (snapshot2 .getSnapshotStorePair ()).thenReturn ("Image_1" );
93+ doReturn (Arrays .asList (snapshot1 , snapshot2 )).when (snapshotJoinDao ).search (mockSearchCriteria , null );
94+ List <SnapshotJoinVO > result = snapshotJoinDao .findById (zoneId , id );
95+ assertNotNull (result );
96+ assertEquals (1 , result .size ());
97+ assertEquals ("Image_1" , result .get (0 ).getSnapshotStorePair ());
98+ verify (mockSearchCriteria ).setParameters ("id" , id );
99+ verify (mockSearchCriteria ).setParameters ("zoneId" , zoneId );
100+ }
101+
102+ @ Test
103+ public void testFindById_WithNullResults () {
104+ long id = 1L ;
105+ doReturn (null ).when (snapshotJoinDao ).search (mockSearchCriteria , null );
106+ List <SnapshotJoinVO > result = snapshotJoinDao .findById (null , id );
107+ assertNull (result );
108+ }
109+ }
0 commit comments