View Javadoc
1   /*
2    * Copyright 2019 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.bremersee.spring.test.api.comparator;
18  
19  import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
20  import static org.bremersee.spring.test.api.comparator.RestApiComparatorAssertionType.ANNOTATION_MUST_NOT_BE_NULL;
21  import static org.bremersee.spring.test.api.comparator.RestApiComparatorAssertionType.METHOD_MUST_NOT_BE_NULL;
22  import static org.bremersee.spring.test.api.comparator.RestApiComparatorAssertionType.SAME_ANNOTATION_ATTRIBUTE_VALUE;
23  import static org.bremersee.spring.test.api.comparator.RestApiComparatorAssertionType.SAME_ANNOTATION_SIZE;
24  import static org.bremersee.spring.test.api.comparator.RestApiComparatorAssertionType.SAME_METHOD_SIZE;
25  import static org.bremersee.spring.test.api.comparator.RestApiComparator.assertSameApi;
26  import static org.bremersee.spring.test.api.comparator.RestApiComparatorExclusion.exclusionBuilder;
27  import static org.bremersee.spring.test.api.comparator.RestApiComparatorPath.PathType.ANNOTATION;
28  import static org.bremersee.spring.test.api.comparator.RestApiComparatorPath.PathType.ATTRIBUTE;
29  import static org.bremersee.spring.test.api.comparator.RestApiComparatorPath.PathType.CLASS;
30  import static org.bremersee.spring.test.api.comparator.RestApiComparatorPath.PathType.METHOD;
31  import static org.bremersee.spring.test.api.comparator.RestApiComparatorPath.pathBuilder;
32  
33  import org.assertj.core.api.SoftAssertions;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * The rest api tester test.
38   *
39   * @author Christian Bremer
40   */
41  class RestApiTesterTest {
42  
43    /**
44     * Compare good apis.
45     */
46    @Test
47    void compareGoodApis() {
48      assertSameApi(GoodRestApiOne.class, GoodRestApiTwo.class);
49    }
50  
51    /**
52     * Compare bad apis and expect wrong class annotations.
53     */
54    @Test
55    void compareBadApisAndExpectWrongClassAnnotations() {
56      assertThatExceptionOfType(AssertionError.class)
57          .isThrownBy(() -> assertSameApi(BadApis.One.class, BadApis.Two.class));
58    }
59  
60    /**
61     * Compare bad apis and expect wrong size of methods.
62     */
63    @Test
64    void compareBadApisAndExpectWrongSizeOfMethods() {
65      assertThatExceptionOfType(AssertionError.class)
66          .isThrownBy(() -> assertSameApi(BadApis.Three.class, BadApis.Four.class));
67    }
68  
69    /**
70     * Compare bad apis and expect wrong methods.
71     */
72    @Test
73    void compareBadApisAndExpectWrongMethods() {
74      assertThatExceptionOfType(AssertionError.class)
75          .isThrownBy(() -> assertSameApi(BadApis.Five.class, BadApis.Six.class));
76    }
77  
78    /**
79     * Compare bad apis and expect wrong method parameters.
80     */
81    @Test
82    void compareBadApisAndExpectWrongMethodParameters() {
83      assertThatExceptionOfType(AssertionError.class)
84          .isThrownBy(() -> assertSameApi(
85              new SoftAssertions(),
86              true,
87              BadApis.Seven.class,
88              BadApis.Eight.class));
89    }
90  
91    /**
92     * Compare bad apis but exclude exclude different values.
93     */
94    @Test
95    void compareBadApisButExcludeDifferentValues() {
96      SoftAssertions softAssertions = new SoftAssertions();
97      assertSameApi(
98          softAssertions,
99          false,
100         BadApis.Three.class,
101         BadApis.Four.class,
102         exclusionBuilder()
103             .path(pathBuilder()
104                 .add(CLASS, "Four")
105                 .build())
106             .type(SAME_METHOD_SIZE)
107             .build(),
108         exclusionBuilder()
109             .path(pathBuilder()
110                 .add(CLASS, "Four")
111                 .add(METHOD, "getExampleModels")
112                 .build())
113             .type(METHOD_MUST_NOT_BE_NULL)
114             .build(),
115         exclusionBuilder()
116             .path(pathBuilder()
117                 .add(CLASS, "Four")
118                 .add(METHOD, "updateExampleModel")
119                 .build())
120             .type(METHOD_MUST_NOT_BE_NULL)
121             .build(),
122         exclusionBuilder()
123             .path(pathBuilder()
124                 .add(CLASS, "Four")
125                 .add(METHOD, "addExampleModel")
126                 .build())
127             .type(SAME_ANNOTATION_SIZE)
128             .build(),
129         exclusionBuilder()
130             .path(pathBuilder()
131                 .add(CLASS, "Four")
132                 .add(METHOD, "addExampleModel")
133                 .add(ANNOTATION, "PostMapping")
134                 .build())
135             .type(ANNOTATION_MUST_NOT_BE_NULL)
136             .build(),
137         exclusionBuilder()
138             .path(pathBuilder()
139                 .add(CLASS, "Four")
140                 .add(METHOD, "getExampleModel")
141                 .add(ANNOTATION, "ApiResponses")
142                 .add(ATTRIBUTE, "value")
143                 .add(ANNOTATION, "ApiResponse")
144                 .add(ATTRIBUTE, "content")
145                 .add(ANNOTATION, "Content")
146                 .add(ATTRIBUTE, "schema")
147                 .add(ANNOTATION, "Schema")
148                 .add(ATTRIBUTE, "implementation")
149                 .build())
150             .type(SAME_ANNOTATION_ATTRIBUTE_VALUE)
151             .build()
152     );
153     softAssertions.assertAll();
154   }
155 }