remove final hamcrest usages

This commit is contained in:
Ken Stevens 2024-05-11 10:20:14 -04:00
parent 10ecf1318a
commit eaf5107396
2 changed files with 51 additions and 83 deletions

View File

@ -8,7 +8,6 @@ import org.reflections.Reflections;
import java.util.Set; import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
public class JsonBeanTest { public class JsonBeanTest {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(JsonBeanTest.class); private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(JsonBeanTest.class);
@ -22,7 +21,7 @@ public class JsonBeanTest {
assertThat(allJsonClasses).contains(CdsServiceJson.class); assertThat(allJsonClasses).contains(CdsServiceJson.class);
for (Class<? extends IModelJson> item : allJsonClasses) { for (Class<? extends IModelJson> item : allJsonClasses) {
assertThat(item, HasGetterOrSetterForAllJsonFields.hasGetterOrSetterForAllJsonFields()); HasGetterOrSetterForAllJsonFields.assertThatJsonProperties(item).hasGetterAndSetterForAllJsonFields();
} }
ourLog.info("Tested {} Json classes", allJsonClasses.size()); ourLog.info("Tested {} Json classes", allJsonClasses.size());

View File

@ -1,69 +1,53 @@
/*-
* #%L
* HAPI FHIR Test Utilities
* %%
* Copyright (C) 2014 - 2024 Smile CDR, Inc.
* %%
* Licensed 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.
* #L%
*/
package ca.uhn.test.util; package ca.uhn.test.util;
import ca.uhn.fhir.model.api.IModelJson; import ca.uhn.fhir.model.api.IModelJson;
import com.fasterxml.jackson.annotation.JsonProperty; import org.assertj.core.api.AbstractAssert;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.annotation.Nonnull;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.beans.BeanInfo; import java.beans.BeanInfo;
import java.beans.FeatureDescriptor;
import java.beans.IntrospectionException;
import java.beans.Introspector; import java.beans.Introspector;
import java.beans.IntrospectionException;
import java.beans.FeatureDescriptor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.ArrayList;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import static org.hamcrest.Matchers.hasItems; public class HasGetterOrSetterForAllJsonFields extends AbstractAssert<HasGetterOrSetterForAllJsonFields, Class<? extends IModelJson>> {
public class HasGetterOrSetterForAllJsonFields extends TypeSafeMatcher<Class<? extends IModelJson>> { private static final Logger LOGGER = LoggerFactory.getLogger(HasGetterOrSetterForAllJsonFields.class);
private static final Logger ourLog = LoggerFactory.getLogger(HasGetterOrSetterForAllJsonFields.class);
@Override public HasGetterOrSetterForAllJsonFields(Class<? extends IModelJson> actual) {
public void describeTo(Description description) { super(actual, HasGetterOrSetterForAllJsonFields.class);
description.appendText("All @JsonProperty annotated fields have getters and setters.");
} }
@Override public static HasGetterOrSetterForAllJsonFields assertThatJsonProperties(Class<? extends IModelJson> actual) {
protected boolean matchesSafely(Class<? extends IModelJson> item) { return new HasGetterOrSetterForAllJsonFields(actual);
List<String> jsonPropertyFields = getJsonPropertyFields(item); }
Matcher<Iterable<Object>> matcher = hasItems(jsonPropertyFields.toArray());
List<String> properties = getProperties(item); public HasGetterOrSetterForAllJsonFields hasGetterAndSetterForAllJsonFields() {
ourLog.info("{}: testing {} @JsonProperty fields", item.getSimpleName(), jsonPropertyFields.size()); isNotNull();
return matcher.matches(properties);
List<String> jsonPropertyFields = getJsonPropertyFields(actual);
List<String> properties = getProperties(actual);
LOGGER.info("{}: testing {} @JsonProperty fields", actual.getSimpleName(), jsonPropertyFields.size());
if (!properties.containsAll(jsonPropertyFields)) {
failWithMessage("Expected class <%s> to have getters and setters for all JSON property fields <%s>, but some were missing <%s>",
actual.getName(), jsonPropertyFields, properties);
}
return this;
} }
@Nonnull @Nonnull
private List<String> getJsonPropertyFields(Class<? extends IModelJson> item) { private List<String> getJsonPropertyFields(Class<? extends IModelJson> item) {
List<Field> fields = new ArrayList<>(); List<Field> fields = new ArrayList<>();
populateFields(fields, item); populateFields(fields, item);
return fields.stream() return fields.stream()
@ -77,14 +61,6 @@ public class HasGetterOrSetterForAllJsonFields extends TypeSafeMatcher<Class<? e
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
private boolean isNotCollection(Field theField) {
return !Collection.class.isAssignableFrom(theField.getType());
}
private boolean isNotMap(Field theField) {
return !Map.class.isAssignableFrom(theField.getType());
}
private boolean isJsonProperty(Field theField) { private boolean isJsonProperty(Field theField) {
if (!theField.isAnnotationPresent(JsonProperty.class)) { if (!theField.isAnnotationPresent(JsonProperty.class)) {
return false; return false;
@ -96,29 +72,26 @@ public class HasGetterOrSetterForAllJsonFields extends TypeSafeMatcher<Class<? e
return apiModelProperty == null || !apiModelProperty.hidden(); return apiModelProperty == null || !apiModelProperty.hidden();
} }
private String stripPrefix(String theFieldName) { private boolean isNotCollection(Field field) {
if (theFieldName.startsWith("my")) { return !java.util.Collection.class.isAssignableFrom(field.getType());
return theFieldName.substring(2, 3).toLowerCase() + theFieldName.substring(3);
}
return theFieldName;
} }
private String stripUnderscoreSuffix(String theFieldName) { private boolean isNotMap(Field field) {
if (theFieldName.endsWith("_")) { return !java.util.Map.class.isAssignableFrom(field.getType());
return theFieldName.substring(0, theFieldName.length() - 1);
}
return theFieldName;
} }
@Override private String stripPrefix(String fieldName) {
protected void describeMismatchSafely(Class<? extends IModelJson> item, Description mismatchDescription) { if (fieldName.startsWith("my")) {
mismatchDescription.appendText(" for class ").appendText(item.getName()).appendText(", "); return fieldName.substring(2, 3).toLowerCase() + fieldName.substring(3);
List<String> jsonFields = getJsonPropertyFields(item); }
Matcher<Iterable<Object>> matcher = hasItems(jsonFields.toArray()); return fieldName;
List<String> properties = getProperties(item); }
matcher.describeMismatch(properties, mismatchDescription);
mismatchDescription.appendText("\n All non-collection @JsonProperty fields: " + String.join(", ", jsonFields)); private String stripUnderscoreSuffix(String fieldName) {
mismatchDescription.appendText("\n Have get/set methods for: " + String.join(", ", properties)); if (fieldName.endsWith("_")) {
return fieldName.substring(0, fieldName.length() - 1);
}
return fieldName;
} }
private List<String> getProperties(Class<? extends IModelJson> item) { private List<String> getProperties(Class<? extends IModelJson> item) {
@ -135,19 +108,15 @@ public class HasGetterOrSetterForAllJsonFields extends TypeSafeMatcher<Class<? e
} }
} }
private String lowerCaseFirstLetter(String thePropertyName) { private String lowerCaseFirstLetter(String propertyName) {
return thePropertyName.substring(0, 1).toLowerCase() + thePropertyName.substring(1); return propertyName.substring(0, 1).toLowerCase() + propertyName.substring(1);
} }
private static void populateFields(List<Field> theFields, Class<?> theItem) { private static void populateFields(List<Field> fields, Class<?> item) {
theFields.addAll(Arrays.asList(theItem.getDeclaredFields())); fields.addAll(Arrays.asList(item.getDeclaredFields()));
if (theItem.getSuperclass() != null) { if (item.getSuperclass() != null) {
populateFields(theFields, theItem.getSuperclass()); populateFields(fields, item.getSuperclass());
} }
} }
public static HasGetterOrSetterForAllJsonFields hasGetterOrSetterForAllJsonFields() {
return new HasGetterOrSetterForAllJsonFields();
}
} }