com.github.dactiv.fear.service.test.config.DictionaryCategoryServiceTest.java Source code

Java tutorial

Introduction

Here is the source code for com.github.dactiv.fear.service.test.config.DictionaryCategoryServiceTest.java

Source

/*
 * Copyright 2015 dactiv
 *
 * 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 com.github.dactiv.fear.service.test.config;

import com.github.dactiv.fear.commons.Casts;
import com.github.dactiv.fear.commons.page.Page;
import com.github.dactiv.fear.commons.page.PageRequest;
import com.github.dactiv.fear.commons.test.ServiceTestCaseSupport;
import com.github.dactiv.fear.service.service.config.ConfigService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

/**
 * 
 *
 * @author maurice
 */
@ContextConfiguration("/applicationContext-core.xml")
public class DictionaryCategoryServiceTest extends ServiceTestCaseSupport {

    @Autowired
    private ConfigService configService;

    @Before
    public void before() throws SQLException {
        install("classpath:data/cleanup-data.sql", "classpath:data/insert-data.sql");
    }

    @Test
    public void testGetDictionaryCategory() {
        Map<String, Object> dictionaryCategory = configService.getDictionaryCategory(1);
        assertEquals(dictionaryCategory.get("code"), "STATE");

        dictionaryCategory = configService.getDictionaryCategory(99);
        assertNull(dictionaryCategory);
    }

    @Test
    public void testGetAllDictionaryCategories() {
        List<Map<String, Object>> list = configService.getAllDictionaryCategories();
        assertEquals(list.size(), 8);

        list = configService.getAllDictionaryCategories(Lists.newArrayList(1));
        assertEquals(list.size(), 7);

        list = configService.getAllDictionaryCategories(Lists.newArrayList(99));
        assertEquals(list.size(), 8);
    }

    @Test
    public void testSaveDictionaryCategory() {
        Map<String, Object> dictionaryCategory = configService.getDictionaryCategory(1);

        dictionaryCategory.remove("id");
        dictionaryCategory.put("name", "?");
        dictionaryCategory.put("code", "nationality");

        int before = countRowsInTable("tb_dictionary_category");

        configService.saveDictionaryCategory(dictionaryCategory);

        int after = countRowsInTable("tb_dictionary_category");

        assertEquals(before + 1, after);

        dictionaryCategory.put("name", "?");
        dictionaryCategory.put("code", "native-place");

        before = countRowsInTable("tb_dictionary_category");

        configService.saveDictionaryCategory(dictionaryCategory);

        after = countRowsInTable("tb_dictionary_category");

        assertEquals(before, after);

        Integer id = Casts.cast(dictionaryCategory.get("id"), Integer.class);
        configService.getDictionaryCategory(id);

        assertEquals(dictionaryCategory.get("name"), "?");
        assertEquals(dictionaryCategory.get("code"), "native-place");
    }

    @Test
    public void testDeleteDictionaryCategories() {

        int before = countRowsInTable("tb_dictionary_category");
        int beforeAssociation = countRowsInTable("tb_data_dictionary");

        configService.deleteDictionaryCategories(Lists.newArrayList(1));

        int after = countRowsInTable("tb_dictionary_category");
        int afterAssociation = countRowsInTable("tb_data_dictionary");

        assertEquals(before - 1, after);
        assertEquals(beforeAssociation - 3, afterAssociation);

    }

    @Test
    public void testFindDictionaryCategories() {
        Map<String, Object> filter = Maps.newHashMap();

        List<Map<String, Object>> list = configService.findDictionaryCategories(filter);

        assertEquals(list.size(), 8);

        filter.put("name", "?");

        list = configService.findDictionaryCategories(filter);

        assertEquals(list.size(), 1);

    }

    @Test
    public void testFindDictionaryCategoriesPage() {

        PageRequest pageRequest = new PageRequest(0, 1);

        Map<String, Object> filter = Maps.newHashMap();
        filter.put("name", "?");

        Page<Map<String, Object>> page = configService.findDictionaryCategories(pageRequest, filter);

        assertEquals(page.getTotalElements(), 1);
        assertEquals(page.hasNext(), false);
        assertEquals(page.hasContent(), true);
        assertEquals(page.getTotalPages(), 1);
        assertEquals(page.isFirst(), true);
    }
}