Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
Adding unit tests for NotificationsCache (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Liew authored and GitHub Enterprise committed Jun 21, 2019
1 parent 7419d58 commit e1cf4b1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import javax.persistence.EntityManager;

import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Provider;

/*
Expand All @@ -45,7 +46,7 @@
*/
public class NotificationsCache {

private NotificationsCacheRefresherThread refresherThread;
private final NotificationsCacheRefresherThread refresherThread;

private Map<BigInteger/*notificationId*/, Map<String/*metricKey*/, Long/*coolDownExpiration*/>> notificationCooldownExpirationMap = new HashMap<BigInteger, Map<String, Long>>();

Expand All @@ -56,6 +57,17 @@ public class NotificationsCache {
public NotificationsCache(Provider<EntityManager> em) {
refresherThread = new NotificationsCacheRefresherThread(this, em);

initCacheAndStartRefresherThread();
}

@VisibleForTesting
protected NotificationsCache(NotificationsCacheRefresherThread refresherThread) {
this.refresherThread = refresherThread;

initCacheAndStartRefresherThread();
}

private void initCacheAndStartRefresherThread() {
// Run once on main thread to populate the cache
refresherThread.runOnce();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.salesforce.dva.argus.service.alert;

import com.google.common.collect.ImmutableMap;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mock;

import java.math.BigInteger;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;

@RunWith(org.mockito.junit.MockitoJUnitRunner.class)
public class NotificationsCacheTest {
@Mock
private NotificationsCacheRefresherThread thread;
private NotificationsCache cache;

@Before
public void setUp() {
cache = new NotificationsCache(thread);
}

@Test
public void initCacheAndStartRefresherThread_test() {
verify(thread).runOnce();
verify(thread).start();
}

@Test
public void setNotificationCooldownExpirationMap_test() {
Map<BigInteger, Map<String, Long>> notificationCooldownExpirationMap = ImmutableMap.of(BigInteger.TEN,
ImmutableMap.of("TEST", Long.MIN_VALUE));
cache.setNotificationCooldownExpirationMap(notificationCooldownExpirationMap);
assertSame(notificationCooldownExpirationMap, cache.getNotificationCooldownExpirationMap());
}

@Test
public void setNotificationActiveStatusMap_test() {
Map<BigInteger, Map<String, Boolean>> notificationActiveStatusMap = ImmutableMap.of(BigInteger.TEN,
ImmutableMap.of("TEST", Boolean.TRUE));
cache.setNotificationActiveStatusMap(notificationActiveStatusMap);
assertSame(notificationActiveStatusMap, cache.getNotificationActiveStatusMap());
}

@Test
public void setNotificationsCacheRefreshed_test() {
boolean refreshed = false;
cache.setNotificationsCacheRefreshed(refreshed);
assertEquals(refreshed, cache.isNotificationsCacheRefreshed());
refreshed = true;
cache.setNotificationsCacheRefreshed(refreshed);
assertEquals(refreshed, cache.isNotificationsCacheRefreshed());
}
}

0 comments on commit e1cf4b1

Please sign in to comment.