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 static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
public class JsonBeanTest {
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);
for (Class<? extends IModelJson> item : allJsonClasses) {
assertThat(item, HasGetterOrSetterForAllJsonFields.hasGetterOrSetterForAllJsonFields());
HasGetterOrSetterForAllJsonFields.assertThatJsonProperties(item).hasGetterAndSetterForAllJsonFields();
}
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;
import ca.uhn.fhir.model.api.IModelJson;
import com.fasterxml.jackson.annotation.JsonProperty;
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.assertj.core.api.AbstractAssert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.beans.BeanInfo;
import java.beans.FeatureDescriptor;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.IntrospectionException;
import java.beans.FeatureDescriptor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
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 ourLog = LoggerFactory.getLogger(HasGetterOrSetterForAllJsonFields.class);
private static final Logger LOGGER = LoggerFactory.getLogger(HasGetterOrSetterForAllJsonFields.class);
@Override
public void describeTo(Description description) {
description.appendText("All @JsonProperty annotated fields have getters and setters.");
public HasGetterOrSetterForAllJsonFields(Class<? extends IModelJson> actual) {
super(actual, HasGetterOrSetterForAllJsonFields.class);
}
@Override
protected boolean matchesSafely(Class<? extends IModelJson> item) {
List<String> jsonPropertyFields = getJsonPropertyFields(item);
Matcher<Iterable<Object>> matcher = hasItems(jsonPropertyFields.toArray());
List<String> properties = getProperties(item);
ourLog.info("{}: testing {} @JsonProperty fields", item.getSimpleName(), jsonPropertyFields.size());
return matcher.matches(properties);
public static HasGetterOrSetterForAllJsonFields assertThatJsonProperties(Class<? extends IModelJson> actual) {
return new HasGetterOrSetterForAllJsonFields(actual);
}
public HasGetterOrSetterForAllJsonFields hasGetterAndSetterForAllJsonFields() {
isNotNull();
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
private List<String> getJsonPropertyFields(Class<? extends IModelJson> item) {
List<Field> fields = new ArrayList<>();
populateFields(fields, item);
return fields.stream()
@ -77,14 +61,6 @@ public class HasGetterOrSetterForAllJsonFields extends TypeSafeMatcher<Class<? e
.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) {
if (!theField.isAnnotationPresent(JsonProperty.class)) {
return false;
@ -96,29 +72,26 @@ public class HasGetterOrSetterForAllJsonFields extends TypeSafeMatcher<Class<? e
return apiModelProperty == null || !apiModelProperty.hidden();
}
private String stripPrefix(String theFieldName) {
if (theFieldName.startsWith("my")) {
return theFieldName.substring(2, 3).toLowerCase() + theFieldName.substring(3);
}
return theFieldName;
private boolean isNotCollection(Field field) {
return !java.util.Collection.class.isAssignableFrom(field.getType());
}
private String stripUnderscoreSuffix(String theFieldName) {
if (theFieldName.endsWith("_")) {
return theFieldName.substring(0, theFieldName.length() - 1);
}
return theFieldName;
private boolean isNotMap(Field field) {
return !java.util.Map.class.isAssignableFrom(field.getType());
}
@Override
protected void describeMismatchSafely(Class<? extends IModelJson> item, Description mismatchDescription) {
mismatchDescription.appendText(" for class ").appendText(item.getName()).appendText(", ");
List<String> jsonFields = getJsonPropertyFields(item);
Matcher<Iterable<Object>> matcher = hasItems(jsonFields.toArray());
List<String> properties = getProperties(item);
matcher.describeMismatch(properties, mismatchDescription);
mismatchDescription.appendText("\n All non-collection @JsonProperty fields: " + String.join(", ", jsonFields));
mismatchDescription.appendText("\n Have get/set methods for: " + String.join(", ", properties));
private String stripPrefix(String fieldName) {
if (fieldName.startsWith("my")) {
return fieldName.substring(2, 3).toLowerCase() + fieldName.substring(3);
}
return fieldName;
}
private String stripUnderscoreSuffix(String fieldName) {
if (fieldName.endsWith("_")) {
return fieldName.substring(0, fieldName.length() - 1);
}
return fieldName;
}
private List<String> getProperties(Class<? extends IModelJson> item) {
@ -135,19 +108,15 @@ public class HasGetterOrSetterForAllJsonFields extends TypeSafeMatcher<Class<? e
}
}
private String lowerCaseFirstLetter(String thePropertyName) {
return thePropertyName.substring(0, 1).toLowerCase() + thePropertyName.substring(1);
private String lowerCaseFirstLetter(String propertyName) {
return propertyName.substring(0, 1).toLowerCase() + propertyName.substring(1);
}
private static void populateFields(List<Field> theFields, Class<?> theItem) {
theFields.addAll(Arrays.asList(theItem.getDeclaredFields()));
private static void populateFields(List<Field> fields, Class<?> item) {
fields.addAll(Arrays.asList(item.getDeclaredFields()));
if (theItem.getSuperclass() != null) {
populateFields(theFields, theItem.getSuperclass());
if (item.getSuperclass() != null) {
populateFields(fields, item.getSuperclass());
}
}
public static HasGetterOrSetterForAllJsonFields hasGetterOrSetterForAllJsonFields() {
return new HasGetterOrSetterForAllJsonFields();
}
}