All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nuiton.validator.ValidatorTestHelper Maven / Gradle / Ivy

package org.nuiton.validator;/*
 * #%L
 * Validation :: Test
 * %%
 * Copyright (C) 2021 Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import org.junit.Assert;

import java.io.File;
import java.util.List;
import java.util.Set;

/**
 * Helper methods to test the validator api.
 *
 * @author Tony Chemit - [email protected]
 * @since 2.0
 */
public class ValidatorTestHelper {

    public static File getBasedir() {

        // Search basedir from maven environment
        String basedirPath = System.getenv("basedir");
        if (basedirPath == null) {

            // hope the tests are running from the root of the module :)
            basedirPath = new File("").getAbsolutePath();
        }
        return new File(basedirPath);
    }

    public static void assertValidatorModel(NuitonValidator validator,
                                            String expectedContext,
                                            Class expectedType,
                                            NuitonValidatorScope... expectedScopes) {
        Assert.assertNotNull(validator);
        NuitonValidatorModel model = validator.getModel();
        Assert.assertNotNull(model);
        Assert.assertEquals(expectedContext, model.getContext());
        Assert.assertEquals(expectedType, model.getType());
        Set scopes = model.getScopes();
        for (NuitonValidatorScope expectedScope : expectedScopes) {
            Assert.assertTrue(scopes.contains(expectedScope));
        }
    }

    public static void assertValidatorEffectiveScopes(NuitonValidator validator,
                                                      NuitonValidatorScope... expectedScopes) {
        Assert.assertNotNull(validator);
        Set effectiveScopes = validator.getEffectiveScopes();
        Assert.assertEquals(expectedScopes.length, effectiveScopes.size());
        for (NuitonValidatorScope expectedScope : expectedScopes) {
            Assert.assertTrue(effectiveScopes.contains(expectedScope));
        }
    }

    public static void assertValidatorEffectiveFields(NuitonValidator validator,
                                                      String... expectedFields) {
        Assert.assertNotNull(validator);
        Set effectiveFields = validator.getEffectiveFields();
        Assert.assertEquals(expectedFields.length, effectiveFields.size());
        for (String expectedField : expectedFields) {
            Assert.assertTrue(effectiveFields.contains(expectedField));
        }
    }

    public static void assertValidatorEffectiveFields(NuitonValidator validator,
                                                      NuitonValidatorScope scope,
                                                      String... expectedFields) {
        Assert.assertNotNull(validator);
        Set effectiveFields = validator.getEffectiveFields(scope);
        Assert.assertEquals(expectedFields.length, effectiveFields.size());
        for (String expectedField : expectedFields) {
            Assert.assertTrue(effectiveFields.contains(expectedField));
        }
    }

    public static void assertFieldMessages(NuitonValidatorResult result,
                                           NuitonValidatorScope scope,
                                           String field,
                                           String... expectedMessages) {

        if (expectedMessages.length == 0) {

            // no messages
            boolean hasMessages = result.hasMessagesForScope(field, scope);
            Assert.assertFalse(hasMessages);

        } else {

            // with messages
            boolean hasMessages = result.hasMessagesForScope(field, scope);
            Assert.assertTrue(hasMessages);

            List messages = result.getMessagesForScope(field, scope);
            Assert.assertNotNull(messages);
            Assert.assertEquals(expectedMessages.length, messages.size());
            for (String expectedMessage : expectedMessages) {
                Assert.assertTrue(messages.contains(expectedMessage));
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy