package org.opennms.netmgt.xml.event;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import java.util.Date;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.junit.Before;
import org.junit.Test;
import org.opennms.core.test.MockLogAppender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EventValidationTest {
    private static final Logger LOG = LoggerFactory.getLogger(EventValidationTest.class);
    private static final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();

    @Before
    public void setUp() {
        MockLogAppender.setupLogging();
    }

    @Test
    public void testEmptyEvent() throws Exception {
        final Validator validator = factory.getValidator();
        final Event event = new Event();
        final Set<ConstraintViolation<Event>> errors = validator.validate(event);
        assertNull(event.getSource());
        assertEquals(2, errors.size());
    }

    @Test
    public void testEmptySource() throws Exception {
        final Validator validator = factory.getValidator();
        final Event event = new Event();
        event.setTime(new Date());
        final Set<ConstraintViolation<Event>> errors = validator.validate(event);
        assertNull(event.getSource());
        assertEquals(1, errors.size());
    }

    @Test
    public void testBadDbid() throws Exception {
        final Validator validator = factory.getValidator();
        final Event event = new Event();
        event.setSource("tests");
        event.setTime(new Date());
        event.setDbid(-1);
        final Set<ConstraintViolation<Event>> errors = validator.validate(event);
        assertEquals(1, errors.size());
    }

    @Test
    public void testBadState() throws Exception {
        final Validator validator = factory.getValidator();
        final Event event = new Event();
        event.setSource("tests");
        event.setTime(new Date());
        final Operaction action = new Operaction();
        action.setState("monkey");
        event.addOperaction(action);
        final Set<ConstraintViolation<Event>> errors = validator.validate(event);
        LOG.debug("errors: {}", errors);
        assertEquals(2, errors.size());
    }

}

@@ -1,8 +1,1210 @@
+/*******************************************************************************%0A * This file is part of OpenNMS(R).%0A *%0A * Copyright (C) 2017 The OpenNMS Group, Inc.%0A * OpenNMS(R) is Copyright (C) 1999-2017 The OpenNMS Group, Inc.%0A *%0A * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.%0A *%0A * OpenNMS(R) is free software: you can redistribute it and/or modify%0A * it under the terms of the GNU Affero General Public License as published%0A * by the Free Software Foundation, either version 3 of the License,%0A * or (at your option) any later version.%0A *%0A * OpenNMS(R) is distributed in the hope that it will be useful,%0A * but WITHOUT ANY WARRANTY; without even the implied warranty of%0A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the%0A * GNU Affero General Public License for more details.%0A *%0A * You should have received a copy of the GNU Affero General Public License%0A * along with OpenNMS(R).  If not, see:%0A *      http://www.gnu.org/licenses/%0A *%0A * For more information contact:%0A *     OpenNMS(R) Licensing %3Clicense@opennms.org%3E%0A *     http://www.opennms.org/%0A *     http://www.opennms.com/%0A *******************************************************************************/%0A%0A
 package 

add copyright header