/*
Copyright 2006 - 2010 Under Dusken
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.
*/
/**
* @author Erlend Hamnaberg<erlenha@underdusken.no>
* @author Marvin B, Lillehaug <lillehau@underdusken.no>
*/
package no.dusken.aranea.service;
import no.dusken.aranea.model.Section;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import static junit.framework.Assert.*;
import static org.junit.Assert.assertNull;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/META-INF/integrationTest.xml"})
@TransactionConfiguration
@Transactional
public class TestSectionServiceImpl {
@Autowired
private SectionService sectionService;
private Section section;
@Before
public void onSetup(){
List<Section> sections = sectionService.getSections(true);
for(Section s : sections) {
System.out.println("ost" + s.getName());
}
assertEquals("database not empty", 0, sections.size() );
section = new Section();
section.setName("test");
section.setUrl("test");
section.setActive(true);
section.setHidden(false);
section = sectionService.saveOrUpdate(section);
assertNotNull("Section was not saved, was null!", section);
Section s = new Section();
s.setName("child");
s.setUrl("child");
s.setActive(true);
s.setHidden(false);
s.setParent(section);
s = sectionService.saveOrUpdate(s);
}
@Test
public void testEntity() {
Section s = sectionService.getEntity(section.getID());
assertNotNull(s);
assertEquals(s, section);
}
@Test
public void testGetTopLevelSections() {
List<Section> sections = sectionService.getTopLevelSections(true);
assertEquals(sections.size(), 1);
}
/**
* A section should be described uniquely by its name and parent.
* Thus two sections can have the same name as long as they do not
* have the same parent.
*/
@Test(expected = IllegalArgumentException.class)
public void testUniqueSectionName(){
Section parent = new Section();
parent.setName("parent");
parent.setUrl("parent");
parent = sectionService.saveOrUpdate(parent);
Section s = new Section();
s.setName("section");
s.setUrl("section");
s.setParent(parent);
sectionService.saveOrUpdate(s);
Section s2 = new Section();
s2.setName("section");
s2.setUrl("section");
s2.setParent(parent);
assertNull("Section should not be saved", sectionService.saveOrUpdate(s2));
}
/**
* It should be possible to have two sections with the same name as long
* as they do not have the same parent.
*/
@Test
public void testUniqueSectionNameAndParent(){
Section parent = new Section();
parent.setName("parent");
parent.setUrl("parent");
parent = sectionService.saveOrUpdate(parent);
Section s = new Section();
s.setName("section");
s.setUrl("section");
s.setParent(parent);
s = sectionService.saveOrUpdate(s);
Section s2 = new Section();
s2.setName("section");
s2.setUrl("section");
s2.setParent(s);
assertNotNull("Section should be saved", sectionService.saveOrUpdate(s2));
}
/*
Since null is not interpreted as unique, this will not work
@Test
public void testUniqueSectionNameAndNoParent(){
Section s = new Section();
s.setName("section");
s.setUrl("section");
sectionService.saveOrUpdate(s);
Section s2 = new Section();
s2.setName("section");
s2.setUrl("section");
assertNull("Section should not be saved", sectionService.saveOrUpdate(s2));
} */
@Test
public void testgetTopLevelSectionsHidden(){
Section s = new Section();
s.setName("section");
s.setUrl("section");
s.setHidden(true);
sectionService.saveOrUpdate(s);
Section s2 = new Section();
s2.setName("section2");
s2.setUrl("section2");
sectionService.saveOrUpdate(s2);
Section s3 = new Section();
s3.setName("subsection");
s3.setUrl("subsection");
s3.setParent(s2);
sectionService.saveOrUpdate(s3);
List<Section> sections = sectionService.getTopLevelSections(true);
assertEquals(3, sections.size());
}
@Test
public void testgetSectionsHidden(){
Section s = new Section();
s.setName("section");
s.setUrl("section");
s.setHidden(true);
sectionService.saveOrUpdate(s);
Section s2 = new Section();
s2.setName("section2");
s2.setUrl("section2");
s2 = sectionService.saveOrUpdate(s2);
Section s3 = new Section();
s3.setName("subsection");
s3.setUrl("subsection");
s3.setParent(s2);
sectionService.saveOrUpdate(s3);
List<Section> sections = sectionService.getSections(true);
assertEquals(4, sections.size());
}
@Test
public void testgetTopLevelSectionsActive(){
Section s = new Section();
s.setName("section");
s.setUrl("section");
s.setActive(false);
s = sectionService.saveOrUpdate(s);
Section s2 = new Section();
s2.setName("section2");
s2.setUrl("section2");
s2.setActive(true);
s2 = sectionService.saveOrUpdate(s2);
Section s3 = new Section();
s3.setName("subsection");
s3.setUrl("subsection");
s3.setParent(s2);
s3.setActive(true);
s3 = sectionService.saveOrUpdate(s3);
List<Section> sections = sectionService.getTopLevelSections(false);
assertEquals(2, sections.size());
assertFalse(sections.contains(s));
assertTrue(sections.contains(s2));
assertFalse(sections.contains(s3));
}
@Test
public void testgetSectionsActive(){
Section s = new Section();
s.setName("section");
s.setUrl("section");
s.setActive(false);
s = sectionService.saveOrUpdate(s);
Section s2 = new Section();
s2.setName("section2");
s2.setUrl("section2");
s2.setActive(true);
s2 = sectionService.saveOrUpdate(s2);
Section s3 = new Section();
s3.setName("subsection");
s3.setUrl("subsection");
s3.setParent(s2);
s3.setActive(true);
s3 = sectionService.saveOrUpdate(s3);
List<Section> sections = sectionService.getSections(false);
assertEquals(4, sections.size());
assertFalse(sections.contains(s));
assertTrue(sections.contains(s2));
assertTrue(sections.contains(s3));
}
}