/*
* Copyright (c) 2014, Francis Galiegue (fgaliegue@gmail.com)
*
* This software is dual-licensed under:
*
* - the Lesser General Public License (LGPL) version 3.0 or, at your option, any
* later version;
* - the Apache Software License (ASL) version 2.0.
*
* The text of this file and of both licenses is available at the root of this
* project or, if you have the jar distribution, in directory META-INF/, under
* the names LGPL-3.0.txt and ASL-2.0.txt respectively.
*
* Direct link to the sources:
*
* - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt
* - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt
*/
package com.github.fge.jsonschema.keyword.special;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.TestUtils;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.processing.Processor;
import com.github.fge.jsonschema.core.report.ProcessingMessage;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.core.tree.CanonicalSchemaTree;
import com.github.fge.jsonschema.core.tree.JsonTree;
import com.github.fge.jsonschema.core.tree.SchemaTree;
import com.github.fge.jsonschema.core.tree.SimpleJsonTree;
import com.github.fge.jsonschema.core.tree.key.SchemaKey;
import com.github.fge.jsonschema.keyword.validator.KeywordValidator;
import com.github.fge.jsonschema.library.validator.CommonValidatorDictionary;
import com.github.fge.jsonschema.messages.JsonSchemaValidationBundle;
import com.github.fge.jsonschema.processors.data.FullData;
import com.github.fge.msgsimple.bundle.MessageBundle;
import com.github.fge.msgsimple.load.MessageBundles;
import com.google.common.collect.Lists;
import org.mockito.ArgumentCaptor;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.List;
import static com.github.fge.jsonschema.TestUtils.*;
import static com.github.fge.jsonschema.matchers.ProcessingMessageAssert.*;
import static org.mockito.Mockito.*;
import static org.testng.Assert.*;
public final class PatternKeywordTest
{
/*
* A special testing class is needed for all keywords which use the null
* digester, since we cannot feed them with a digest: its information
* comes with the validation data itself (the schema to be precise).
*/
private static final MessageBundle BUNDLE
= MessageBundles.getBundle(JsonSchemaValidationBundle.class);
private final Constructor<? extends KeywordValidator> constructor;
private final JsonNode testData;
public PatternKeywordTest()
throws IOException
{
constructor = CommonValidatorDictionary.get().entries().get("pattern");
testData = JsonLoader.fromResource("/keyword/special/pattern.json");
}
@Test
public void keywordExists()
{
assertNotNull(constructor, "no support for pattern??");
}
@DataProvider
public Iterator<Object[]> getValueTests()
{
final List<Object[]> list = Lists.newArrayList();
String msg;
JsonNode msgNode, msgData, msgParams;
for (final JsonNode node: testData) {
msgNode = node.get("message");
msgData = node.get("msgData");
msgParams = node.get("msgParams");
msg = msgNode == null ? null
: TestUtils.buildMessage(BUNDLE, msgNode.textValue(),
msgParams, msgData);
list.add(new Object[]{ node.get("schema"), node.get("data"), msg,
node.get("valid").booleanValue(), node.get("msgData") });
}
return list.iterator();
}
@Test(dataProvider = "getValueTests", dependsOnMethods = "keywordExists")
public void instancesAreValidatedCorrectly(final JsonNode schema,
final JsonNode node, final String msg,
final boolean valid, final ObjectNode msgData)
throws IllegalAccessException, InvocationTargetException,
InstantiationException, ProcessingException
{
final SchemaTree tree
= new CanonicalSchemaTree(SchemaKey.anonymousKey(), schema);
final JsonTree instance = new SimpleJsonTree(node);
final FullData data = new FullData(tree, instance);
final ProcessingReport report = mock(ProcessingReport.class);
@SuppressWarnings("unchecked")
final Processor<FullData, FullData> processor = mock(Processor.class);
// It is a null node which is ignored by the constructor, so we can
// do that
final KeywordValidator validator = constructor.newInstance(schema);
validator.validate(processor, report, BUNDLE, data);
if (valid) {
verify(report, never()).error(anyMessage());
return;
}
final ArgumentCaptor<ProcessingMessage> captor
= ArgumentCaptor.forClass(ProcessingMessage.class);
verify(report).error(captor.capture());
final ProcessingMessage message = captor.getValue();
assertMessage(message).isValidationError("pattern", msg)
.hasContents(msgData);
}
}