org.homiefund.test.dao.TransactionDAOTest.java Source code

Java tutorial

Introduction

Here is the source code for org.homiefund.test.dao.TransactionDAOTest.java

Source

/**
 * Copyright  2016 REPLACE ME OWNER (REPLACE ME YEAR)
 *
 * 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 org.homiefund.test.dao;

import lombok.extern.log4j.Log4j2;
import org.homiefund.api.dao.TransactionDAO;
import org.homiefund.api.dao.domain.Transaction;
import org.homiefund.api.dao.domain.TransactionParticipant;
import org.homiefund.test.config.AbstractDAOTest;
import org.homiefund.test.config.DAOTest;
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 java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created by Dominik Szalai - emptulik at gmail.com on 22.9.2016.
 */
@DAOTest
@Log4j2
@RunWith(SpringJUnit4ClassRunner.class)
public class TransactionDAOTest extends AbstractDAOTest {
    @BeforeClass
    public static void setUp() {
        checkMethods(TransactionDAOTest.class, TransactionDAO.class);
    }

    @Test
    public void create() {
        Transaction tx = new Transaction();
        tx.setRevoked(false);
        tx.setDate(LocalDate.now());
        tx.setDescription(" create tessst ");
        tx.setOwner(userDAO.getById(1L));
        tx.setTransactionType(transactionTypeDAO.getById(4L));
        tx.setAmount(BigDecimal.valueOf(30.52));
        TransactionParticipant tp1 = new TransactionParticipant();
        tp1.setParticipant(userDAO.getById(1L));
        TransactionParticipant tp2 = new TransactionParticipant();
        tp2.setParticipant(userDAO.getById(2L));
        tx.setParticipants(new ArrayList<>(Arrays.asList(tp1, tp2)));

        Assert.assertNotNull(transactionDAO.create(tx));
        Assert.assertNotNull(tx.getId());

        BigDecimal half = tx.getAmount().divide(BigDecimal.valueOf(2L), new MathContext(2, RoundingMode.HALF_UP));
        tx.getParticipants().forEach(p -> Assert.assertEquals(half, p.getAmount()));
    }

    @Test
    public void update() {
        Transaction tx = transactionDAO.getById(2L);
        tx.setRevoked(true);

        transactionDAO.update(tx);

        Assert.assertEquals(tx.getRevoked(), transactionDAO.getById(tx.getId()).getRevoked());
    }

    @Test
    public void delete() {
        transactionDAO.delete(2L);

        Assert.assertNull(transactionDAO.getById(2L));
    }

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

    @Test
    public void getAll() {
        Assert.assertEquals(9, transactionDAO.getAll().size());
    }

    @Test
    public void getClassType() {
        Assert.assertEquals(Transaction.class, transactionDAO.getClassType());
    }
}