Skip to content

Commit

Permalink
fix: adjust resolver methods to ignore nullable values
Browse files Browse the repository at this point in the history
  • Loading branch information
bbortt committed Jul 8, 2024
1 parent fcac9de commit 92a5536
Show file tree
Hide file tree
Showing 25 changed files with 417 additions and 306 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,25 @@

package org.citrusframework.openapi.actions;

import java.net.URL;

import org.citrusframework.TestAction;
import org.citrusframework.TestActionBuilder;
import org.citrusframework.endpoint.Endpoint;
import org.citrusframework.exceptions.CitrusRuntimeException;
import org.citrusframework.http.client.HttpClient;
import org.citrusframework.openapi.OpenApiSpecification;
import org.citrusframework.spi.AbstractReferenceResolverAwareTestActionBuilder;
import org.citrusframework.spi.ReferenceResolver;
import org.citrusframework.spi.ReferenceResolverAware;
import org.citrusframework.util.ObjectHelper;

import java.net.URL;

/**
* Action executes client and server operations using given OpenApi specification.
* Action creates proper request and response data from given specification rules.
*
* @author Christoph Deppisch
* @since 4.1
*/
public class OpenApiActionBuilder implements TestActionBuilder.DelegatingTestActionBuilder<TestAction>, ReferenceResolverAware {

/** Bean reference resolver */
private ReferenceResolver referenceResolver;

private TestActionBuilder<?> delegate;
public class OpenApiActionBuilder extends AbstractReferenceResolverAwareTestActionBuilder<TestAction> {

private OpenApiSpecification specification;

Expand Down Expand Up @@ -155,24 +149,4 @@ public TestAction build() {
ObjectHelper.assertNotNull(delegate, "Missing delegate action to build");
return delegate.build();
}

@Override
public TestActionBuilder<?> getDelegate() {
return delegate;
}

/**
* Specifies the referenceResolver.
* @param referenceResolver
*/
@Override
public void setReferenceResolver(ReferenceResolver referenceResolver) {
if (referenceResolver == null) {
this.referenceResolver = referenceResolver;

if (delegate instanceof ReferenceResolverAware) {
((ReferenceResolverAware) delegate).setReferenceResolver(referenceResolver);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
package org.citrusframework.openapi.actions;

import org.citrusframework.TestAction;
import org.citrusframework.TestActionBuilder;
import org.citrusframework.endpoint.Endpoint;
import org.citrusframework.openapi.OpenApiSpecification;
import org.citrusframework.spi.AbstractReferenceResolverAwareTestActionBuilder;
import org.citrusframework.spi.ReferenceResolver;
import org.citrusframework.spi.ReferenceResolverAware;
import org.citrusframework.util.ObjectHelper;
import org.springframework.http.HttpStatus;

Expand All @@ -31,19 +30,14 @@
* @author Christoph Deppisch
* @since 4.1
*/
public class OpenApiClientActionBuilder implements TestActionBuilder.DelegatingTestActionBuilder<TestAction>, ReferenceResolverAware {
public class OpenApiClientActionBuilder extends AbstractReferenceResolverAwareTestActionBuilder<TestAction> {

private final OpenApiSpecification specification;

/** Bean reference resolver */
private ReferenceResolver referenceResolver;

/** Target http client instance */
private Endpoint httpClient;
private String httpClientUri;

private TestActionBuilder<?> delegate;

/**
* Default constructor.
*/
Expand Down Expand Up @@ -124,24 +118,4 @@ public TestAction build() {
ObjectHelper.assertNotNull(delegate, "Missing delegate action to build");
return delegate.build();
}

@Override
public TestActionBuilder<?> getDelegate() {
return delegate;
}

/**
* Specifies the referenceResolver.
* @param referenceResolver
*/
@Override
public void setReferenceResolver(ReferenceResolver referenceResolver) {
if (referenceResolver == null) {
this.referenceResolver = referenceResolver;

if (delegate instanceof ReferenceResolverAware) {
((ReferenceResolverAware) delegate).setReferenceResolver(referenceResolver);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
package org.citrusframework.openapi.actions;

import org.citrusframework.TestAction;
import org.citrusframework.TestActionBuilder;
import org.citrusframework.endpoint.Endpoint;
import org.citrusframework.openapi.OpenApiSpecification;
import org.citrusframework.spi.AbstractReferenceResolverAwareTestActionBuilder;
import org.citrusframework.spi.ReferenceResolver;
import org.citrusframework.spi.ReferenceResolverAware;
import org.citrusframework.util.ObjectHelper;
import org.springframework.http.HttpStatus;

Expand All @@ -31,19 +30,14 @@
* @author Christoph Deppisch
* @since 4.1
*/
public class OpenApiServerActionBuilder implements TestActionBuilder.DelegatingTestActionBuilder<TestAction>, ReferenceResolverAware {
public class OpenApiServerActionBuilder extends AbstractReferenceResolverAwareTestActionBuilder<TestAction> {

private final OpenApiSpecification specification;

/** Bean reference resolver */
private ReferenceResolver referenceResolver;

/** Target http client instance */
private Endpoint httpServer;
private String httpServerUri;

private TestActionBuilder<?> delegate;

/**
* Default constructor.
*/
Expand Down Expand Up @@ -125,24 +119,4 @@ public TestAction build() {
ObjectHelper.assertNotNull(delegate, "Missing delegate action to build");
return delegate.build();
}

@Override
public TestActionBuilder<?> getDelegate() {
return delegate;
}

/**
* Specifies the referenceResolver.
* @param referenceResolver
*/
@Override
public void setReferenceResolver(ReferenceResolver referenceResolver) {
if (referenceResolver == null) {
this.referenceResolver = referenceResolver;

if (delegate instanceof ReferenceResolverAware) {
((ReferenceResolverAware) delegate).setReferenceResolver(referenceResolver);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,28 @@

package org.citrusframework.openapi.actions;

import java.util.Map;

import org.citrusframework.TestActionBuilder;
import org.citrusframework.spi.AbstractReferenceResolverAwareTestActionBuilder;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.Map;

import static org.testng.Assert.assertTrue;

/**
* @author Christoph Deppisch
*/
public class OpenApiActionBuilderTest {

private OpenApiActionBuilder fixture;

@BeforeMethod
public void beforeMethod() {
fixture = new OpenApiActionBuilder();
}

@Test
public void shouldLookupTestActionBuilder() {
Map<String, TestActionBuilder<?>> endpointBuilders = TestActionBuilder.lookup();
Expand All @@ -35,4 +46,9 @@ public void shouldLookupTestActionBuilder() {
Assert.assertTrue(TestActionBuilder.lookup("openapi").isPresent());
Assert.assertEquals(TestActionBuilder.lookup("openapi").get().getClass(), OpenApiActionBuilder.class);
}

@Test
public void isReferenceResolverAwareTestActionBuilder() {
assertTrue(fixture instanceof AbstractReferenceResolverAwareTestActionBuilder<?>, "Is instanceof AbstractReferenceResolverAwareTestActionBuilder");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.citrusframework.openapi.actions;

import org.citrusframework.endpoint.Endpoint;
import org.citrusframework.openapi.OpenApiSpecification;
import org.citrusframework.spi.AbstractReferenceResolverAwareTestActionBuilder;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.mockito.Mockito.mock;
import static org.testng.Assert.assertTrue;

public class OpenApiClientActionBuilderTest {

private OpenApiClientActionBuilder fixture;

@BeforeMethod
public void beforeMethod() {
fixture = new OpenApiClientActionBuilder(mock(Endpoint.class), mock(OpenApiSpecification.class));
}

@Test
public void isReferenceResolverAwareTestActionBuilder() {
assertTrue(fixture instanceof AbstractReferenceResolverAwareTestActionBuilder<?>, "Is instanceof AbstractReferenceResolverAwareTestActionBuilder");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.citrusframework.openapi.actions;

import org.citrusframework.endpoint.Endpoint;
import org.citrusframework.openapi.OpenApiSpecification;
import org.citrusframework.spi.AbstractReferenceResolverAwareTestActionBuilder;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.mockito.Mockito.mock;
import static org.testng.Assert.assertTrue;

public class OpenApiServerActionBuilderTest {

private OpenApiServerActionBuilder fixture;

@BeforeMethod
public void beforeMethod() {
fixture = new OpenApiServerActionBuilder(mock(Endpoint.class), mock(OpenApiSpecification.class));
}

@Test
public void isReferenceResolverAwareTestActionBuilder() {
assertTrue(fixture instanceof AbstractReferenceResolverAwareTestActionBuilder<?>, "Is instanceof AbstractReferenceResolverAwareTestActionBuilder");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.citrusframework.spi;

import jakarta.annotation.Nullable;
import org.citrusframework.TestAction;
import org.citrusframework.TestActionBuilder;

public abstract class AbstractReferenceResolverAwareTestActionBuilder<T extends TestAction> implements TestActionBuilder.DelegatingTestActionBuilder<T>, ReferenceResolverAware {

/** Bean reference resolver */
protected ReferenceResolver referenceResolver;

protected TestActionBuilder<? extends T> delegate;

@Override
public TestActionBuilder<? extends T> getDelegate() {
return delegate;
}

/**
* Specifies the referenceResolver.
*/
@Override
public void setReferenceResolver(@Nullable ReferenceResolver referenceResolver) {
if (referenceResolver != null) {
this.referenceResolver = referenceResolver;

if (delegate instanceof ReferenceResolverAware referenceResolverAware) {
referenceResolverAware.setReferenceResolver(referenceResolver);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.citrusframework.spi;

import org.citrusframework.TestAction;
import org.citrusframework.TestActionBuilder;
import org.mockito.Mock;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.MockitoAnnotations.openMocks;
import static org.springframework.test.util.ReflectionTestUtils.getField;
import static org.springframework.test.util.ReflectionTestUtils.setField;

public class AbstractReferenceResolverAwareTestActionBuilderTest {

@Mock
private ReferenceResolver referenceResolver;

@Mock
private TestReferenceResolver referenceResolverAware;

private AbstractReferenceResolverAwareTestActionBuilder fixture;

private AutoCloseable openedMocks;

@BeforeMethod
public void beforeMethod() {
openedMocks = openMocks(this);

fixture = new AbstractReferenceResolverAwareTestActionBuilder() {

@Override
public TestAction build() {
throw new IllegalArgumentException("Test implementation!");
}
};

setField(fixture, "delegate", referenceResolverAware);
}

@Test
public void getDelegate() {
assertEquals(referenceResolverAware, fixture.getDelegate());
}

@Test
public void setReferenceResolver() {
fixture.setReferenceResolver(referenceResolver);

assertNotNull(getField(fixture, "referenceResolver"), "ReferenceResolver should be set");
verify(referenceResolverAware).setReferenceResolver(referenceResolver);
}

@Test
public void setReferenceResolver_doesNotPropagateToNonReferenceResolverAware() {
var testActionBuilder = mock(TestActionBuilder.class);
setField(fixture, "delegate", testActionBuilder);

fixture.setReferenceResolver(referenceResolver);

assertNotNull(getField(fixture, "referenceResolver"), "ReferenceResolver should be set");
verifyNoInteractions(referenceResolverAware);
verifyNoInteractions(testActionBuilder);
}

@Test
public void setReferenceResolver_ignoresNullReferenceResolver() {
fixture.setReferenceResolver(null);

assertNull(getField(fixture, "referenceResolver"), "ReferenceResolver should NOT be set");
verifyNoInteractions(referenceResolverAware);
}

@AfterMethod
public void afterMethod() throws Exception {
openedMocks.close();
}

private abstract static class TestReferenceResolver implements TestActionBuilder, ReferenceResolverAware {
}
}
Loading

0 comments on commit 92a5536

Please sign in to comment.