Skip to content

Commit

Permalink
Validate all web links in all classnames at once
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-bogastry committed Nov 8, 2023
1 parent 7cfd618 commit 13290d1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ private RuntimeValue<GetterAccessorsContainer> implementPathParameterValueGetter
}
}

getterAccessorsContainerRecorder.validateContainer(getterAccessorsContainer, entityType);
}
getterAccessorsContainerRecorder.validateContainer(getterAccessorsContainer);
}

return getterAccessorsContainer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
package io.quarkus.resteasy.reactive.links.deployment;

import static io.restassured.RestAssured.given;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;

import io.quarkus.runtime.util.ExceptionUtil;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;

public class RestLinksWithFailureInjectionTest {

@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(TestRecordNoId.class, TestResourceNoId.class));

@TestHTTPResource("recordsNoId")
String recordsUrl;
.withApplicationRoot(jar -> jar.addClasses(TestRecordNoId.class, TestResourceNoId.class)).assertException(t -> {
Throwable rootCause = ExceptionUtil.getRootCause(t);
assertThat(rootCause).isInstanceOf(IllegalStateException.class)
.hasMessageContaining("Cannot generate web links for the class(es) " +
"io.quarkus.resteasy.reactive.links.deployment.TestRecordNoId because it does not contain an `id` field");
});

@Test
void shouldReturnIllegalStateException() {
String response = given().when()
.get(recordsUrl + "/by-name/second_value")
.getBody().asString();
assertThat(response)
.contains("java.lang.IllegalStateException: Cannot generate web links for " +
"io.quarkus.resteasy.reactive.links.deployment.TestRecordNoId because the class does not contain an `id` field");
void validationFailed(){
// Should not be reached: verify
assertTrue(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* Utility class that allows us to easily find a {@code GetterAccessor} based on a type and a field name.
Expand All @@ -24,8 +25,8 @@ public void put(String className, String fieldName, GetterAccessor getterAccesso
}
}

public boolean containsClassName(String className) {
return getterAccessors.containsKey(className);
public Set<String> classNameSet(){
return getterAccessors.keySet();
}

public boolean containsClassNameAndFieldName(String className, String fieldName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,31 @@ public void addAccessor(RuntimeValue<GetterAccessorsContainer> container, String
}

/**
* Validates if the container contains an accessor for the given className and the field name id
* Validates if all classes the container contain an accessor for the field name id
* @throws IllegalStateException if no accessor for classname.id is found
*/
public void validateContainer(RuntimeValue<GetterAccessorsContainer> container, String className) {
public void validateContainer(RuntimeValue<GetterAccessorsContainer> container) {
boolean shouldThrow = false;
StringBuilder builder = new StringBuilder();
String mandatoryField = "id";
boolean containsClassName = container.getValue().containsClassName(className);
boolean containsFieldName = container.getValue().containsClassNameAndFieldName(className, mandatoryField);

if (containsClassName && !containsFieldName) {
throw new IllegalStateException("Cannot generate web links for " + className
+ " because the class does not contain an `id` field");
var allClassNames = container.getValue().classNameSet();
if(!allClassNames.isEmpty()){
builder.append("Cannot generate web links for the class(es) ");
}

for (String className: allClassNames){
boolean containsIdField = container.getValue().containsClassNameAndFieldName(className, mandatoryField);
if (!containsIdField){
shouldThrow = true;
builder.append(className);
builder.append(" ");
}
}

if (shouldThrow){
builder.append("because it does not contain an `id` field");
throw new IllegalStateException(builder.toString());
}
}
}

0 comments on commit 13290d1

Please sign in to comment.