package org.sboehm.gbf.pages.utils;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.sboehm.gbf.pages.PagePropertyTO;
import org.sboehm.gbf.pages.PageTO;
import org.sboehm.gbf.templates.PropertyType;

import java.util.List;

import static org.junit.Assert.*;

public class PageTOAssert {

    public static void assertPageTO(long id, String path, String title, PageTO pageTO) {
        assertEquals(Long.valueOf(id), pageTO.getId());
        assertPageTO(path, title, pageTO);
    }

    public static void assertPageTO(String path, String title, PageTO pageTO) {
        assertEquals(path, pageTO.getPath());
        assertEquals(title, pageTO.getTitle());
    }

    public static void assertPageTOTemplate(String name, int version, PageTO pageTO) {
        assertEquals(name, pageTO.getTemplateId().getName());
        assertEquals(Integer.valueOf(version), pageTO.getTemplateId().getVersion());
    }

    public static void assertPageTOContainsPageProperty(String name, PropertyType type, String value, PageTO pageTO) {
        List<PagePropertyTO> pagePropertyTOs = pageTO.getProperties();
        assertTrue(pagePropertyTOs.size() >= 1);
        for (PagePropertyTO pagePropertyTO : pagePropertyTOs) {
            if (!StringUtils.equals(name, pagePropertyTO.getName())) {
                continue;
            }
            if (!ObjectUtils.equals(type, pagePropertyTO.getType())) {
                continue;
            }
            if (!StringUtils.equals(value, pagePropertyTO.getValue())) {
                continue;
            }
            return;
        }
        fail("No page property with the name '" + name + "', the type '" + type + "' and the value '" + value + "' exists!");
    }

}
