Skip to content

Commit

Permalink
HSEARCH-2519 Reduce code duplication in DefaultElasticsearchSchemaVal…
Browse files Browse the repository at this point in the history
…idator

The volume of code does not change much, but it feels a bit cleaner.
This commit can be removed without impacting features.
  • Loading branch information
yrodiere authored and Sanne committed Jan 22, 2017
1 parent 0ea079c commit a16d369
Show file tree
Hide file tree
Showing 7 changed files with 404 additions and 437 deletions.

Large diffs are not rendered by default.

Expand Up @@ -20,44 +20,44 @@
public interface ElasticsearchValidationMessages {

@Message(
value = "Index '%1$s':"
value = "index '%1$s'"
)
String errorIntro(String indexName);
String indexContext(String name);

@Message(
value = "Index '%1$s', mapping '%2$s':"
value = "mapping '%1$s'"
)
String mappingErrorIntro(String indexName, String mappingName);
String mappingContext(String name);

@Message(
value = "Index '%1$s', mapping '%2$s', property '%3$s':"
value = "property '%1$s'"
)
String mappingErrorIntro(String indexName, String mappingName, String path);
String mappingPropertyContext(String path);

@Message(
value = "Index '%1$s', mapping '%2$s', property '%3$s', field '%4$s':"
value = "field '%1$s'"
)
String mappingErrorIntro(String indexName, String mappingName, String path, String field);
String mappingPropertyFieldContext(String name);

@Message(
value = "Index '%1$s', analyzer '%2$s':"
value = "analyzer '%1$s'"
)
String analyzerErrorIntro(String indexName, String analyzerName);
String analyzerContext(String name);

@Message(
value = "Index '%1$s', char filter '%2$s':"
value = "char filter '%1$s'"
)
String charFilterErrorIntro(String indexName, String charFilterName);
String charFilterContext(String name);

@Message(
value = "Index '%1$s', tokenizer '%2$s':"
value = "tokenizer '%1$s'"
)
String tokenizerErrorIntro(String indexName, String tokenizerName);
String tokenizerContext(String name);

@Message(
value = "Index '%1$s', token filter '%2$s':"
value = "token filter '%1$s'"
)
String tokenFilterErrorIntro(String indexName, String tokenizerName);
String tokenFilterContext(String name);

@Message(
value = "Missing type mapping"
Expand Down
Expand Up @@ -6,81 +6,29 @@
*/
package org.hibernate.search.elasticsearch.schema.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

final class ValidationContext {
private final String indexName;
private final List<ValidationContextElement> elements;

private final String mappingName;
private final String propertyPath;
private final String fieldName;

private final String analyzerName;
private final String charFilterName;
private final String tokenizerName;
private final String tokenFilterName;

public ValidationContext(String indexName, String mappingName, String propertyPath, String fieldName,
String analyzerName, String charFilterName, String tokenizerName, String tokenFilterName) {
public ValidationContext(Collection<ValidationContextElement> elements) {
super();
this.indexName = indexName;
this.mappingName = mappingName;
this.propertyPath = propertyPath;
this.fieldName = fieldName;
this.analyzerName = analyzerName;
this.charFilterName = charFilterName;
this.tokenizerName = tokenizerName;
this.tokenFilterName = tokenFilterName;
}

public String getIndexName() {
return indexName;
}

public String getMappingName() {
return mappingName;
}

public String getPropertyPath() {
return propertyPath;
}

public String getFieldName() {
return fieldName;
this.elements = Collections.unmodifiableList( new ArrayList<>( elements ) );
}


public String getAnalyzerName() {
return analyzerName;
}


public String getCharFilterName() {
return charFilterName;
}


public String getTokenizerName() {
return tokenizerName;
}


public String getTokenFilterName() {
return tokenFilterName;
public List<ValidationContextElement> getElements() {
return elements;
}

@Override
public boolean equals(Object obj) {
if ( obj != null && getClass().equals( obj.getClass() ) ) {
ValidationContext other = (ValidationContext) obj;
return Objects.equals( indexName, other.indexName )
&& Objects.equals( mappingName, other.mappingName )
&& Objects.equals( propertyPath, other.propertyPath )
&& Objects.equals( fieldName, other.fieldName )
&& Objects.equals( analyzerName, other.analyzerName )
&& Objects.equals( charFilterName, other.charFilterName )
&& Objects.equals( tokenizerName, other.tokenizerName )
&& Objects.equals( tokenFilterName, other.tokenFilterName );
return Objects.equals( elements, other.elements );
}
return false;
}
Expand All @@ -89,14 +37,7 @@ public boolean equals(Object obj) {
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Objects.hashCode( indexName );
result = prime * result + Objects.hashCode( mappingName );
result = prime * result + Objects.hashCode( propertyPath );
result = prime * result + Objects.hashCode( fieldName );
result = prime * result + Objects.hashCode( analyzerName );
result = prime * result + Objects.hashCode( charFilterName );
result = prime * result + Objects.hashCode( tokenizerName );
result = prime * result + Objects.hashCode( tokenFilterName );
result = prime * result + Objects.hashCode( elements );
return result;
}
}
@@ -0,0 +1,58 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.elasticsearch.schema.impl;

import java.util.Objects;

final class ValidationContextElement {

private final ValidationContextType type;
private final String name;

public ValidationContextElement(ValidationContextType type, String name) {
super();
this.type = type;
this.name = name;
}

public ValidationContextType getType() {
return type;
}

public String getName() {
return name;
}

@Override
public String toString() {
return new StringBuilder()
.append( type )
.append( "[" )
.append( name )
.append( "]" )
.toString();
}

@Override
public boolean equals(Object obj) {
if ( obj != null && getClass().equals( obj.getClass() ) ) {
ValidationContextElement other = (ValidationContextElement) obj;
return Objects.equals( type, other.type )
&& Objects.equals( name, other.name );
}
return false;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Objects.hashCode( type );
result = prime * result + Objects.hashCode( name );
return result;
}
}
@@ -0,0 +1,20 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.elasticsearch.schema.impl;

enum ValidationContextType {

INDEX,
MAPPING,
MAPPING_PROPERTY,
MAPPING_PROPERTY_FIELD,
ANALYZER,
CHAR_FILTER,
TOKENIZER,
TOKEN_FILTER;

}
Expand Up @@ -13,61 +13,22 @@
import java.util.List;
import java.util.Map;

import org.hibernate.search.util.StringHelper;

final class ValidationErrorCollector {

private String indexName;

private String mappingName;
private final Deque<String> currentPropertyPath = new ArrayDeque<String>();
private String fieldName;

private String analyzerName;
private String charFilterName;
private String tokenizerName;
private String tokenFilterName;
private final Deque<ValidationContextElement> currentContext = new ArrayDeque<ValidationContextElement>();

private final Map<ValidationContext, List<String>> messagesByContext = new LinkedHashMap<>();

public void setIndexName(String indexName) {
this.indexName = indexName;
}

public void setMappingName(String mappingName) {
this.mappingName = mappingName;
public void push(ValidationContextType contextType, String name) {
this.currentContext.addLast( new ValidationContextElement( contextType, name ) );
}

public void pushPropertyName(String propertyName) {
currentPropertyPath.addLast( propertyName );
}

public void popPropertyName() {
currentPropertyPath.removeLast();
}

public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}

public void setAnalyzerName(String analyzerName) {
this.analyzerName = analyzerName;
}

public void setCharFilterName(String charFilterName) {
this.charFilterName = charFilterName;
}

public void setTokenizerName(String tokenizerName) {
this.tokenizerName = tokenizerName;
}

public void setTokenFilterName(String tokenFilterName) {
this.tokenFilterName = tokenFilterName;
public void pop() {
this.currentContext.removeLast();
}

public void addError(String errorMessage) {
ValidationContext context = createContext();
ValidationContext context = new ValidationContext( currentContext );
List<String> messages = messagesByContext.get( context );
if ( messages == null ) {
messages = new ArrayList<>();
Expand All @@ -76,15 +37,6 @@ public void addError(String errorMessage) {
messages.add( errorMessage );
}

private ValidationContext createContext() {
return new ValidationContext(
indexName,
mappingName, StringHelper.join( currentPropertyPath, "." ), fieldName,
analyzerName,
charFilterName, tokenizerName, tokenFilterName
);
}

/**
* @return The collected messages mapped by their context.
*/
Expand Down
Expand Up @@ -548,9 +548,9 @@ public void multipleErrors_singleIndexManagers() throws Exception {
isException( ElasticsearchSchemaValidationException.class )
.withMessage( VALIDATION_FAILED_MESSAGE_ID )
.withMessage(
"\nIndex 'org.hibernate.search.elasticsearch.test.elasticsearchschemavalidationit$simpledateentity', mapping 'org.hibernate.search.elasticsearch.test.ElasticsearchSchemaValidationIT$SimpleDateEntity':"
"\nindex 'org.hibernate.search.elasticsearch.test.elasticsearchschemavalidationit$simpledateentity', mapping 'org.hibernate.search.elasticsearch.test.ElasticsearchSchemaValidationIT$SimpleDateEntity':"
+ "\n\tInvalid value for attribute 'dynamic'. Expected 'STRICT', actual is 'FALSE'"
+ "\nIndex 'org.hibernate.search.elasticsearch.test.elasticsearchschemavalidationit$simpledateentity', mapping 'org.hibernate.search.elasticsearch.test.ElasticsearchSchemaValidationIT$SimpleDateEntity', property 'myField':"
+ "\nindex 'org.hibernate.search.elasticsearch.test.elasticsearchschemavalidationit$simpledateentity', mapping 'org.hibernate.search.elasticsearch.test.ElasticsearchSchemaValidationIT$SimpleDateEntity', property 'myField':"
+ "\n\tInvalid value for attribute 'type'. Expected 'DATE', actual is 'STRING'"
)
.build()
Expand Down Expand Up @@ -598,16 +598,16 @@ public void multipleErrors_multipleIndexManagers() throws Exception {
isException( ElasticsearchSchemaValidationException.class )
.withMessage( VALIDATION_FAILED_MESSAGE_ID )
.withMessage(
"\nIndex 'org.hibernate.search.elasticsearch.test.elasticsearchschemavalidationit$simplebooleanentity', mapping 'org.hibernate.search.elasticsearch.test.ElasticsearchSchemaValidationIT$SimpleBooleanEntity':"
"\nindex 'org.hibernate.search.elasticsearch.test.elasticsearchschemavalidationit$simplebooleanentity', mapping 'org.hibernate.search.elasticsearch.test.ElasticsearchSchemaValidationIT$SimpleBooleanEntity':"
+ "\n\tInvalid value for attribute 'dynamic'. Expected 'STRICT', actual is 'FALSE'"
)
.withSuppressed(
isException( ElasticsearchSchemaValidationException.class )
.withMessage( VALIDATION_FAILED_MESSAGE_ID )
.withMessage(
"\nIndex 'org.hibernate.search.elasticsearch.test.elasticsearchschemavalidationit$simpledateentity', mapping 'org.hibernate.search.elasticsearch.test.ElasticsearchSchemaValidationIT$SimpleDateEntity':"
"\nindex 'org.hibernate.search.elasticsearch.test.elasticsearchschemavalidationit$simpledateentity', mapping 'org.hibernate.search.elasticsearch.test.ElasticsearchSchemaValidationIT$SimpleDateEntity':"
+ "\n\tInvalid value for attribute 'dynamic'. Expected 'STRICT', actual is 'FALSE'"
+ "\nIndex 'org.hibernate.search.elasticsearch.test.elasticsearchschemavalidationit$simpledateentity', mapping 'org.hibernate.search.elasticsearch.test.ElasticsearchSchemaValidationIT$SimpleDateEntity', property 'myField':"
+ "\nindex 'org.hibernate.search.elasticsearch.test.elasticsearchschemavalidationit$simpledateentity', mapping 'org.hibernate.search.elasticsearch.test.ElasticsearchSchemaValidationIT$SimpleDateEntity', property 'myField':"
+ "\n\tInvalid value for attribute 'type'. Expected 'DATE', actual is 'STRING'"
)
.build()
Expand Down

0 comments on commit a16d369

Please sign in to comment.