cz.muni.fi.editor.test.service.NotificationServiceIT.java Source code

Java tutorial

Introduction

Here is the source code for cz.muni.fi.editor.test.service.NotificationServiceIT.java

Source

/*
*Copyright  2016 Dominik Szalai (emptulik@gmail.com)
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/
package cz.muni.fi.editor.test.service;

import cz.muni.fi.editor.api.dto.NotificationDTO;
import cz.muni.fi.editor.api.dto.NotificationRequest;
import cz.muni.fi.editor.api.dto.NotificationResponse;
import cz.muni.fi.editor.api.exceptions.FieldException;
import cz.muni.fi.editor.api.NotificationService;
import cz.muni.fi.editor.test.service.support.annotations.IntegrationTest;
import cz.muni.fi.editor.test.service.support.annotations.WithEditorUser;
import lombok.extern.log4j.Log4j2;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.Assert.fail;

/**
 * Created by Dominik Szalai - emptulik at gmail.com on 13.11.2016.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@IntegrationTest
@Log4j2
public class NotificationServiceIT {
    @Autowired
    private NotificationService notificationService;

    @Test(expected = IllegalArgumentException.class)
    @WithEditorUser
    public void markSeenNull() throws FieldException {
        notificationService.markSeen(null);
    }

    @Test(expected = FieldException.class)
    @WithEditorUser(2L)
    public void markSeenWithWrongID() throws FieldException {
        notificationService.markSeen(Stream.of(new NotificationDTO()).collect(Collectors.toList()));
    }

    @Test
    @WithEditorUser(2L)
    public void markSeen() throws FieldException {
        NotificationRequest r = new NotificationRequest();
        r.setPageSize(2);
        r.setPage(0);
        r.setIncludeSeen(false);

        notificationService.markSeen(notificationService.getNotifications(r).getResult());

        NotificationResponse response = notificationService.getNotifications(r);
        Assert.assertEquals(1, response.getResult().size());
        Assert.assertEquals(1, response.getTotalUnseen());
    }

    @Test(expected = IllegalArgumentException.class)
    @WithEditorUser(2L)
    public void pollNull() throws FieldException {
        notificationService.poll(null);
    }

    @Test(expected = FieldException.class)
    @WithEditorUser(2L)
    public void pollNoID() throws FieldException {
        notificationService.poll(new NotificationDTO());
    }

    @Test
    @WithEditorUser(2L)
    public void poll() throws FieldException {
        NotificationDTO dto = new NotificationDTO();
        dto.setId(11L);

        // poll includes seen notifications so there are 4 notifications
        // with id higher than 11. 4-> value set in database-test property file
        // as size limiter
        Assert.assertEquals(4, notificationService.poll(dto).getResult().size());
        dto.setId(12L);
        Assert.assertEquals(3, notificationService.poll(dto).getResult().size());
        dto.setId(19L);
        Assert.assertEquals(1, notificationService.poll(dto).getResult().size());
        dto.setId(30L);
        Assert.assertTrue(notificationService.poll(dto).getResult().isEmpty());
    }

    @Test(expected = IllegalArgumentException.class)
    @WithEditorUser(2L)
    public void getNotificationsNull() throws FieldException {
        notificationService.getNotifications(null);
    }

    @Test(expected = IllegalArgumentException.class)
    @WithEditorUser(2L)
    public void getNotificationsWrongRequest() throws IllegalArgumentException {
        NotificationRequest request = new NotificationRequest();
        request.setPage(-5);
        request.setPageSize(4);

        try {
            notificationService.getNotifications(request);
            fail(IllegalArgumentException.class.getName());
        } catch (IllegalArgumentException ex) {
            //ok
            request.setPage(4);
            request.setPageSize(-3);
        }

        notificationService.getNotifications(request);
    }

    @Test
    @WithEditorUser(2L)
    public void getNotifications() {
        NotificationRequest request = new NotificationRequest(0, true);
        request.setPageSize(3);

        Long[] firstWave = new Long[] { 21L, 18L, 15L };
        Long[] secondWave = new Long[] { 12L, 9L, 2L };

        NotificationResponse response = notificationService.getNotifications(request);
        Assert.assertEquals(3, response.getTotalUnseen());
        Assert.assertArrayEquals(firstWave, response.getResult().stream().map(NotificationDTO::getId)
                .collect(Collectors.toList()).toArray(new Long[3]));
        request.setPage(1);

        response = notificationService.getNotifications(request);
        Assert.assertEquals(3, response.getTotalUnseen());
        Assert.assertArrayEquals(secondWave, response.getResult().stream().map(NotificationDTO::getId)
                .collect(Collectors.toList()).toArray(new Long[3]));
    }
}