cz.swi2.mendeluis.dataaccesslayer.domain.PortletTest.java Source code

Java tutorial

Introduction

Here is the source code for cz.swi2.mendeluis.dataaccesslayer.domain.PortletTest.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.swi2.mendeluis.dataaccesslayer.domain;

import cz.swi2.mendeluis.dataaccesslayer.core.DatabaseConfig;
import cz.swi2.mendeluis.dataaccesslayer.repository.PortletRepository;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.varia.NullAppender;
import static org.junit.Assert.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Ivo
 */
@ContextConfiguration(classes = DatabaseConfig.class)
@TestExecutionListeners(TransactionalTestExecutionListener.class)
@Transactional
public class PortletTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private PortletRepository portletRepository;

    public PortletTest() {
        org.apache.log4j.BasicConfigurator.configure(new NullAppender());
    }

    @org.testng.annotations.Test
    public void testCreate() {
        // Create portlet
        Portlet portlet = new Portlet("Food menu xx");

        // Insert portlet
        this.portletRepository.insert(portlet);

        // Find portlet
        Portlet result = this.portletRepository.getById(portlet.getId());

        // assert equal
        assertEquals(portlet, result);
    }

    @org.testng.annotations.Test
    public void testGetByName() {
        // Create portlet
        String name = "Food menu xx";
        Portlet portlet = new Portlet(name);

        // Insert portlet
        this.portletRepository.insert(portlet);

        // Find portlet
        Portlet result = this.portletRepository.getByName(name);

        // assert equal
        assertEquals(portlet, result);
    }

    @org.testng.annotations.Test
    public void testUpdate() {
        // Create portlet
        Portlet portlet = new Portlet("Food menu xx");

        // Insert portlet
        this.portletRepository.insert(portlet);

        // Find portlet
        Portlet result = this.portletRepository.getById(portlet.getId());

        // try to update
        String description = "I am the best portlet in the world.";
        String filePath = "/abc.java";
        portlet.setDescription(description);
        portlet.setScriptFilePath(filePath);
        this.portletRepository.update(portlet);

        // Find portlet again
        result = this.portletRepository.getById(portlet.getId());

        // Assert updated value
        assertEquals(description, result.getDescription());
        assertEquals(filePath, result.getScriptFilePath());
    }

    @org.testng.annotations.Test
    public void testDelete() {
        // Create portlet
        Portlet portlet = new Portlet("Food menu xx");

        // Insert portlet
        this.portletRepository.insert(portlet);

        // Find portlet
        Portlet result = this.portletRepository.getById(portlet.getId());

        // try to delete
        this.portletRepository.delete(portlet);

        // Find portlet again
        result = this.portletRepository.getById(portlet.getId());

        // Assert updated value
        assertEquals(null, result);
    }

    @org.testng.annotations.Test
    public void testGetAll() {
        // Create portlets
        Portlet portlet = new Portlet("Food menu xx");
        Portlet portlet2 = new Portlet("Food menu xx 2");

        // Insert portlet
        this.portletRepository.insert(portlet);
        this.portletRepository.insert(portlet2);

        // Fetch all portlets
        List<Portlet> results = this.portletRepository.getAll();

        // Prepare a list to be compared
        List<Portlet> portlets = new ArrayList();
        portlets.add(portlet);
        portlets.add(portlet2);

        // We do not use a different db for testing. Thefore we must test only matching items, not all. 
        assertTrue(results.containsAll(portlets));
    }

}