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

Java tutorial

Introduction

Here is the source code for com.github.dactiv.fear.service.test.config.DataDictionaryServiceTest.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 DataDictionaryServiceTest 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 testGetDataDictionary() {
        Map<String, Object> dataDictionary = configService.getDataDictionary(1);
        assertEquals(dataDictionary.get("name"), "?");

        dataDictionary = configService.getDataDictionary(99);
        assertNull(dataDictionary);
    }

    @Test
    public void testGetDataDictionaries() {
        List<Map<String, Object>> dataDictionaries = configService.getDataDictionaries("STATE");
        assertEquals(dataDictionaries.size(), 3);

        dataDictionaries = configService.getDataDictionaries("xxx");
        assertEquals(dataDictionaries.size(), 0);
    }

    @Test
    public void testDeleteDataDictionaries() {

        int before = countRowsInTable("tb_data_dictionary");

        configService.deleteDataDictionaries(Lists.newArrayList(1, 2, 3, 4));

        int after = countRowsInTable("tb_data_dictionary");

        assertEquals(before - 3, after);
    }

    @Test
    public void testSaveDataDictionary() {
        Map<String, Object> dataDictionary = configService.getDataDictionary(1);

        dataDictionary.remove("id");
        dataDictionary.put("name", "?");
        dataDictionary.put("code", "RESET");
        dataDictionary.put("state", "reset");

        int before = countRowsInTable("tb_data_dictionary");
        configService.saveDataDictionary(dataDictionary);
        int after = countRowsInTable("tb_data_dictionary");

        assertEquals(before + 1, after);

        dataDictionary.put("name", "???");
        dataDictionary.put("value", "4");

        before = countRowsInTable("tb_data_dictionary");
        configService.saveDataDictionary(dataDictionary);
        after = countRowsInTable("tb_data_dictionary");

        assertEquals(before, after);

        Integer id = Casts.cast(dataDictionary.get("id"), Integer.class);
        dataDictionary = configService.getDataDictionary(id);

        assertEquals(dataDictionary.get("name"), "???");
        assertEquals(dataDictionary.get("value"), "4");

    }

    @Test
    public void testFindDataDictionaries() {

        Map<String, Object> filter = Maps.newHashMap();

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

        assertEquals(list.size(), 15);

        filter.put("name", "");
        list = configService.findDataDictionaries(filter);
        assertEquals(list.size(), 2);

        filter.put("fkCategoryId", 2);
        list = configService.findDataDictionaries(filter);
        assertEquals(list.size(), 2);

        filter.put("value", "2");
        list = configService.findDataDictionaries(filter);
        assertEquals(list.size(), 1);
    }

    @Test
    public void testFindDataDictionariesPage() {
        Map<String, Object> filter = Maps.newHashMap();
        PageRequest pageRequest = new PageRequest(0, 1);

        filter.put("name", "");
        filter.put("fkCategoryId", 2);
        filter.put("value", "2");

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

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