Java tutorial
/* * 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 cz.swi2.mendeluis.dataaccesslayer.repository.UserPortletRepository; import cz.swi2.mendeluis.dataaccesslayer.repository.UserRepository; 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 UserPortletTest extends AbstractTestNGSpringContextTests { @Autowired private UserPortletRepository userPortletRepository; @Autowired private PortletRepository portletRepository; @Autowired private UserRepository userRepository; public UserPortletTest() { org.apache.log4j.BasicConfigurator.configure(new NullAppender()); } @org.testng.annotations.Test public void testCreate() { // Create entities User u = new User("ivo", "ivo", "pw123456"); Portlet p = new Portlet("Food menu xx"); UserPortlet up = new UserPortlet(u, p, Placement.LEFT, 0); // Insert this.userRepository.insert(u); this.portletRepository.insert(p); this.userPortletRepository.insert(up); // Find portlet UserPortlet result = this.userPortletRepository.getById(up.getId()); // assert equal assertEquals(up, result); } @org.testng.annotations.Test public void testGetAll() { // Create entities User u = new User("ivo", "ivo", "pw123456"); Portlet p = new Portlet("Food menu xx"); Portlet p2 = new Portlet("Food menu xx 2"); UserPortlet up = new UserPortlet(u, p, Placement.LEFT, 0); UserPortlet up2 = new UserPortlet(u, p2, Placement.RIGHT, 0); // Insert this.userRepository.insert(u); this.portletRepository.insert(p); this.portletRepository.insert(p2); this.userPortletRepository.insert(up); this.userPortletRepository.insert(up2); // Fetch all user portlets List<UserPortlet> results = this.userPortletRepository.getAll(); // Prepare a list to be compared List<UserPortlet> portlets = new ArrayList(); portlets.add(up); portlets.add(up2); // We do not use a different db for testing. Thefore we must test only matching items, not all. assertTrue(results.containsAll(portlets)); } @org.testng.annotations.Test public void testGetAllByUser() { // Create entities User u = new User("ivo", "ivo", "pw123456"); Portlet p = new Portlet("Food menu xx"); Portlet p2 = new Portlet("Food menu xx 2"); UserPortlet up = new UserPortlet(u, p, Placement.LEFT, 0); UserPortlet up2 = new UserPortlet(u, p2, Placement.RIGHT, 0); // Insert this.userRepository.insert(u); this.portletRepository.insert(p); this.portletRepository.insert(p2); this.userPortletRepository.insert(up); this.userPortletRepository.insert(up2); // Find portlet List<UserPortlet> results = this.userPortletRepository.getAllByUser(u); // assert equal List<UserPortlet> original = new ArrayList(); original.add(up); original.add(up2); assertEquals(original, results); } @org.testng.annotations.Test public void testUpdate() { // Create entities User u = new User("ivo", "ivo", "pw123456"); Portlet p = new Portlet("Food menu xx"); UserPortlet up = new UserPortlet(u, p, Placement.LEFT, 0); // Insert this.userRepository.insert(u); this.portletRepository.insert(p); this.userPortletRepository.insert(up); // Find portlet UserPortlet result = this.userPortletRepository.getById(up.getId()); // try to update String description = "I am the best portlet in the world."; int orderIndex = 55; boolean hidden = true; Placement placement = Placement.TOP; up.setDescription(description); up.setHidden(hidden); up.setOrderIndex(orderIndex); up.setPlacement(placement); this.userPortletRepository.update(up); // Find portlet again result = this.userPortletRepository.getById(up.getId()); // Assert updated value assertEquals(description, result.getDescription()); assertEquals(orderIndex, result.getOrderIndex()); assertEquals(hidden, result.getHidden()); assertEquals(placement, result.getPlacement()); } @org.testng.annotations.Test public void testDelete() { // Create entities User u = new User("ivo", "ivo", "pw123456"); Portlet p = new Portlet("Food menu xx"); UserPortlet up = new UserPortlet(u, p, Placement.LEFT, 0); // Insert this.userRepository.insert(u); this.portletRepository.insert(p); this.userPortletRepository.insert(up); // Find portlet UserPortlet result = this.userPortletRepository.getById(up.getId()); // try to delete this.userPortletRepository.delete(result); // Find portlet again result = this.userPortletRepository.getById(up.getId()); // Assert updated value assertEquals(null, result); } }