/*
* Copyright (C) 2008-2017, Juick
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package com.juick.components.test;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.Appender;
import com.juick.components.CleanUp;
import com.juick.components.Notifications;
import com.juick.components.service.TokenService;
import com.notnoop.apns.ApnsService;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestTemplate;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Created by vitalyster on 22.11.2016.
*/
public class NotificationTests {
@InjectMocks
CleanUp cleanUp;
@Mock
ApnsService service;
@Mock
RestTemplate rest;
@Mock
TokenService tokenService;
@Mock
Notifications push;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
@Test
public void CleanUpTest() {
Map inactiveDevices = new HashMap<>();
inactiveDevices.put("yoyoyo", new Date());
when(service.getInactiveDevices()).thenReturn(inactiveDevices);
ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
final Appender mockAppender = mock(Appender.class);
when(mockAppender.getName()).thenReturn("MOCK");
root.addAppender(mockAppender);
cleanUp.cleanupTokens();
verify(mockAppender).doAppend(argThat(argument ->
((LoggingEvent)argument).getFormattedMessage().contains("1 tokens to delete")));
when(service.getInactiveDevices()).thenReturn(new HashMap<>());
cleanUp.cleanupTokens();
verify(mockAppender).doAppend(argThat(argument ->
((LoggingEvent)argument).getFormattedMessage().contains("No APNS tokens to delete")));
}
}