org.homiefund.test.config.AbstractDAOTest.java Source code

Java tutorial

Introduction

Here is the source code for org.homiefund.test.config.AbstractDAOTest.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.config;

import org.homiefund.api.dao.HomeDAO;
import org.homiefund.api.dao.InvitationDAO;
import org.homiefund.api.dao.TransactionDAO;
import org.homiefund.api.dao.TransactionTypeDAO;
import org.homiefund.api.dao.UserDAO;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by Dominik Szalai - emptulik at gmail.com on 22.9.2016.
 */
public class AbstractDAOTest {
    @Autowired
    protected UserDAO userDAO;
    @Autowired
    protected HomeDAO homeDAO;
    @Autowired
    protected InvitationDAO invitationDAO;
    @Autowired
    protected TransactionTypeDAO transactionTypeDAO;
    @Autowired
    protected TransactionDAO transactionDAO;

    public static final String CREATE = "Database did not assign ID for {0}";
    public static final String DELETE = "Database did not delete entity {0}";

    protected static void checkMethods(Class testClass, Class testingInterface) {
        Set<String> testMethods = new HashSet<>();
        for (Method m : testClass.getDeclaredMethods()) {
            if (m.isAnnotationPresent(Test.class)) {
                testMethods.add(m.getName());
            }
        }

        List<String> targetMethods = new ArrayList<>(
                Arrays.asList("create", "update", "getById", "delete", "getAll", "getClassType"));
        targetMethods.addAll(new ArrayList<>(Arrays.asList(testingInterface.getDeclaredMethods())).stream()
                .map(m -> m.getName()).collect(Collectors.toList()));
        testMethods.forEach(targetMethods::remove);

        Assert.assertEquals("Following method(s) are missing in DAO test :" + targetMethods.toString(), 0,
                targetMethods.size());
    }

    protected <T> List<T> toList(T element) {
        return Stream.of(element).collect(Collectors.toList());
    }

    protected String message(String key, Class clazz) {
        return MessageFormat.format(key, clazz.getSimpleName());
    }
}