|
| 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.event.dao; |
| 18 | + |
| 19 | +import com.cloud.event.EventVO; |
| 20 | +import com.cloud.utils.db.Filter; |
| 21 | +import com.cloud.utils.db.SearchCriteria; |
| 22 | +import org.junit.Assert; |
| 23 | +import org.junit.Test; |
| 24 | +import org.junit.runner.RunWith; |
| 25 | +import org.mockito.ArgumentCaptor; |
| 26 | +import org.mockito.InjectMocks; |
| 27 | +import org.mockito.Spy; |
| 28 | +import org.mockito.junit.MockitoJUnitRunner; |
| 29 | +import org.springframework.test.util.ReflectionTestUtils; |
| 30 | + |
| 31 | +import java.util.Collections; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | + |
| 35 | +import static org.mockito.ArgumentMatchers.any; |
| 36 | +import static org.mockito.Mockito.doReturn; |
| 37 | +import static org.mockito.Mockito.verify; |
| 38 | + |
| 39 | +@RunWith(MockitoJUnitRunner.class) |
| 40 | +public class EventDaoImplTest { |
| 41 | + |
| 42 | + @Spy |
| 43 | + @InjectMocks |
| 44 | + EventDaoImpl eventDaoImpl = new EventDaoImpl(); |
| 45 | + |
| 46 | + @Test |
| 47 | + public void listLatestEventsByResourceQueriesByResourceAndTypeExcludingArchived() { |
| 48 | + List<EventVO> expected = Collections.singletonList(new EventVO()); |
| 49 | + doReturn(expected).when(eventDaoImpl).listBy(any(SearchCriteria.class), any(Filter.class)); |
| 50 | + |
| 51 | + List<EventVO> result = eventDaoImpl.listLatestEventsByResource(1L, "Volume", "SNAPSHOT.CREATE", 3); |
| 52 | + |
| 53 | + Assert.assertSame(expected, result); |
| 54 | + |
| 55 | + ArgumentCaptor<SearchCriteria> scCaptor = ArgumentCaptor.forClass(SearchCriteria.class); |
| 56 | + ArgumentCaptor<Filter> filterCaptor = ArgumentCaptor.forClass(Filter.class); |
| 57 | + verify(eventDaoImpl).listBy(scCaptor.capture(), filterCaptor.capture()); |
| 58 | + |
| 59 | + SearchCriteria<EventVO> sc = scCaptor.getValue(); |
| 60 | + Assert.assertEquals("event.resource_id = ? AND event.resource_type = ? AND event.type = ? AND event.archived = ? ", |
| 61 | + sc.getWhereClause()); |
| 62 | + @SuppressWarnings("unchecked") |
| 63 | + Map<String, Object[]> params = (Map<String, Object[]>) ReflectionTestUtils.getField(sc, "_params"); |
| 64 | + Assert.assertArrayEquals(new Object[]{1L}, params.get("resourceId")); |
| 65 | + Assert.assertArrayEquals(new Object[]{"Volume"}, params.get("resourceType")); |
| 66 | + Assert.assertArrayEquals(new Object[]{"SNAPSHOT.CREATE"}, params.get("type")); |
| 67 | + Assert.assertArrayEquals(new Object[]{false}, params.get("archived")); |
| 68 | + |
| 69 | + Filter filter = filterCaptor.getValue(); |
| 70 | + Assert.assertEquals(" ORDER BY event.created DESC ", filter.getOrderBy()); |
| 71 | + Assert.assertEquals(Long.valueOf(0L), filter.getOffset()); |
| 72 | + Assert.assertEquals(Long.valueOf(3L), filter.getLimit()); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void listLatestEventsByResourceReturnsEmptyListWhenNoneFound() { |
| 77 | + doReturn(Collections.emptyList()).when(eventDaoImpl).listBy(any(SearchCriteria.class), any(Filter.class)); |
| 78 | + |
| 79 | + List<EventVO> result = eventDaoImpl.listLatestEventsByResource(1L, "Volume", "SNAPSHOT.CREATE", 3); |
| 80 | + |
| 81 | + Assert.assertTrue(result.isEmpty()); |
| 82 | + } |
| 83 | +} |
0 commit comments