From 3bdf90388bc1048bb278ae054b4e8aece0434030 Mon Sep 17 00:00:00 2001 From: co-mmer <183944322+co-mmer@users.noreply.github.com> Date: Fri, 25 Oct 2024 12:37:25 +0200 Subject: [PATCH] v1.2.0 --- .github/workflows/build-and-analyze.yml | 37 ++++ CHANGELOG.md | 11 ++ README.md | 118 +++++++++++- pom.xml | 8 +- .../aaamockmvc/test/web/act/TestAct2.java | 61 ++---- .../aaamockmvc/test/web/act/TestActImpl.java | 101 +++------- .../test/web/answer/TestAnswer.java | 61 ++++++ .../test/web/answer/TestAnswerImpl.java | 113 +++++++++++ .../answer/exception/TestAnswerException.java | 24 +++ .../content/TestAssertContentImpl.java | 28 ++- .../TestGenericMapper.java} | 74 ++++--- .../TestGenericMapperConfigurer.java} | 10 +- .../TestGenericMapperException.java} | 8 +- .../test/web/act/TestActImplTest.java | 158 ++------------- .../test/web/answer/TestAnswerTest.java | 181 ++++++++++++++++++ .../content/TestAssertContentImplTest.java | 44 ++--- .../mapper/TestAssertObjectMapperTest.java | 55 ------ .../TestGenericMapperConfigurerTest.java | 57 ++++++ .../TestGenericMapperTest.java} | 85 ++++++-- .../testutil/TestObjectDeserializer.java | 4 +- .../testdata/testutil/TestValue.java | 2 + 21 files changed, 810 insertions(+), 430 deletions(-) create mode 100644 .github/workflows/build-and-analyze.yml create mode 100644 src/main/java/io/github/co_mmer/aaamockmvc/test/web/answer/TestAnswer.java create mode 100644 src/main/java/io/github/co_mmer/aaamockmvc/test/web/answer/TestAnswerImpl.java create mode 100644 src/main/java/io/github/co_mmer/aaamockmvc/test/web/answer/exception/TestAnswerException.java rename src/main/java/io/github/co_mmer/aaamockmvc/test/web/{asserts/mapper/TestAssertResultMapper.java => mapper/TestGenericMapper.java} (69%) rename src/main/java/io/github/co_mmer/aaamockmvc/test/web/{asserts/mapper/TestAssertObjectMapper.java => mapper/TestGenericMapperConfigurer.java} (85%) rename src/main/java/io/github/co_mmer/aaamockmvc/test/web/{asserts/mapper/exception/TestAssertResultMapperException.java => mapper/exception/TestGenericMapperException.java} (72%) create mode 100644 src/test/java/io/github/co_mmer/aaamockmvc/test/web/answer/TestAnswerTest.java delete mode 100644 src/test/java/io/github/co_mmer/aaamockmvc/test/web/asserts/mapper/TestAssertObjectMapperTest.java create mode 100644 src/test/java/io/github/co_mmer/aaamockmvc/test/web/mapper/TestGenericMapperConfigurerTest.java rename src/test/java/io/github/co_mmer/aaamockmvc/test/web/{asserts/mapper/TestAssertResultMapperTest.java => mapper/TestGenericMapperTest.java} (65%) diff --git a/.github/workflows/build-and-analyze.yml b/.github/workflows/build-and-analyze.yml new file mode 100644 index 0000000..69e3204 --- /dev/null +++ b/.github/workflows/build-and-analyze.yml @@ -0,0 +1,37 @@ +name: Java CI with Maven and SonarCloud + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + types: [ opened, synchronize, reopened ] + +jobs: + build-and-analyze: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + java-version: '17' + distribution: 'zulu' + + - name: Cache Maven and SonarCloud packages + uses: actions/cache@v4 + with: + path: | + ~/.m2 + ~/.sonar/cache + key: ${{ runner.os }}-m2-sonar-${{ hashFiles('**/pom.xml') }} + + - name: Build and analyze with Maven + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=co-mmer_aaa-mockmvc -PcoverageReport \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index e107f40..2676f1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,15 @@ # Changelog +## [1.2.0] + +### Added + +- **TestAnswer**: Introduced for accessing the results of HTTP requests. + - New method `answer()` in TestAct for retrieving the result of the executed request. + +--- + ## [1.1.0] ### Added @@ -23,6 +32,8 @@ - `assertStatusIsAccessUnauthorized()` - `assertStatusInRange(int minStatusCode, int maxStatusCode);` +--- + ## [1.0.0] - Initial release of the **AAA-MockMvc** diff --git a/README.md b/README.md index 4ca6106..6cbbdab 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,16 @@ - +
- +
iconaaa-mockmvc-icon

AAA-MockMvc

+[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=co-mmer_aaa-mockmvc&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=co-mmer_aaa-mockmvc) +[![SQALE Rating](https://sonarcloud.io/api/project_badges/measure?project=co-mmer_aaa-mockmvc&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=co-mmer_aaa-mockmvc) +[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=co-mmer_aaa-mockmvc&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=co-mmer_aaa-mockmvc) +[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=co-mmer_aaa-mockmvc&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=co-mmer_aaa-mockmvc) +[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=co-mmer_aaa-mockmvc&metric=code_smells)](https://sonarcloud.io/summary/new_code?id=co-mmer_aaa-mockmvc) + ## Overview This project provides a framework for structuring unit tests following the AAA (Arrange, Act, @@ -37,7 +43,7 @@ documentation of the classes. io.github.co-mmer aaa-mockmvc - 1.1.0 + 1.2.0 test @@ -262,6 +268,9 @@ In the provided library, every test follows the AAA structure using the followin 2. **Act**: Perform the operation (e.g., make the API request). 3. **Assert**: Validate the result (e.g., check HTTP status, response content). +Optionally, the result of the request can be accessed using the `answer()` method, allowing for +further examination and validation of the response. + ### Arrange Section
@@ -1009,6 +1018,109 @@ specific `ResultMatcher` that verifies if the specified cookie is present in the
+### Answer Section + +
+Answer ResultActions + +### Retrieve Result Actions + +This method retrieves the ResultActions from the executed HTTP request, allowing detailed +examination and validation of the response. + +``` + var answer = get() + ... + .act() + .actPerform() + .answer() + .answerAsResultActions(); +``` + +--- + +
+ +
+Answer String + +### Retrieve Response as String + +This method retrieves the content of the HTTP response as a String. + +``` + var answer = get() + ... + .act() + .actPerform() + .answer() + .answerAsString(); +``` + +--- + +
+ +
+Answer Byte + +### Retrieve Response as Byte Array + +This method retrieves the content of the HTTP response as a byte array. + +``` + var answer = get() + ... + .act() + .actPerform() + .answer() + .answerAsByte(); +``` + +--- + +
+ +
+Answer Header + +### Retrieve Specific Response Header + +This method retrieves the value of a specific response header. + +``` + var answer = get() + ... + .act() + .actPerform() + .answer() + .answerHeader(KEY); +``` + +--- + +
+ + +
+Answer Void + +### Execute and Discard Response Content + +This method retrieves the result of the HTTP request without returning any content. It is used when +the response is not needed. + +``` + get() + ... + .act() + .actPerform() + .answer() + .answerVoid(); +``` + +
+ --- ## Examples diff --git a/pom.xml b/pom.xml index 173a168..88ee0ba 100755 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.github.co-mmer aaa-mockmvc - 1.1.0 + 1.2.0 jar AAA-MockMvc @@ -51,9 +51,9 @@ 17 - com.github.co-mmer + co-mmer https://sonarcloud.io - aaa-mockmvc + co-mmer_aaa-mockmvc @@ -135,6 +135,7 @@ org.apache.maven.plugins maven-javadoc-plugin + 3.10.1 attach-javadocs @@ -178,6 +179,7 @@ org.jacoco jacoco-maven-plugin + 0.8.12 prepare-agent diff --git a/src/main/java/io/github/co_mmer/aaamockmvc/test/web/act/TestAct2.java b/src/main/java/io/github/co_mmer/aaamockmvc/test/web/act/TestAct2.java index 56085bd..6756226 100644 --- a/src/main/java/io/github/co_mmer/aaamockmvc/test/web/act/TestAct2.java +++ b/src/main/java/io/github/co_mmer/aaamockmvc/test/web/act/TestAct2.java @@ -1,9 +1,8 @@ package io.github.co_mmer.aaamockmvc.test.web.act; import io.github.co_mmer.aaamockmvc.test.web.act.exception.TestActException; +import io.github.co_mmer.aaamockmvc.test.web.answer.TestAnswer; import io.github.co_mmer.aaamockmvc.test.web.asserts.TestAssert; -import org.springframework.lang.Nullable; -import org.springframework.test.web.servlet.ResultActions; /** * This interface represents a contract for performing actions and obtaining results from HTTP @@ -17,59 +16,25 @@ public interface TestAct2 { /** - * Retrieves the {@link ResultActions} from the executed HTTP request. + * Retrieves the {@link TestAssert} instance for asserting the response of the HTTP request. * - * @return the result actions of the request - * @throws TestActException if an error occurs while retrieving the result actions - * @since 1.0.0 - */ - ResultActions resultActions() throws TestActException; - - /** - * Retrieves the response content as a string from the executed HTTP request. - * - * @return the response content as a string - * @throws TestActException if an error occurs while retrieving the response content - * @since 1.0.0 - */ - String resultAsString() throws TestActException; - - /** - * Retrieves the response content as a byte array from the executed HTTP request. - * - * @return the response content as a byte array - * @throws TestActException if an error occurs while retrieving the response content - * @since 1.0.0 - */ - byte[] resultAsByte() throws TestActException; - - /** - * Executes the HTTP request without returning any content. + *

This method allows for further validation of the result using various assertion methods, + * enabling comprehensive checks on the HTTP response to ensure it meets expected criteria. * - * @throws TestActException if an error occurs during the execution - * @since 1.0.0 - */ - void resultVoid() throws TestActException; - - /** - * Retrieves the value of a specified response header from the executed HTTP request. - * - * @param key the name of the response header to retrieve - * @return the value of the response header, or {@code null} if the header is not present - * @throws TestActException if an error occurs while retrieving the header value + * @return a {@code TestAssert} instance for asserting the result of the request + * @throws TestActException if an error occurs while performing the request * @since 1.0.0 */ - @Nullable - String resultHeader(String key) throws TestActException; + TestAssert asserts() throws TestActException; /** - * Returns an instance of {@link TestAssert} for asserting the response of the HTTP request. + * Retrieves the {@link TestAnswer} instance for the executed HTTP request. * - *

This method allows for further validation of the result using various assertion methods. + *

This method provides access to the response content and other aspects of the request's + * outcome, enabling further validation and examination of the HTTP response. * - * @return a {@code TestAssert} instance for asserting the result of the request - * @throws TestActException if an error occurs while performing the request - * @since 1.0.0 + * @return a {@code TestAnswer} instance for accessing the result of the request + * @since 1.2.0 */ - TestAssert asserts() throws TestActException; + TestAnswer answer(); } diff --git a/src/main/java/io/github/co_mmer/aaamockmvc/test/web/act/TestActImpl.java b/src/main/java/io/github/co_mmer/aaamockmvc/test/web/act/TestActImpl.java index 298262b..3d6e618 100644 --- a/src/main/java/io/github/co_mmer/aaamockmvc/test/web/act/TestActImpl.java +++ b/src/main/java/io/github/co_mmer/aaamockmvc/test/web/act/TestActImpl.java @@ -2,24 +2,26 @@ import io.github.co_mmer.aaamockmvc.test.web.act.exception.TestActException; import io.github.co_mmer.aaamockmvc.test.web.act.strategy.TestRequestStrategyFactory; +import io.github.co_mmer.aaamockmvc.test.web.answer.TestAnswer; +import io.github.co_mmer.aaamockmvc.test.web.answer.TestAnswerImpl; import io.github.co_mmer.aaamockmvc.test.web.asserts.TestAssert; import io.github.co_mmer.aaamockmvc.test.web.asserts.TestAssertImpl; import io.github.co_mmer.aaamockmvc.test.web.request.context.TestRequestBean; import io.github.co_mmer.aaamockmvc.test.web.request.context.TestRequestContext; import io.github.co_mmer.aaamockmvc.test.web.request.model.TestRequestDto; -import java.io.UnsupportedEncodingException; import org.springframework.lang.NonNull; -import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; /** - * This class represents the implementation of the HTTP request execution process in the testing - * context. It provides methods for configuring, executing, and retrieving results from HTTP - * requests. + * This class is responsible for executing HTTP requests and providing access to assertions and + * results within a testing context. It allows users to assert the response of the HTTP request + * through the {@link #asserts()} method and access the results via the {@link #answer()} method. * - *

Using the {@code MockMvc} framework, this class simulates various HTTP requests defined by the - * {@link TestRequestDto} and allows for result extraction and validation. + *

Utilizing a {@link TestRequestContext}, it manages request data and integrates with the {@link + * MockMvc} framework to perform the actual HTTP requests, enabling comprehensive testing + * strategies. * * @since 1.0.0 */ @@ -27,6 +29,7 @@ public final class TestActImpl implements TestAct1, TestAct2 { private final TestRequestBean bean; private final TestRequestDto request; + private final TestRequestContext context; private MockHttpServletRequestBuilder requestBuilder; /** @@ -41,6 +44,7 @@ public final class TestActImpl implements TestAct1, TestAct2 { * @since 1.0.0 */ public TestActImpl(@NonNull TestRequestContext context) { + this.context = context; this.request = context.request(); this.bean = context.bean(); } @@ -60,89 +64,40 @@ public TestAct2 actPerform() { } /** - * Performs the HTTP request and returns the actions result. + * Retrieves the {@link TestAssert} instance for asserting the response of the HTTP request. * - * @return the {@link ResultActions} for the performed request - * @throws TestActException if an error occurs while performing the request - * @since 1.0.0 - */ - @Override - public ResultActions resultActions() throws TestActException { - try { - return this.bean.mvc().perform(this.requestBuilder); - } catch (Exception e) { - throw new TestActException(e); - } - } - - /** - * Performs the HTTP request but does not return the result. + *

This method allows for further validation of the result using various assertion methods, + * enabling comprehensive checks on the HTTP response to ensure it meets expected criteria. * + * @return a {@code TestAssert} instance for asserting the result of the request * @throws TestActException if an error occurs while performing the request * @since 1.0.0 */ @Override - public void resultVoid() throws TestActException { - resultActions(); + public TestAssert asserts() throws TestActException { + var resultActions = resultActions(); + return new TestAssertImpl(resultActions, this.bean.objectMapper()); } - /** - * Retrieves the content of the response as a string. - * - * @return the response content as a string - * @throws TestActException if an error occurs while retrieving the content - * @since 1.0.0 - */ - @Override - public String resultAsString() throws TestActException { + private ResultActions resultActions() throws TestActException { try { - return getResponse().getContentAsString(); - } catch (UnsupportedEncodingException e) { + return this.bean.mvc().perform(this.requestBuilder); + } catch (Exception e) { throw new TestActException(e); } } - private MockHttpServletResponse getResponse() throws TestActException { - return resultActions().andReturn().getResponse(); - } - - /** - * Retrieves the response as a byte array. - * - * @return the response content as a byte array - * @throws TestActException if an error occurs while retrieving the response - * @since 1.0.0 - */ - @Override - public byte[] resultAsByte() throws TestActException { - return getResponse().getContentAsByteArray(); - } - - /** - * Retrieves the value of a specific header from the response. - * - * @param key the name of the header to retrieve (must not be {@code null}) - * @return the value of the specified header, or {@code null} if not present - * @throws TestActException if an error occurs while retrieving the header - * @since 1.0.0 - */ - @Override - public String resultHeader(String key) throws TestActException { - return getResponse().getHeader(key); - } - /** - * Returns an instance of {@link TestAssert} for asserting the response of the HTTP request. + * Retrieves the {@link TestAnswer} instance for the executed HTTP request. * - *

This method allows for further validation of the result using various assertion methods. + *

This method provides access to the response content and other aspects of the request's + * outcome, enabling further validation and examination of the HTTP response. * - * @return a {@code TestAssert} instance for asserting the result of the request - * @throws TestActException if an error occurs while performing the request - * @since 1.0.0 + * @return a {@code TestAnswer} instance for accessing the result of the request + * @since 1.2.0 */ @Override - public TestAssert asserts() throws TestActException { - var resultActions = resultActions(); - return new TestAssertImpl(resultActions, this.bean.objectMapper()); + public TestAnswer answer() { + return new TestAnswerImpl(this.context, this.requestBuilder); } } diff --git a/src/main/java/io/github/co_mmer/aaamockmvc/test/web/answer/TestAnswer.java b/src/main/java/io/github/co_mmer/aaamockmvc/test/web/answer/TestAnswer.java new file mode 100644 index 0000000..be1a454 --- /dev/null +++ b/src/main/java/io/github/co_mmer/aaamockmvc/test/web/answer/TestAnswer.java @@ -0,0 +1,61 @@ +package io.github.co_mmer.aaamockmvc.test.web.answer; + +import io.github.co_mmer.aaamockmvc.test.web.answer.exception.TestAnswerException; +import org.springframework.lang.Nullable; +import org.springframework.test.web.servlet.ResultActions; + +/** + * This interface defines methods for handling responses from HTTP requests in a testing context. It + * provides various ways to retrieve the results of executed HTTP requests, including response + * content as strings, byte arrays, and headers. + * + * @since 1.2.0 + */ +public interface TestAnswer { + + /** + * Retrieves the {@link ResultActions} from the executed HTTP request. + * + * @return the result actions of the request + * @throws TestAnswerException if an error occurs while retrieving the result actions + * @since 1.2.0 + */ + ResultActions answerAsResultActions() throws TestAnswerException; + + /** + * Retrieves the response content as a string from the executed HTTP request. + * + * @return the response content as a string + * @throws TestAnswerException if an error occurs while retrieving the response content + * @since 1.2.0 + */ + String answerAsString() throws TestAnswerException; + + /** + * Retrieves the response content as a byte array from the executed HTTP request. + * + * @return the response content as a byte array + * @throws TestAnswerException if an error occurs while retrieving the response content + * @since 1.2.0 + */ + byte[] answerAsByte() throws TestAnswerException; + + /** + * Retrieves the value of a specified response header from the executed HTTP request. + * + * @param key the name of the response header to retrieve + * @return the value of the response header, or {@code null} if the header is not present + * @throws TestAnswerException if an error occurs while retrieving the header value + * @since 1.2.0 + */ + @Nullable + String answerHeader(String key) throws TestAnswerException; + + /** + * Executes the HTTP request without returning any content. + * + * @throws TestAnswerException if an error occurs during the execution + * @since 1.2.0 + */ + void answerVoid() throws TestAnswerException; +} diff --git a/src/main/java/io/github/co_mmer/aaamockmvc/test/web/answer/TestAnswerImpl.java b/src/main/java/io/github/co_mmer/aaamockmvc/test/web/answer/TestAnswerImpl.java new file mode 100644 index 0000000..821856b --- /dev/null +++ b/src/main/java/io/github/co_mmer/aaamockmvc/test/web/answer/TestAnswerImpl.java @@ -0,0 +1,113 @@ +package io.github.co_mmer.aaamockmvc.test.web.answer; + +import io.github.co_mmer.aaamockmvc.test.web.answer.exception.TestAnswerException; +import io.github.co_mmer.aaamockmvc.test.web.request.context.TestRequestBean; +import io.github.co_mmer.aaamockmvc.test.web.request.context.TestRequestContext; +import java.io.UnsupportedEncodingException; +import org.springframework.lang.NonNull; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; + +/** + * This class represents the implementation of the {@link TestAnswer} interface, providing methods + * for executing HTTP requests and retrieving their responses in a testing context. + * + * @since 1.2.0 + */ +public final class TestAnswerImpl implements TestAnswer { + + private final TestRequestBean bean; + private final MockHttpServletRequestBuilder requestBuilder; + + /** + * Constructs an instance of {@code TestAnswerImpl} with the specified request context and request + * builder. + * + * @param context the {@link TestRequestContext} containing the request context and associated + * beans (must not be {@code null}) + * @param requestBuilder the {@link MockHttpServletRequestBuilder} used to build and perform the + * HTTP request (must not be {@code null}) + * @throws NullPointerException if either {@code context} or {@code requestBuilder} is {@code + * null} + * @since 1.2.0 + */ + public TestAnswerImpl( + @NonNull TestRequestContext context, @NonNull MockHttpServletRequestBuilder requestBuilder) { + + this.bean = context.bean(); + this.requestBuilder = requestBuilder; + } + + /** + * Performs the HTTP request and returns the actions result. + * + * @return the {@link ResultActions} for the performed request + * @throws TestAnswerException if an error occurs while performing the request + * @since 1.2.0 + */ + @Override + public ResultActions answerAsResultActions() throws TestAnswerException { + try { + return this.bean.mvc().perform(this.requestBuilder); + } catch (Exception e) { + throw new TestAnswerException(e); + } + } + + /** + * Retrieves the content of the response as a string. + * + * @return the response content as a string + * @throws TestAnswerException if an error occurs while retrieving the content + * @since 1.2.0 + */ + @Override + public String answerAsString() throws TestAnswerException { + try { + return getResponse().getContentAsString(); + } catch (UnsupportedEncodingException e) { + throw new TestAnswerException(e); + } + } + + private MockHttpServletResponse getResponse() throws TestAnswerException { + return answerAsResultActions().andReturn().getResponse(); + } + + /** + * Retrieves the response as a byte array. + * + * @return the response content as a byte array + * @throws TestAnswerException if an error occurs while retrieving the response + * @since 1.2.0 + */ + @Override + public byte[] answerAsByte() throws TestAnswerException { + return getResponse().getContentAsByteArray(); + } + + /** + * Retrieves the value of a specific header from the response. + * + * @param key the name of the header to retrieve (must not be {@code null}) + * @return the value of the specified header, or {@code null} if not present + * @throws TestAnswerException if an error occurs while retrieving the header + * @since 1.2.0 + */ + @Override + public String answerHeader(String key) throws TestAnswerException { + return getResponse().getHeader(key); + } + + /** + * Performs the HTTP request but does not return the result. + * + * @throws TestAnswerException if an error occurs while performing the request + * @since 1.2.0 + */ + @Override + public void answerVoid() throws TestAnswerException { + answerAsResultActions(); + } +} diff --git a/src/main/java/io/github/co_mmer/aaamockmvc/test/web/answer/exception/TestAnswerException.java b/src/main/java/io/github/co_mmer/aaamockmvc/test/web/answer/exception/TestAnswerException.java new file mode 100644 index 0000000..264d381 --- /dev/null +++ b/src/main/java/io/github/co_mmer/aaamockmvc/test/web/answer/exception/TestAnswerException.java @@ -0,0 +1,24 @@ +package io.github.co_mmer.aaamockmvc.test.web.answer.exception; + +/** + * This class represents an exception that is thrown when an error occurs during the processing of a + * test answer operation. It serves to encapsulate underlying exceptions that may arise from various + * issues in handling HTTP responses or related tasks. + * + * @since 1.2.0 + */ +public final class TestAnswerException extends Exception { + + /** + * Constructs a new {@code TestAnswerException} with the specified cause. + * + *

This constructor allows the wrapping of another throwable, enabling better error handling + * and debugging by preserving the original exception information. + * + * @param cause the underlying cause of the exception (must not be {@code null}) + * @since 1.2.0 + */ + public TestAnswerException(Throwable cause) { + super(cause); + } +} diff --git a/src/main/java/io/github/co_mmer/aaamockmvc/test/web/asserts/content/TestAssertContentImpl.java b/src/main/java/io/github/co_mmer/aaamockmvc/test/web/asserts/content/TestAssertContentImpl.java index 3059da6..4b59e74 100644 --- a/src/main/java/io/github/co_mmer/aaamockmvc/test/web/asserts/content/TestAssertContentImpl.java +++ b/src/main/java/io/github/co_mmer/aaamockmvc/test/web/asserts/content/TestAssertContentImpl.java @@ -1,5 +1,9 @@ package io.github.co_mmer.aaamockmvc.test.web.asserts.content; +import static io.github.co_mmer.aaamockmvc.test.web.mapper.TestGenericMapper.mapTo; +import static io.github.co_mmer.aaamockmvc.test.web.mapper.TestGenericMapper.mapToList; +import static io.github.co_mmer.aaamockmvc.test.web.mapper.TestGenericMapper.mapToMap; +import static io.github.co_mmer.aaamockmvc.test.web.mapper.TestGenericMapper.mapToSet; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; @@ -9,8 +13,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import io.github.co_mmer.aaamockmvc.test.web.asserts.head.TestAssertHead; import io.github.co_mmer.aaamockmvc.test.web.asserts.head.TestAssertHeadImpl; -import io.github.co_mmer.aaamockmvc.test.web.asserts.mapper.TestAssertResultMapper; -import io.github.co_mmer.aaamockmvc.test.web.asserts.mapper.exception.TestAssertResultMapperException; +import io.github.co_mmer.aaamockmvc.test.web.mapper.exception.TestGenericMapperException; import java.util.List; import java.util.Map; import java.util.Set; @@ -197,10 +200,9 @@ public TestAssertContent assertContentEquals( try { var result = this.actions.andReturn(); - var content = - TestAssertResultMapper.mapTo(this.objectMapper, result, expectedClass, deserializers); + var content = mapTo(this.objectMapper, result, expectedClass, deserializers); assertThat(content, is(expectedResponse)); - } catch (TestAssertResultMapperException e) { + } catch (TestGenericMapperException e) { Assertions.fail(e); } return this; @@ -228,10 +230,9 @@ public TestAssertContent assertContentEquals( try { var result = this.actions.andReturn(); - var content = - TestAssertResultMapper.mapToList(this.objectMapper, result, expectedClass, deserializers); + var content = mapToList(this.objectMapper, result, expectedClass, deserializers); assertThat(content, is(expectedResponse)); - } catch (TestAssertResultMapperException e) { + } catch (TestGenericMapperException e) { Assertions.fail(e); } return this; @@ -259,10 +260,9 @@ public TestAssertContent assertContentEquals( try { var result = this.actions.andReturn(); - var content = - TestAssertResultMapper.mapToSet(this.objectMapper, result, expectedClass, deserializers); + var content = mapToSet(this.objectMapper, result, expectedClass, deserializers); assertThat(content, is(expectedResponse)); - } catch (TestAssertResultMapperException e) { + } catch (TestGenericMapperException e) { Assertions.fail(e); } return this; @@ -293,11 +293,9 @@ public TestAssertContent assertContentEquals( try { var result = this.actions.andReturn(); - var content = - TestAssertResultMapper.mapToMap( - this.objectMapper, result, keyClass, valueClass, deserializers); + var content = mapToMap(this.objectMapper, result, keyClass, valueClass, deserializers); assertThat(content, is(expectedResponse)); - } catch (TestAssertResultMapperException e) { + } catch (TestGenericMapperException e) { Assertions.fail(e); } return this; diff --git a/src/main/java/io/github/co_mmer/aaamockmvc/test/web/asserts/mapper/TestAssertResultMapper.java b/src/main/java/io/github/co_mmer/aaamockmvc/test/web/mapper/TestGenericMapper.java similarity index 69% rename from src/main/java/io/github/co_mmer/aaamockmvc/test/web/asserts/mapper/TestAssertResultMapper.java rename to src/main/java/io/github/co_mmer/aaamockmvc/test/web/mapper/TestGenericMapper.java index 4f91d05..82b3b7e 100644 --- a/src/main/java/io/github/co_mmer/aaamockmvc/test/web/asserts/mapper/TestAssertResultMapper.java +++ b/src/main/java/io/github/co_mmer/aaamockmvc/test/web/mapper/TestGenericMapper.java @@ -1,9 +1,11 @@ -package io.github.co_mmer.aaamockmvc.test.web.asserts.mapper; +package io.github.co_mmer.aaamockmvc.test.web.mapper; + +import static io.github.co_mmer.aaamockmvc.test.web.mapper.TestGenericMapperConfigurer.registerDeserializers; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.ObjectMapper; -import io.github.co_mmer.aaamockmvc.test.web.asserts.mapper.exception.TestAssertResultMapperException; +import io.github.co_mmer.aaamockmvc.test.web.mapper.exception.TestGenericMapperException; import java.util.List; import java.util.Map; import java.util.Set; @@ -16,11 +18,11 @@ * Utility class responsible for mapping the content of {@code MvcResult} responses to various * generic types such as objects, lists, sets, or maps. * - *

This class uses the {@code TestAssertObjectMapper} to deserialize the JSON content of HTTP - * responses into the expected data structures, supporting custom deserializers when provided. + *

This class uses the {@code TestGenericMapperConfigurer} to deserialize the JSON content of + * HTTP responses into the expected data structures, supporting custom deserializers when provided. * - *

All methods throw {@code TestAssertResultMapperException} if an error occurs during the - * mapping process, which encapsulates the underlying exception. + *

All methods throw {@code TestGenericMapperException} if an error occurs during the mapping + * process, which encapsulates the underlying exception. * *

This class is marked as {@code final} and cannot be instantiated directly. It uses a private * constructor for utility purposes. @@ -28,13 +30,13 @@ * @since 1.0.0 */ @RequiredArgsConstructor(access = AccessLevel.PRIVATE) -public final class TestAssertResultMapper { +public final class TestGenericMapper { /** * Maps the JSON content of the {@code MvcResult} response to an object of the specified class. * *

If an error occurs, execution is terminated with a call to {@code Assertions.fail}, passing - * the corresponding exception encapsulated in {@code TestAssertResultMapperException}. + * the corresponding exception encapsulated in {@code TestGenericMapperException}. * * @param objectMapper the {@code ObjectMapper} used for mapping (must not be {@code null}) * @param the type of the object to be mapped @@ -44,7 +46,7 @@ public final class TestAssertResultMapper { * @return an object of the specified type * @throws NullPointerException if the {@code result}, {@code objectMapper}, or {@code * expectedClass} is {@code null} - * @throws TestAssertResultMapperException if an error occurs during mapping + * @throws TestGenericMapperException if an error occurs during mapping * @since 1.0.0 */ @SuppressWarnings("unchecked") @@ -53,25 +55,22 @@ public static T mapTo( @NonNull MvcResult result, @NonNull Class expectedClass, JsonDeserializer... deserializers) - throws TestAssertResultMapperException { - - var javaType = - TestAssertObjectMapper.create(objectMapper, expectedClass, deserializers) - .getTypeFactory() - .constructType(expectedClass); + throws TestGenericMapperException { - return mapToGenericType(objectMapper, result, javaType); + var mapper = registerDeserializers(objectMapper, expectedClass, deserializers); + var javaType = mapper.getTypeFactory().constructType(expectedClass); + return mapToGenericType(mapper, result, javaType); } private static T mapToGenericType( ObjectMapper objectMapper, MvcResult result, JavaType javaType) - throws TestAssertResultMapperException { + throws TestGenericMapperException { try { var jsonContent = result.getResponse().getContentAsString(); return objectMapper.readValue(jsonContent, javaType); } catch (Exception e) { - throw new TestAssertResultMapperException(e); + throw new TestGenericMapperException(e); } } @@ -87,7 +86,7 @@ private static T mapToGenericType( * @return a {@code List} of objects of the specified type * @throws NullPointerException if the {@code result}, {@code objectMapper}, or {@code * expectedClass} is {@code null} - * @throws TestAssertResultMapperException if an error occurs during mapping + * @throws TestGenericMapperException if an error occurs during mapping * @since 1.0.0 */ @SuppressWarnings("unchecked") @@ -96,14 +95,11 @@ public static List mapToList( @NonNull MvcResult result, @NonNull Class expectedClass, JsonDeserializer... deserializers) - throws TestAssertResultMapperException { - - var javaType = - TestAssertObjectMapper.create(objectMapper, expectedClass, deserializers) - .getTypeFactory() - .constructParametricType(List.class, expectedClass); + throws TestGenericMapperException { - return mapToGenericType(objectMapper, result, javaType); + var mapper = registerDeserializers(objectMapper, expectedClass, deserializers); + var javaType = mapper.getTypeFactory().constructParametricType(List.class, expectedClass); + return mapToGenericType(mapper, result, javaType); } /** @@ -118,7 +114,7 @@ public static List mapToList( * @return a {@code Set} of objects of the specified type * @throws NullPointerException if the {@code result}, {@code objectMapper}, or {@code * expectedClass} is {@code null} - * @throws TestAssertResultMapperException if an error occurs during mapping + * @throws TestGenericMapperException if an error occurs during mapping * @since 1.0.0 */ @SuppressWarnings("unchecked") @@ -127,14 +123,11 @@ public static Set mapToSet( @NonNull MvcResult result, @NonNull Class expectedClass, JsonDeserializer... deserializers) - throws TestAssertResultMapperException { + throws TestGenericMapperException { - var javaType = - TestAssertObjectMapper.create(objectMapper, expectedClass, deserializers) - .getTypeFactory() - .constructParametricType(Set.class, expectedClass); - - return mapToGenericType(objectMapper, result, javaType); + var mapper = registerDeserializers(objectMapper, expectedClass, deserializers); + var javaType = mapper.getTypeFactory().constructParametricType(Set.class, expectedClass); + return mapToGenericType(mapper, result, javaType); } /** @@ -151,7 +144,7 @@ public static Set mapToSet( * @return a {@code Map} of key-value pairs of the specified types * @throws NullPointerException if the {@code result}, {@code objectMapper}, {@code keyClass}, or * {@code valueClass} is {@code null} - * @throws TestAssertResultMapperException if an error occurs during mapping + * @throws TestGenericMapperException if an error occurs during mapping * @since 1.0.0 */ @SuppressWarnings("unchecked") @@ -161,13 +154,10 @@ public static Map mapToMap( @NonNull Class keyClass, @NonNull Class valueClass, JsonDeserializer... deserializers) - throws TestAssertResultMapperException { - - var javaType = - TestAssertObjectMapper.create(objectMapper, valueClass, deserializers) - .getTypeFactory() - .constructParametricType(Map.class, keyClass, valueClass); + throws TestGenericMapperException { - return mapToGenericType(objectMapper, result, javaType); + var mapper = registerDeserializers(objectMapper, valueClass, deserializers); + var javaType = mapper.getTypeFactory().constructParametricType(Map.class, keyClass, valueClass); + return mapToGenericType(mapper, result, javaType); } } diff --git a/src/main/java/io/github/co_mmer/aaamockmvc/test/web/asserts/mapper/TestAssertObjectMapper.java b/src/main/java/io/github/co_mmer/aaamockmvc/test/web/mapper/TestGenericMapperConfigurer.java similarity index 85% rename from src/main/java/io/github/co_mmer/aaamockmvc/test/web/asserts/mapper/TestAssertObjectMapper.java rename to src/main/java/io/github/co_mmer/aaamockmvc/test/web/mapper/TestGenericMapperConfigurer.java index f854fe7..0a17e18 100644 --- a/src/main/java/io/github/co_mmer/aaamockmvc/test/web/asserts/mapper/TestAssertObjectMapper.java +++ b/src/main/java/io/github/co_mmer/aaamockmvc/test/web/mapper/TestGenericMapperConfigurer.java @@ -1,4 +1,4 @@ -package io.github.co_mmer.aaamockmvc.test.web.asserts.mapper; +package io.github.co_mmer.aaamockmvc.test.web.mapper; import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.ObjectMapper; @@ -9,7 +9,7 @@ import lombok.RequiredArgsConstructor; /** - * Utility class for creating and configuring an {@code ObjectMapper} with custom deserializers. + * Utility class for configuring an {@code ObjectMapper} with custom deserializers. * *

This class provides methods to add specific deserializers to an existing {@code ObjectMapper}, * allowing for the deserialization of complex objects in a controlled manner. @@ -17,10 +17,10 @@ * @since 1.0.0 */ @RequiredArgsConstructor(access = AccessLevel.PRIVATE) -public final class TestAssertObjectMapper { +public final class TestGenericMapperConfigurer { /** - * Creates and configures an {@code ObjectMapper} with optional custom deserializers. + * Registers custom deserializers in the provided {@code ObjectMapper}. * *

If provided, the deserializers are registered for the specified expected class. * @@ -35,7 +35,7 @@ public final class TestAssertObjectMapper { * null} * @since 1.0.0 */ - public static ObjectMapper create( + public static ObjectMapper registerDeserializers( @NonNull ObjectMapper objectMapper, @NonNull Class expectedClass, JsonDeserializer[] deserializers) { diff --git a/src/main/java/io/github/co_mmer/aaamockmvc/test/web/asserts/mapper/exception/TestAssertResultMapperException.java b/src/main/java/io/github/co_mmer/aaamockmvc/test/web/mapper/exception/TestGenericMapperException.java similarity index 72% rename from src/main/java/io/github/co_mmer/aaamockmvc/test/web/asserts/mapper/exception/TestAssertResultMapperException.java rename to src/main/java/io/github/co_mmer/aaamockmvc/test/web/mapper/exception/TestGenericMapperException.java index 0a8a9c4..85f6bd8 100644 --- a/src/main/java/io/github/co_mmer/aaamockmvc/test/web/asserts/mapper/exception/TestAssertResultMapperException.java +++ b/src/main/java/io/github/co_mmer/aaamockmvc/test/web/mapper/exception/TestGenericMapperException.java @@ -1,4 +1,4 @@ -package io.github.co_mmer.aaamockmvc.test.web.asserts.mapper.exception; +package io.github.co_mmer.aaamockmvc.test.web.mapper.exception; /** * Custom exception class for handling errors that occur during the mapping of HTTP response results @@ -10,10 +10,10 @@ * * @since 1.0.0 */ -public final class TestAssertResultMapperException extends Exception { +public final class TestGenericMapperException extends Exception { /** - * Constructs a new {@code TestAssertResultMapperException} with the specified cause. + * Constructs a new {@code TestGenericMapperException} with the specified cause. * *

This constructor is used to wrap an existing {@link Throwable} that caused the mapping * error, preserving the original exception for further analysis. @@ -22,7 +22,7 @@ public final class TestAssertResultMapperException extends Exception { * @throws NullPointerException if the {@code cause} is {@code null} * @since 1.0.0 */ - public TestAssertResultMapperException(Throwable cause) { + public TestGenericMapperException(Throwable cause) { super(cause); } } diff --git a/src/test/java/io/github/co_mmer/aaamockmvc/test/web/act/TestActImplTest.java b/src/test/java/io/github/co_mmer/aaamockmvc/test/web/act/TestActImplTest.java index 6e2f5f1..5a40b53 100644 --- a/src/test/java/io/github/co_mmer/aaamockmvc/test/web/act/TestActImplTest.java +++ b/src/test/java/io/github/co_mmer/aaamockmvc/test/web/act/TestActImplTest.java @@ -1,13 +1,8 @@ package io.github.co_mmer.aaamockmvc.test.web.act; -import static io.github.co_mmer.aaamockmvc.testdata.testutil.TestBody.TEST_BODY_JSON; import static io.github.co_mmer.aaamockmvc.testdata.testutil.TestDataRequestDto.TEST_REQUEST_DTO; -import static io.github.co_mmer.aaamockmvc.testdata.testutil.TestHeader.TEST_HEADER_KEY_1; -import static io.github.co_mmer.aaamockmvc.testdata.testutil.TestHeader.TEST_HEADER_VALUE_1; -import static io.github.co_mmer.aaamockmvc.testdata.testutil.TestValue.TEST_BYTE; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.notNullValue; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; @@ -18,6 +13,7 @@ import io.github.co_mmer.aaamockmvc.test.web.act.exception.TestActException; import io.github.co_mmer.aaamockmvc.test.web.act.strategy.TestRequestBaseStrategy; import io.github.co_mmer.aaamockmvc.test.web.act.strategy.TestRequestStrategyFactory; +import io.github.co_mmer.aaamockmvc.test.web.answer.TestAnswerImpl; import io.github.co_mmer.aaamockmvc.test.web.asserts.TestAssertImpl; import io.github.co_mmer.aaamockmvc.test.web.request.context.TestRequestBean; import io.github.co_mmer.aaamockmvc.test.web.request.context.TestRequestContext; @@ -27,13 +23,11 @@ import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; class TestActImplTest { private MockMvc mvc; private MockMvcSetup setup; - private MockHttpServletRequestBuilder mockRequestBuilder; private TestActImpl testAct; private TestRequestContext testRequestContext; @@ -42,7 +36,7 @@ void setUp() { this.mvc = mock(MockMvc.class); this.setup = new MockMvcSetup(this.mvc); var testRequestConfig = new TestRequestBean(this.mvc, new ObjectMapper()); - this.mockRequestBuilder = mock(MockHttpServletRequestBuilder.class); + this.testRequestContext = new TestRequestContext(TEST_REQUEST_DTO, testRequestConfig); this.testAct = new TestActImpl(this.testRequestContext); } @@ -79,170 +73,42 @@ void WHEN_actPerform_THEN_strategy_apply_is_called() { } @Test - void WHEN_resultActions_THEN_mvc_perform_is_called() throws Exception { - // Arrange - var mockTestRequestStrategy = mock(TestRequestBaseStrategy.class); - var mockTestRequestStrategyFactory = - MockTestRequestStrategyFactory.mockTestRequestStrategyFactory(mockTestRequestStrategy); - - when(mockTestRequestStrategy.apply(TEST_REQUEST_DTO)).thenReturn(this.mockRequestBuilder); - - this.testAct.actPerform(); - - // Act - this.testAct.resultActions(); - - // Assert - verify(this.mvc).perform(this.mockRequestBuilder); - mockTestRequestStrategyFactory.close(); - } - - @Test - void GIVEN_exception_WHEN_resultActions_THEN_throw_TestActException() throws Exception { - // Arrange - when(this.mvc.perform(any())).thenThrow(Exception.class); - - // Act - var exception = assertThrows(Exception.class, () -> this.testAct.resultActions()); - - // Assert - assertThat(exception, is(notNullValue())); - assertThat(exception.getClass(), is(TestActException.class)); - } - - @Test - void WHEN_resultVoid_THEN_mvc_perform_is_called() throws Exception { - // Arrange - var mockTestRequestStrategy = mock(TestRequestBaseStrategy.class); - var mockTestRequestStrategyFactory = - MockTestRequestStrategyFactory.mockTestRequestStrategyFactory(mockTestRequestStrategy); - - when(mockTestRequestStrategy.apply(TEST_REQUEST_DTO)).thenReturn(this.mockRequestBuilder); - this.testAct.actPerform(); - - // Act - this.testAct.resultVoid(); - - // Assert - verify(this.mvc).perform(this.mockRequestBuilder); - mockTestRequestStrategyFactory.close(); - } - - @Test - void GIVEN_exception_WHEN_resultVoid_THEN_throw_TestActException() throws Exception { - // Arrange - when(this.mvc.perform(any())).thenThrow(Exception.class); - - // Act - var exception = assertThrows(Exception.class, () -> this.testAct.resultVoid()); - - // Assert - assertThat(exception, is(notNullValue())); - assertThat(exception.getClass(), is(TestActException.class)); - } - - @Test - void WHEN_resultAsString_THEN_mvc_getContentAsString_is_called() throws Exception { - // Arrange - this.setup.mockGetContentAsString(); - this.testAct = new TestActImpl(this.testRequestContext); - - // Act - this.testAct.resultAsString(); - - // Assert - this.setup.verifyGetContentAsString(); - } - - @Test - void WHEN_resultAsString_THEN_return_expected_value() throws Exception { - // Arrange - this.setup.mockGetContentAsString(); - this.testAct = new TestActImpl(this.testRequestContext); - - // Act - var result = this.testAct.resultAsString(); - - // Assert - assertThat(result, is(TEST_BODY_JSON)); - } - - @Test - void GIVEN_exception_WHEN_resultAsString_THEN_throw_TestActException() throws Exception { - // Arrange - this.setup.mockThrowGetContentAsString(); - this.testAct = new TestActImpl(this.testRequestContext); - - // Act - var exception = assertThrows(Exception.class, () -> this.testAct.resultAsString()); - - // Assert - assertThat(exception, is(notNullValue())); - assertThat(exception.getClass(), is(TestActException.class)); - } - - @Test - void WHEN_resultAsByte_THEN_mvc_getContentAsByteArray_is_called() throws Exception { - // Arrange - this.setup.mockGetContentAsByteArray(); - this.testAct = new TestActImpl(this.testRequestContext); - - // Act - this.testAct.resultAsByte(); - - // Assert - this.setup.verifyGetContentAsByteArray(); - } - - @Test - void WHEN_resultAsByte_THEN_return_expected_value() throws Exception { - // Arrange - this.setup.mockGetContentAsByteArray(); - this.testAct = new TestActImpl(this.testRequestContext); - - // Act - var result = this.testAct.resultAsByte(); - - // Assert - assertThat(result, is(TEST_BYTE)); - } - - @Test - void WHEN_resultHeader_THEN_mvc_getContentAsByteArray_is_called() throws Exception { + void WHEN_asserts_THEN_return_expected_class() throws Exception { // Arrange this.setup.mockGetHeader(); this.testAct = new TestActImpl(this.testRequestContext); // Act - this.testAct.resultHeader(TEST_HEADER_KEY_1); + var asserts = this.testAct.asserts(); // Assert - this.setup.verifyGetHeader(); + assertThat(asserts.getClass(), is(TestAssertImpl.class)); } @Test - void WHEN_resultHeader_THEN_return_expected_value() throws Exception { + void GIVEN_exception_WHEN_asserts_THEN_throw_exception() throws Exception { // Arrange this.setup.mockGetHeader(); + when(this.mvc.perform(any())).thenThrow(Exception.class); this.testAct = new TestActImpl(this.testRequestContext); // Act - var result = this.testAct.resultHeader(TEST_HEADER_KEY_1); + var exception = assertThrows(Exception.class, () -> this.testAct.asserts()); // Assert - assertThat(result, is(TEST_HEADER_VALUE_1)); + assertThat(exception.getClass(), is(TestActException.class)); } @Test - void WHEN_asserts_THEN_return_expected_class() throws Exception { + void WHEN_answer_THEN_return_expected_class() throws Exception { // Arrange this.setup.mockGetHeader(); this.testAct = new TestActImpl(this.testRequestContext); // Act - var asserts = this.testAct.asserts(); + var answer = this.testAct.answer(); // Assert - assertThat(asserts.getClass(), is(TestAssertImpl.class)); + assertThat(answer.getClass(), is(TestAnswerImpl.class)); } } diff --git a/src/test/java/io/github/co_mmer/aaamockmvc/test/web/answer/TestAnswerTest.java b/src/test/java/io/github/co_mmer/aaamockmvc/test/web/answer/TestAnswerTest.java new file mode 100644 index 0000000..a5e563a --- /dev/null +++ b/src/test/java/io/github/co_mmer/aaamockmvc/test/web/answer/TestAnswerTest.java @@ -0,0 +1,181 @@ +package io.github.co_mmer.aaamockmvc.test.web.answer; + +import static io.github.co_mmer.aaamockmvc.testdata.testutil.TestBody.TEST_BODY_JSON; +import static io.github.co_mmer.aaamockmvc.testdata.testutil.TestDataRequestDto.TEST_REQUEST_DTO; +import static io.github.co_mmer.aaamockmvc.testdata.testutil.TestHeader.TEST_HEADER_KEY_1; +import static io.github.co_mmer.aaamockmvc.testdata.testutil.TestHeader.TEST_HEADER_VALUE_1; +import static io.github.co_mmer.aaamockmvc.testdata.testutil.TestValue.TEST_BYTE; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.co_mmer.aaamockmvc.test.web.act.strategy.TestRequestBaseStrategy; +import io.github.co_mmer.aaamockmvc.test.web.answer.exception.TestAnswerException; +import io.github.co_mmer.aaamockmvc.test.web.request.context.TestRequestBean; +import io.github.co_mmer.aaamockmvc.test.web.request.context.TestRequestContext; +import io.github.co_mmer.aaamockmvc.testdata.MockTestRequestStrategyFactory; +import io.github.co_mmer.aaamockmvc.testdata.testsetup.MockMvcSetup; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; + +class TestAnswerTest { + + private MockMvc mvc; + private MockMvcSetup setup; + private TestAnswer testAnswer; + private MockHttpServletRequestBuilder mockRequestBuilder; + private TestRequestContext testRequestContext; + + @BeforeEach + void setUp() { + this.mvc = mock(MockMvc.class); + this.setup = new MockMvcSetup(this.mvc); + var testRequestConfig = new TestRequestBean(this.mvc, new ObjectMapper()); + this.mockRequestBuilder = mock(MockHttpServletRequestBuilder.class); + + this.testRequestContext = new TestRequestContext(TEST_REQUEST_DTO, testRequestConfig); + this.testAnswer = new TestAnswerImpl(this.testRequestContext, this.mockRequestBuilder); + } + + @Test + void GIVEN_exception_WHEN_answerAsResultActions_THEN_throw_exception() throws Exception { + // Arrange + when(this.mvc.perform(any())).thenThrow(Exception.class); + + // Act + var exception = assertThrows(Exception.class, () -> this.testAnswer.answerAsResultActions()); + + // Assert + assertThat(exception, is(notNullValue())); + assertThat(exception.getClass(), is(TestAnswerException.class)); + } + + @Test + void WHEN_answerVoid_THEN_mvc_perform_is_called() throws Exception { + // Arrange + var mockTestRequestStrategy = mock(TestRequestBaseStrategy.class); + var mockTestRequestStrategyFactory = + MockTestRequestStrategyFactory.mockTestRequestStrategyFactory(mockTestRequestStrategy); + + when(mockTestRequestStrategy.apply(TEST_REQUEST_DTO)).thenReturn(this.mockRequestBuilder); + + // Act + this.testAnswer.answerVoid(); + + // Assert + verify(this.mvc).perform(this.mockRequestBuilder); + mockTestRequestStrategyFactory.close(); + } + + @Test + void GIVEN_exception_WHEN_answerVoid_THEN_throw_TestResultException() throws Exception { + // Arrange + when(this.mvc.perform(any())).thenThrow(Exception.class); + + // Act + var exception = assertThrows(Exception.class, () -> this.testAnswer.answerVoid()); + + // Assert + assertThat(exception.getClass(), is(TestAnswerException.class)); + } + + @Test + void WHEN_answerAsString_THEN_mvc_getContentAsString_is_called() throws Exception { + // Arrange + this.setup.mockGetContentAsString(); + this.testAnswer = new TestAnswerImpl(this.testRequestContext, this.mockRequestBuilder); + + // Act + this.testAnswer.answerAsString(); + + // Assert + this.setup.verifyGetContentAsString(); + } + + @Test + void WHEN_answerAsString_THEN_return_expected_value() throws Exception { + // Arrange + this.setup.mockGetContentAsString(); + this.testAnswer = new TestAnswerImpl(this.testRequestContext, this.mockRequestBuilder); + + // Act + var result = this.testAnswer.answerAsString(); + + // Assert + assertThat(result, is(TEST_BODY_JSON)); + } + + @Test + void GIVEN_exception_WHEN_answerAsString_THEN_throw_exception() throws Exception { + // Arrange + this.setup.mockThrowGetContentAsString(); + this.testAnswer = new TestAnswerImpl(this.testRequestContext, this.mockRequestBuilder); + + // Act + var exception = assertThrows(Exception.class, () -> this.testAnswer.answerAsString()); + + // Assert + assertThat(exception, is(notNullValue())); + assertThat(exception.getClass(), is(TestAnswerException.class)); + } + + @Test + void WHEN_answerAsByte_THEN_mvc_getContentAsByteArray_is_called() throws Exception { + // Arrange + this.setup.mockGetContentAsByteArray(); + this.testAnswer = new TestAnswerImpl(this.testRequestContext, this.mockRequestBuilder); + + // Act + this.testAnswer.answerAsByte(); + + // Assert + this.setup.verifyGetContentAsByteArray(); + } + + @Test + void WHEN_answerAsByte_THEN_return_expected_value() throws Exception { + // Arrange + this.setup.mockGetContentAsByteArray(); + this.testAnswer = new TestAnswerImpl(this.testRequestContext, this.mockRequestBuilder); + + // Act + var result = this.testAnswer.answerAsByte(); + + // Assert + assertThat(result, is(TEST_BYTE)); + } + + @Test + void WHEN_answerHeader_THEN_mvc_getContentAsByteArray_is_called() throws Exception { + // Arrange + this.setup.mockGetHeader(); + this.testAnswer = new TestAnswerImpl(this.testRequestContext, this.mockRequestBuilder); + + // Act + this.testAnswer.answerHeader(TEST_HEADER_KEY_1); + + // Assert + this.setup.verifyGetHeader(); + } + + @Test + void WHEN_answerHeader_THEN_return_expected_value() throws Exception { + // Arrange + this.setup.mockGetHeader(); + this.testAnswer = new TestAnswerImpl(this.testRequestContext, this.mockRequestBuilder); + + // Act + var result = this.testAnswer.answerHeader(TEST_HEADER_KEY_1); + + // Assert + assertThat(result, is(TEST_HEADER_VALUE_1)); + } +} diff --git a/src/test/java/io/github/co_mmer/aaamockmvc/test/web/asserts/content/TestAssertContentImplTest.java b/src/test/java/io/github/co_mmer/aaamockmvc/test/web/asserts/content/TestAssertContentImplTest.java index e73c4bf..303de87 100644 --- a/src/test/java/io/github/co_mmer/aaamockmvc/test/web/asserts/content/TestAssertContentImplTest.java +++ b/src/test/java/io/github/co_mmer/aaamockmvc/test/web/asserts/content/TestAssertContentImplTest.java @@ -27,8 +27,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import io.github.co_mmer.aaamockmvc.test.web.asserts.head.TestAssertHeadImpl; -import io.github.co_mmer.aaamockmvc.test.web.asserts.mapper.TestAssertResultMapper; -import io.github.co_mmer.aaamockmvc.test.web.asserts.mapper.exception.TestAssertResultMapperException; +import io.github.co_mmer.aaamockmvc.test.web.mapper.TestGenericMapper; +import io.github.co_mmer.aaamockmvc.test.web.mapper.exception.TestGenericMapperException; import io.github.co_mmer.aaamockmvc.testdata.testutil.TestObjectDto; import org.apache.logging.log4j.util.Strings; import org.junit.jupiter.api.BeforeEach; @@ -263,17 +263,17 @@ void GIVEN_unexpected_object_WHEN_assertContentEquals_THEN_assert_is_false() thr @SuppressWarnings("unchecked") void GIVEN_exception_object_WHEN_assertContentEquals_THEN_assert_is_false() { // Arrange - var mockTestAssertResultMapper = mockStatic(TestAssertResultMapper.class); - mockTestAssertResultMapper - .when(() -> TestAssertResultMapper.mapTo(any(), any(), any())) - .thenThrow(new TestAssertResultMapperException(new Throwable("error"))); + var mockTestGenericMapper = mockStatic(TestGenericMapper.class); + mockTestGenericMapper + .when(() -> TestGenericMapper.mapTo(any(), any(), any())) + .thenThrow(new TestGenericMapperException(new Throwable("error"))); // Act & Assert assertThrows( AssertionFailedError.class, () -> this.testAssert.assertContentEquals(TestObjectDto.class, TEST_OBJECT_1_DTO)); - mockTestAssertResultMapper.close(); + mockTestGenericMapper.close(); } @Test @@ -302,17 +302,17 @@ void GIVEN_unexpected_list_WHEN_assertContentEquals_THEN_assert_is_false() throw @SuppressWarnings("unchecked") void GIVEN_exception_list_WHEN_assertContentEquals_THEN_assert_is_false() { // Arrange - var mockTestAssertResultMapper = mockStatic(TestAssertResultMapper.class); - mockTestAssertResultMapper - .when(() -> TestAssertResultMapper.mapToList(any(), any(), any())) - .thenThrow(new TestAssertResultMapperException(new Throwable("error"))); + var mockTestGenericMapper = mockStatic(TestGenericMapper.class); + mockTestGenericMapper + .when(() -> TestGenericMapper.mapToList(any(), any(), any())) + .thenThrow(new TestGenericMapperException(new Throwable("error"))); // Act & Assert assertThrows( AssertionFailedError.class, () -> this.testAssert.assertContentEquals(TestObjectDto.class, TEST_OBJECTS_1_DTO)); - mockTestAssertResultMapper.close(); + mockTestGenericMapper.close(); } @Test @@ -341,17 +341,17 @@ void GIVEN_unexpected_set_WHEN_assertContentEquals_THEN_assert_is_false() throws @SuppressWarnings("unchecked") void GIVEN_exception_set_WHEN_assertContentEquals_THEN_assert_is_false() { // Arrange - var mockTestAssertResultMapper = mockStatic(TestAssertResultMapper.class); - mockTestAssertResultMapper - .when(() -> TestAssertResultMapper.mapToSet(any(), any(), any())) - .thenThrow(new TestAssertResultMapperException(new Throwable("error"))); + var mockTestGenericMapper = mockStatic(TestGenericMapper.class); + mockTestGenericMapper + .when(() -> TestGenericMapper.mapToSet(any(), any(), any())) + .thenThrow(new TestGenericMapperException(new Throwable("error"))); // Act & Assert assertThrows( AssertionFailedError.class, () -> this.testAssert.assertContentEquals(TestObjectDto.class, TEST_OBJECTS_SET_1_DTO)); - mockTestAssertResultMapper.close(); + mockTestGenericMapper.close(); } @Test @@ -382,10 +382,10 @@ void GIVEN_unexpected_map_WHEN_assertContentEquals_THEN_assert_is_false() throws @SuppressWarnings("unchecked") void GIVEN_exception_map_WHEN_assertContentEquals_THEN_assert_is_false() { // Arrange - var mockTestAssertResultMapper = mockStatic(TestAssertResultMapper.class); - mockTestAssertResultMapper - .when(() -> TestAssertResultMapper.mapToMap(any(), any(), any(), any())) - .thenThrow(new TestAssertResultMapperException(new Throwable("error"))); + var mockTestGenericMapper = mockStatic(TestGenericMapper.class); + mockTestGenericMapper + .when(() -> TestGenericMapper.mapToMap(any(), any(), any(), any())) + .thenThrow(new TestGenericMapperException(new Throwable("error"))); // Act & Assert assertThrows( @@ -394,7 +394,7 @@ void GIVEN_exception_map_WHEN_assertContentEquals_THEN_assert_is_false() { this.testAssert.assertContentEquals( Boolean.class, TestObjectDto.class, TEST_OBJECTS_MAP_1_DTO)); - mockTestAssertResultMapper.close(); + mockTestGenericMapper.close(); } @Test diff --git a/src/test/java/io/github/co_mmer/aaamockmvc/test/web/asserts/mapper/TestAssertObjectMapperTest.java b/src/test/java/io/github/co_mmer/aaamockmvc/test/web/asserts/mapper/TestAssertObjectMapperTest.java deleted file mode 100644 index 451e7e1..0000000 --- a/src/test/java/io/github/co_mmer/aaamockmvc/test/web/asserts/mapper/TestAssertObjectMapperTest.java +++ /dev/null @@ -1,55 +0,0 @@ -package io.github.co_mmer.aaamockmvc.test.web.asserts.mapper; - -import static io.github.co_mmer.aaamockmvc.testdata.testutil.TestObject.TEST_OBJECT_1_JSON; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; - -import com.fasterxml.jackson.databind.JsonDeserializer; -import com.fasterxml.jackson.databind.ObjectMapper; -import io.github.co_mmer.aaamockmvc.testdata.testutil.TestObjectDeserializer; -import io.github.co_mmer.aaamockmvc.testdata.testutil.TestObjectDto; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -class TestAssertObjectMapperTest { - - private ObjectMapper objectMapper; - - @BeforeEach - void setUp() { - this.objectMapper = new ObjectMapper(); - } - - @Test - void GIVEN_JsonDeserializer_null_WHEN_mapTo_THEN_return_getRegisteredModuleIds_size_0() { - // Act - var updatedObjectMapper = TestAssertObjectMapper.create(this.objectMapper, String.class, null); - - // Assert - assertThat(updatedObjectMapper.getRegisteredModuleIds().size(), is(0)); - } - - @Test - void GIVEN_JsonDeserialize_empty_WHEN_mapTo_THEN_return_getRegisteredModuleIds_size_0() { - // Act - var updatedObjectMapper = - TestAssertObjectMapper.create(this.objectMapper, String.class, new JsonDeserializer[0]); - - // Assert - assertThat(updatedObjectMapper.getRegisteredModuleIds().size(), is(0)); - } - - @Test - void GIVEN_JsonDeserialize_WHEN_mapTo_THEN_return_deserialize() throws Exception { - // Arrange - JsonDeserializer[] deserializers = {new TestObjectDeserializer()}; - - // Act - var updatedObjectMapper = - TestAssertObjectMapper.create(this.objectMapper, TestObjectDto.class, deserializers); - - // Assert - var result = updatedObjectMapper.readValue(TEST_OBJECT_1_JSON, TestObjectDto.class); - assertThat(result.name(), is("deserialize")); - } -} diff --git a/src/test/java/io/github/co_mmer/aaamockmvc/test/web/mapper/TestGenericMapperConfigurerTest.java b/src/test/java/io/github/co_mmer/aaamockmvc/test/web/mapper/TestGenericMapperConfigurerTest.java new file mode 100644 index 0000000..afcdc5a --- /dev/null +++ b/src/test/java/io/github/co_mmer/aaamockmvc/test/web/mapper/TestGenericMapperConfigurerTest.java @@ -0,0 +1,57 @@ +package io.github.co_mmer.aaamockmvc.test.web.mapper; + +import static io.github.co_mmer.aaamockmvc.test.web.mapper.TestGenericMapperConfigurer.registerDeserializers; +import static io.github.co_mmer.aaamockmvc.testdata.testutil.TestObject.TEST_OBJECT_1_JSON; +import static io.github.co_mmer.aaamockmvc.testdata.testutil.TestValue.TEST_DESERIALIZE; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.co_mmer.aaamockmvc.testdata.testutil.TestObjectDeserializer; +import io.github.co_mmer.aaamockmvc.testdata.testutil.TestObjectDto; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class TestGenericMapperConfigurerTest { + + private ObjectMapper objectMapper; + + @BeforeEach + void setUp() { + this.objectMapper = new ObjectMapper(); + } + + @Test + void + GIVEN_JsonDeserializer_null_WHEN_registerDeserializers_THEN_return_getRegisteredModuleIds_size_0() { + // Act + var mapper = registerDeserializers(this.objectMapper, String.class, null); + + // Assert + assertThat(mapper.getRegisteredModuleIds().size(), is(0)); + } + + @Test + void + GIVEN_JsonDeserialize_empty_WHEN_registerDeserializers_THEN_return_getRegisteredModuleIds_size_0() { + // Act + var mapper = registerDeserializers(this.objectMapper, String.class, new JsonDeserializer[0]); + + // Assert + assertThat(mapper.getRegisteredModuleIds().size(), is(0)); + } + + @Test + void GIVEN_JsonDeserialize_WHEN_registerDeserializers_THEN_return_deserialize() throws Exception { + // Arrange + JsonDeserializer[] deserializers = {new TestObjectDeserializer()}; + + // Act + var mapper = registerDeserializers(this.objectMapper, TestObjectDto.class, deserializers); + + // Assert + var result = mapper.readValue(TEST_OBJECT_1_JSON, TestObjectDto.class); + assertThat(result.name(), is(TEST_DESERIALIZE)); + } +} diff --git a/src/test/java/io/github/co_mmer/aaamockmvc/test/web/asserts/mapper/TestAssertResultMapperTest.java b/src/test/java/io/github/co_mmer/aaamockmvc/test/web/mapper/TestGenericMapperTest.java similarity index 65% rename from src/test/java/io/github/co_mmer/aaamockmvc/test/web/asserts/mapper/TestAssertResultMapperTest.java rename to src/test/java/io/github/co_mmer/aaamockmvc/test/web/mapper/TestGenericMapperTest.java index 6d0a71a..7492d0b 100644 --- a/src/test/java/io/github/co_mmer/aaamockmvc/test/web/asserts/mapper/TestAssertResultMapperTest.java +++ b/src/test/java/io/github/co_mmer/aaamockmvc/test/web/mapper/TestGenericMapperTest.java @@ -1,9 +1,9 @@ -package io.github.co_mmer.aaamockmvc.test.web.asserts.mapper; +package io.github.co_mmer.aaamockmvc.test.web.mapper; -import static io.github.co_mmer.aaamockmvc.test.web.asserts.mapper.TestAssertResultMapper.mapTo; -import static io.github.co_mmer.aaamockmvc.test.web.asserts.mapper.TestAssertResultMapper.mapToList; -import static io.github.co_mmer.aaamockmvc.test.web.asserts.mapper.TestAssertResultMapper.mapToMap; -import static io.github.co_mmer.aaamockmvc.test.web.asserts.mapper.TestAssertResultMapper.mapToSet; +import static io.github.co_mmer.aaamockmvc.test.web.mapper.TestGenericMapper.mapTo; +import static io.github.co_mmer.aaamockmvc.test.web.mapper.TestGenericMapper.mapToList; +import static io.github.co_mmer.aaamockmvc.test.web.mapper.TestGenericMapper.mapToMap; +import static io.github.co_mmer.aaamockmvc.test.web.mapper.TestGenericMapper.mapToSet; import static io.github.co_mmer.aaamockmvc.testdata.testutil.TestObject.TEST_OBJECTS_1_DTO; import static io.github.co_mmer.aaamockmvc.testdata.testutil.TestObject.TEST_OBJECTS_LIST_1_JSON; import static io.github.co_mmer.aaamockmvc.testdata.testutil.TestObject.TEST_OBJECTS_MAP_1_DTO; @@ -12,21 +12,26 @@ import static io.github.co_mmer.aaamockmvc.testdata.testutil.TestObject.TEST_OBJECTS_SET_1_JSON; import static io.github.co_mmer.aaamockmvc.testdata.testutil.TestObject.TEST_OBJECT_1_DTO; import static io.github.co_mmer.aaamockmvc.testdata.testutil.TestObject.TEST_OBJECT_1_JSON; +import static io.github.co_mmer.aaamockmvc.testdata.testutil.TestValue.TEST_DESERIALIZE; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.ObjectMapper; -import io.github.co_mmer.aaamockmvc.test.web.asserts.mapper.exception.TestAssertResultMapperException; +import io.github.co_mmer.aaamockmvc.test.web.mapper.exception.TestGenericMapperException; +import io.github.co_mmer.aaamockmvc.testdata.testutil.TestObjectDeserializer; import io.github.co_mmer.aaamockmvc.testdata.testutil.TestObjectDto; +import java.util.List; +import java.util.Map; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.web.servlet.MvcResult; -class TestAssertResultMapperTest { +class TestGenericMapperTest { private MvcResult mockMvcResult; private MockHttpServletResponse mockHttpServletResponse; @@ -53,6 +58,21 @@ void GIVEN_expected_class_WHEN_mapTo_THEN_return_expect_object() throws Exceptio assertThat(result, is(TEST_OBJECT_1_DTO)); } + @Test + @SuppressWarnings("unchecked") + void GIVEN_deserializers_WHEN_mapTo_THEN_return_deserialize_value() throws Exception { + // Arrange + var deserializer = new TestObjectDeserializer(); + var deserializers = new JsonDeserializer[] {deserializer}; + when(this.mockHttpServletResponse.getContentAsString()).thenReturn(TEST_OBJECT_1_JSON); + + // Act + var result = mapTo(this.objectMapper, this.mockMvcResult, TestObjectDto.class, deserializers); + + // Assert + assertThat(result.name(), is(TEST_DESERIALIZE)); + } + @Test @SuppressWarnings("unchecked") void GIVEN_unexpected_expectedClass_WHEN_mapTo_THEN_throw_exception() throws Exception { @@ -61,7 +81,7 @@ void GIVEN_unexpected_expectedClass_WHEN_mapTo_THEN_throw_exception() throws Exc // Assert assertThrows( - TestAssertResultMapperException.class, + TestGenericMapperException.class, // Act () -> mapTo(this.objectMapper, this.mockMvcResult, String.class)); @@ -80,6 +100,23 @@ void GIVEN_expected_list_WHEN_mapToList_THEN_return_expect_object() throws Excep assertThat(result, is(TEST_OBJECTS_1_DTO)); } + @Test + @SuppressWarnings("unchecked") + void GIVEN_deserializers_WHEN_mapToList_THEN_return_deserialize_values() throws Exception { + // Arrange + var deserializer = new TestObjectDeserializer(); + var deserializers = new JsonDeserializer[] {deserializer}; + when(this.mockHttpServletResponse.getContentAsString()).thenReturn(TEST_OBJECTS_LIST_1_JSON); + + // Act + List result = + mapToList(this.objectMapper, this.mockMvcResult, TestObjectDto.class, deserializers); + + // Assert + assertThat(result.get(0).name(), is(TEST_DESERIALIZE)); + assertThat(result.get(1).name(), is(TEST_DESERIALIZE)); + } + @Test @SuppressWarnings("unchecked") void GIVEN_unexpected_expectedClass_WHEN_mapToList_THEN_throw_exception() throws Exception { @@ -88,7 +125,7 @@ void GIVEN_unexpected_expectedClass_WHEN_mapToList_THEN_throw_exception() throws // Assert assertThrows( - TestAssertResultMapperException.class, + TestGenericMapperException.class, // Act () -> mapToList(this.objectMapper, this.mockMvcResult, String.class)); @@ -115,7 +152,7 @@ void GIVEN_unexpected_expectedClass_WHEN_mapToSet_THEN_throw_exception() throws // Assert assertThrows( - TestAssertResultMapperException.class, + TestGenericMapperException.class, // Act () -> mapToSet(this.objectMapper, this.mockMvcResult, String.class)); @@ -135,6 +172,28 @@ void GIVEN_expected_map_WHEN_mapToSet_THEN_return_expect_object() throws Excepti assertThat(result, is(TEST_OBJECTS_MAP_1_DTO)); } + @Test + @SuppressWarnings("unchecked") + void GIVEN_deserializers_WHEN_mapToSet_THEN_return_deserialize_values() throws Exception { + // Arrange + var deserializer = new TestObjectDeserializer(); + var deserializers = new JsonDeserializer[] {deserializer}; + when(this.mockHttpServletResponse.getContentAsString()).thenReturn(TEST_OBJECTS_MAP_1_JSON); + + // Act + Map result = + mapToMap( + this.objectMapper, + this.mockMvcResult, + Boolean.class, + TestObjectDto.class, + deserializers); + + // Assert + assertThat(result.get(Boolean.TRUE).name(), is(TEST_DESERIALIZE)); + assertThat(result.get(Boolean.FALSE).name(), is(TEST_DESERIALIZE)); + } + @Test @SuppressWarnings("unchecked") void GIVEN_unexpected_keyClass_WHEN_mapToMap_THEN_throw_exception() throws Exception { @@ -143,7 +202,7 @@ void GIVEN_unexpected_keyClass_WHEN_mapToMap_THEN_throw_exception() throws Excep // Assert assertThrows( - TestAssertResultMapperException.class, + TestGenericMapperException.class, // Act () -> mapToMap(this.objectMapper, this.mockMvcResult, String.class, TestObjectDto.class)); @@ -157,7 +216,7 @@ void GIVEN_unexpected_valueClass_WHEN_mapToMap_THEN_throw_exception() throws Exc // Assert assertThrows( - TestAssertResultMapperException.class, + TestGenericMapperException.class, // Act () -> mapToMap(this.objectMapper, this.mockMvcResult, Boolean.class, String.class)); @@ -171,7 +230,7 @@ void GIVEN_unexpected_keyClass_valueClass_WHEN_mapToMap_THEN_throw_exception() t // Assert assertThrows( - TestAssertResultMapperException.class, + TestGenericMapperException.class, // Act () -> mapToMap(this.objectMapper, this.mockMvcResult, String.class, String.class)); diff --git a/src/test/java/io/github/co_mmer/aaamockmvc/testdata/testutil/TestObjectDeserializer.java b/src/test/java/io/github/co_mmer/aaamockmvc/testdata/testutil/TestObjectDeserializer.java index f15550b..f9fae52 100644 --- a/src/test/java/io/github/co_mmer/aaamockmvc/testdata/testutil/TestObjectDeserializer.java +++ b/src/test/java/io/github/co_mmer/aaamockmvc/testdata/testutil/TestObjectDeserializer.java @@ -1,5 +1,7 @@ package io.github.co_mmer.aaamockmvc.testdata.testutil; +import static io.github.co_mmer.aaamockmvc.testdata.testutil.TestValue.TEST_DESERIALIZE; + import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; @@ -13,6 +15,6 @@ public TestObjectDto deserialize(JsonParser p, DeserializationContext context) var node = p.getCodec().readTree(p); var id = node.get("id").toString(); - return new TestObjectDto(Integer.parseInt(id), "deserialize"); + return new TestObjectDto(Integer.parseInt(id), TEST_DESERIALIZE); } } diff --git a/src/test/java/io/github/co_mmer/aaamockmvc/testdata/testutil/TestValue.java b/src/test/java/io/github/co_mmer/aaamockmvc/testdata/testutil/TestValue.java index 6088855..15e152a 100644 --- a/src/test/java/io/github/co_mmer/aaamockmvc/testdata/testutil/TestValue.java +++ b/src/test/java/io/github/co_mmer/aaamockmvc/testdata/testutil/TestValue.java @@ -75,6 +75,8 @@ public final class TestValue { public static final String TEST_HEAD_VALUE_1 = "headValue1"; public static final String TEST_HEAD_VALUE_2 = "headValue2"; + public static final String TEST_DESERIALIZE = "deserialize"; + private static URI createUri() { try { return new URI(TEST_PATH);