TypeServiceTest.java Source code

Java tutorial

Introduction

Here is the source code for TypeServiceTest.java

Source

/*
*Copyright  2016 Dominik Szalai (emptulik@gmail.com)
*
*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.
*/
import cz.muni.fi.editor.commons.CommonsConfiguration;
import cz.muni.fi.editor.commons.FileSystemProvider;
import cz.muni.fi.editor.typemanager.TypeService;
import cz.muni.fi.editor.typemanager.TypeServiceImpl;
import cz.muni.fi.editor.typemanager.domain.PropertyDefinition;
import cz.muni.fi.editor.typemanager.domain.Type;
import lombok.extern.log4j.Log4j2;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;

import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static org.unitils.reflectionassert.ReflectionAssert.assertReflectionEquals;

/**
 * Created by Dominik Szalai - emptulik at gmail.com on 2.11.2016.
 */
@Log4j2
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles({ "test" })
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class })
public class TypeServiceTest {
    @Autowired
    private FileSystemProvider fileSystemProvider;
    @Autowired
    private TypeService typeService;

    @Before
    public void setUp() throws IOException {
        if (!Files.exists(fileSystemProvider.getFileSystem().getPath("/type-periodical.xml"))) {
            Files.copy(TypeServiceTest.class.getResourceAsStream("/type-periodical.xml"),
                    fileSystemProvider.getFileSystem().getPath("/type-periodical.xml"));
        }
    }

    @Test
    public void testLoad() {
        assertReflectionEquals(storedType(), typeService.load("type-periodical.xml"));
    }

    @Test(expected = IllegalArgumentException.class)
    public void testLoadCorrupted() {
        typeService.load("type-periodical-corrupted.xml");
    }

    @Test(expected = IllegalArgumentException.class)
    public void testIOException() {
        Path p = fileSystemProvider.getFileSystem().getPath("/demo");
        try {
            Files.createDirectory(p);
        } catch (IOException e) {

        }

        // reading from folder will trigger IOexception
        typeService.load("demo");
    }

    @Test
    public void testSave() {
        typeService.save("type-periodical-new.xml", storedType());

        Assert.assertTrue(Files.exists(fileSystemProvider.getFileSystem().getPath("/type-periodical-new.xml")));

        assertReflectionEquals(storedType(), typeService.load("type-periodical-new.xml"));
    }

    @Test
    public void testCreateBackup() {
        typeService.save("type-test.xml", storedType());
        simulateWait();
        typeService.save("type-test.xml", storedType());

        try (DirectoryStream<Path> s = Files.newDirectoryStream(fileSystemProvider.getFileSystem().getPath("/"))) {
            Iterable<Path> iterable = () -> s.iterator();

            Assert.assertEquals(2L, StreamSupport.stream(iterable.spliterator(), false)
                    .filter(p -> p.getFileName().toString().startsWith("type-test")).count());
        } catch (IOException ex) {
            log.fatal(ex);
        }
    }

    private void simulateWait() {
        long start = System.currentTimeMillis();
        long end = 0L;

        do {
            end = System.currentTimeMillis();
        } while (start + 2000 >= end);

    }

    private Type storedType() {
        Type t = new Type();
        t.setId("editor:periodical");
        t.setLocalName("Periodical");
        t.setLocalNamespace("http://editor.fi.muni.cz/");
        t.setParentId("cmis:document");
        t.setDisplayName("Periodical");
        t.setQueryName("editor:periodical");
        t.setDescription("Cmis type for periodical type");
        t.setBaseId("cmis:document");
        t.setCreatable(true);
        t.setFileable(true);
        t.setQueryable(true);
        t.setFulltextIndexed(false);
        t.setIncludedInSupertypeQuery(true);
        t.setControllablePolicy(false);
        t.setControllableACL(false);
        t.setVersionable(true);
        t.setContentStreamAllowed("required");

        PropertyDefinition pds = new PropertyDefinition();
        pds.setId("periodical:displayname");
        pds.setLocalName("Display name");
        pds.setDisplayName("Display name");
        pds.setQueryName("periodical:displayname");
        pds.setDescription("Name displayed when preiodical is viewed");
        pds.setPropertyType("string");
        pds.setCardinality("single");
        pds.setUpdatability("readwrite");
        pds.setInherited(false);
        pds.setRequired(true);
        pds.setQueryable(true);
        pds.setOrderable(false);

        t.setPropertyStringDefinition(Stream.of(pds).collect(Collectors.toList()));

        PropertyDefinition pddt = new PropertyDefinition();
        pddt.setId("periodical:issuedfrom");
        pddt.setLocalName("issuedfrom");
        pddt.setDisplayName("issuedfrom");
        pddt.setQueryName("periodical:issuedfrom");
        pddt.setDescription("Date specifiyng since when is periodical issued");
        pddt.setPropertyType("datetime");
        pddt.setCardinality("single");
        pddt.setUpdatability("readwrite");
        pddt.setInherited(false);
        pddt.setRequired(true);
        pddt.setQueryable(false);
        pddt.setOrderable(false);

        t.setPropertyDateTimeDefinition(Stream.of(pddt).collect(Collectors.toList()));

        PropertyDefinition pduri = new PropertyDefinition();
        pduri.setId("periodical:url");
        pduri.setLocalName("periodicalurl");
        pduri.setDisplayName("periodical url");
        pduri.setQueryName("periodical:url");
        pduri.setDescription("External URL of periodical");
        pduri.setPropertyType("uri");
        pduri.setCardinality("single");
        pduri.setUpdatability("readwrite");
        pduri.setInherited(false);
        pduri.setRequired(false);
        pduri.setQueryable(false);
        pduri.setOrderable(false);

        t.setPropertyUriDefinition(Stream.of(pduri).collect(Collectors.toList()));

        return t;
    }

    @Configuration
    @Import(CommonsConfiguration.class)
    static class ContextConfiguration {

        @Bean
        public TypeService typeService() {
            try {
                return new TypeServiceImpl();
            } catch (JAXBException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}