cz.muni.fi.editor.database.test.NotificationDAOTest.java Source code

Java tutorial

Introduction

Here is the source code for cz.muni.fi.editor.database.test.NotificationDAOTest.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.
*/
/*
 * 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.database.test;

import cz.muni.fi.editor.database.dao.NotificationDAO;
import cz.muni.fi.editor.database.domain.Notification;
import cz.muni.fi.editor.database.domain.user.User;
import cz.muni.fi.editor.database.test.helpers.AbstractDAOTest;
import cz.muni.fi.editor.database.test.helpers.DAOTest;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * Created by Dominik Szalai - emptulik at gmail.com on 8/30/16.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@DAOTest
@Log4j2
public class NotificationDAOTest extends AbstractDAOTest {
    @BeforeClass
    public static void setUpClass() {
        checkMethods(NotificationDAOTest.class, NotificationDAO.class);
    }

    @Test
    public void create() {
        Notification n = new Notification();
        n.setNotified(userDAO.getById(8L));
        n.setRequest(requestDAO.getById(8L));
        n.setNotificationDate(LocalDateTime.now());
        n.setMessage("demo");

        Assert.assertNull(n.getId());
        notificationDAO.create(n);
        Assert.assertNotNull(n.getId());
    }

    @Test
    public void update() {
        Notification n = notificationDAO.getById(18L);
        n.setSeenDate(LocalDateTime.now());

        notificationDAO.update(n);

        Assert.assertNotNull(notificationDAO.getById(18L).getSeenDate());
    }

    @Test
    public void delete() {
        notificationDAO.delete(22L);

        Assert.assertNull(notificationDAO.getById(22L));
    }

    @Test
    public void getById() {
        Assert.assertEquals(Long.valueOf(11L), notificationDAO.getById(11L).getId());
    }

    @Test
    public void getAll() {
        Assert.assertEquals(22, notificationDAO.getAll().size());
    }

    @Test
    public void getNotifications() {
        User u = userDAO.getById(1L);
        Page p = new Page();
        Assert.assertEquals(4, notificationDAO.getNotifications(p.offset(), p.getPageSize(), u, true).size());
        p.next();
        Assert.assertEquals(2, notificationDAO.getNotifications(p.offset(), p.getPageSize(), u, true).size());

        User u2 = userDAO.getById(2L);
        Assert.assertEquals(6, notificationDAO.getNotifications(0, 10, u2, true).size());
        Assert.assertEquals(3, notificationDAO.getNotifications(0, 10, u2, false).size());
    }

    @Test
    public void getLatestNotifications() {
        Notification firstPoll = new Notification();
        firstPoll.setId(Long.valueOf(0L));
        Assert.assertEquals(4, notificationDAO.getLatestNotifications(userDAO.getById(2L), firstPoll).size());
        Notification poll = new Notification();
        poll.setId(Long.valueOf(15L));

        Assert.assertEquals(2, notificationDAO.getLatestNotifications(userDAO.getById(2L), poll).size());
    }

    @Test
    public void numberOfUnseen() {
        Assert.assertEquals(3, notificationDAO.numberOfUnseen(userDAO.getById(2L)));
    }

    @Test
    @Transactional(isolation = Isolation.READ_UNCOMMITTED)
    public void markSeen() {
        notificationDAO.markSeen(notificationDAO.getNotifications(0, 10, userDAO.getById(2L), false));

        Assert.assertTrue(notificationDAO.getNotifications(0, 10, userDAO.getById(2L), false).isEmpty());
    }

    @Test
    public void getClassType() {
        Assert.assertEquals(Notification.class, notificationDAO.getClassType());
    }

    // coverage test for branching
    @Test
    public void markLarge() {
        List<Notification> list = IntStream.rangeClosed(1, 101).mapToObj(i -> {
            Notification n = new Notification();
            n.setMessage("test");
            n.setNotificationDate(LocalDateTime.now());
            n.setNotified(userDAO.getById(1L));

            notificationDAO.create(n);
            return n;
        }).collect(Collectors.toList());

        notificationDAO.markSeen(list);

    }

    @AllArgsConstructor
    @NoArgsConstructor
    @Getter
    private class Page {
        private int page = 1;
        private int pageSize = 4;

        public void next() {
            page++;
        }

        public int offset() {
            return (page - 1) * pageSize;
        }
    }
}