Skip to content

Commit

Permalink
Fix XSD validation
Browse files Browse the repository at this point in the history
- Relocated gora-elasticsearch.xsd file to main resources
- Covered XSD validation with test
- Added gora-elasticsearch-mapping-invalid.xml file for test
  • Loading branch information
podorvanova committed Jan 7, 2021
1 parent 5020efc commit 05e8ed5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
Expand Up @@ -17,6 +17,7 @@
package org.apache.gora.elasticsearch.mapping;

import com.google.inject.ConfigurationException;
import org.apache.commons.io.IOUtils;
import org.apache.gora.elasticsearch.store.ElasticsearchStore;
import org.apache.gora.persistency.impl.PersistentBase;
import org.apache.gora.util.GoraException;
Expand All @@ -33,10 +34,9 @@
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Locale;

Expand Down Expand Up @@ -116,16 +116,17 @@ public void readMappingFile(InputStream inputStream) {
throw new GoraException("The mapping input stream is null!");
}

// Convert input stream to a string to use it a few times
String mappingStream = IOUtils.toString(inputStream, Charset.defaultCharset());

// XSD validation for XML file
File schemaFile = new File(XSD_MAPPING_FILE);
Source xmlFile = new StreamSource(inputStream);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(schemaFile);
Validator validator = schema.newValidator();
validator.validate(xmlFile);
LOG.info(xmlFile.getSystemId() + " is valid.");

Document document = saxBuilder.build(inputStream);
Source xmlSource = new StreamSource(IOUtils.toInputStream(mappingStream, Charset.defaultCharset()));
Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
.newSchema(new StreamSource(getClass().getClassLoader().getResourceAsStream(XSD_MAPPING_FILE)));
schema.newValidator().validate(xmlSource);
LOG.info(xmlSource.getSystemId() + " is valid.");

Document document = saxBuilder.build(IOUtils.toInputStream(mappingStream, Charset.defaultCharset()));
if (document == null) {
LOG.error("The mapping document is null!");
throw new GoraException("The mapping document is null!");
Expand Down
Expand Up @@ -21,6 +21,7 @@
import org.apache.gora.elasticsearch.mapping.ElasticsearchMapping;
import org.apache.gora.elasticsearch.mapping.Field;
import org.apache.gora.elasticsearch.utils.ElasticsearchParameters;
import org.apache.gora.examples.generated.EmployeeInt;
import org.apache.gora.store.DataStoreTestBase;
import org.apache.gora.util.GoraException;
import org.junit.Assert;
Expand Down Expand Up @@ -75,4 +76,13 @@ public void testLoadElasticsearchParameters() throws IOException {
Assert.assertEquals("username", parameters.getUsername());
Assert.assertEquals("password", parameters.getPassword());
}

@Test(expected = GoraException.class)
public void testInvalidXmlFile() throws Exception {
log.info("test method: testInvalidXmlFile");

Properties properties = new Properties();
properties.setProperty("gora.elasticsearch.mapping.file", "gora-elasticsearch-mapping-invalid.xml");
DataStoreTestBase.testDriver.createDataStore(String.class, EmployeeInt.class, properties);
}
}
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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.
-->

<gora-otd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="gora-elasticsearch.xsd">

<class name="org.apache.gora.examples.generated.Employee" keyClass="java.lang.String" index="frontier">
<!--
Remove type to test XSD validation.
<field name="name" docfield="name" type="text"/>
-->
<field name="name" docfield="name"/>
</class>

</gora-otd>

0 comments on commit 05e8ed5

Please sign in to comment.