View Javadoc
1   /*
2    * Copyright 2024 Adrian Herscu
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 dev.aherscu.qa.testing.example.scenarios.tutorial6;
18  
19  import static dev.aherscu.qa.jgiven.commons.utils.AbstractCsvDataProvider.*;
20  import static dev.aherscu.qa.testing.utils.StreamMatchersExtensions.*;
21  import static jakarta.ws.rs.core.Response.Status.Family.*;
22  import static org.hamcrest.Matchers.*;
23  import static org.hamcrest.Matchers.equalTo;
24  
25  import org.testng.annotations.*;
26  
27  import dev.aherscu.qa.jgiven.commons.model.*;
28  import dev.aherscu.qa.jgiven.commons.tags.*;
29  import dev.aherscu.qa.jgiven.commons.utils.*;
30  import dev.aherscu.qa.jgiven.rest.model.*;
31  import dev.aherscu.qa.jgiven.rest.tags.*;
32  import dev.aherscu.qa.testing.example.*;
33  import dev.aherscu.qa.testing.example.model.tutorial.*;
34  import dev.aherscu.qa.testing.example.steps.tutorial.*;
35  import dev.aherscu.qa.testing.utils.rest.*;
36  import jakarta.ws.rs.client.*;
37  
38  @RestTest
39  public class SwaggerPetstore extends
40      ConfigurableScenarioTest<TestConfiguration, RestScenarioType, SwaggerPetstoreFixtures<?>, SwaggerPetstoreActions<?>, SwaggerPetstoreVerifications<?>> {
41  
42      public static final class CredentialsCsvDataProvider
43          extends AbstractCsvDataProvider {
44          @Override
45          protected Class<?> type() {
46              return Credentials.class;
47          }
48      }
49  
50      protected Client client;
51  
52      protected SwaggerPetstore() {
53          super(TestConfiguration.class);
54      }
55  
56      @Test
57      @Reference("235")
58      public void shouldAddPet() {
59          given()
60              .a_swagger_petstore(client)
61              .with(configuration());
62  
63          when()
64              .adding(Pet.builder()
65                  .name(randomId())
66                  .build());
67  
68          then()
69              .the_available_pets(adaptedStream(pet -> pet.name,
70                  hasItemsMatching(equalTo(randomId()))));
71      }
72  
73      @Test(dataProviderClass = CredentialsCsvDataProvider.class,
74          dataProvider = DATA)
75      @Reference("235")
76      public void shouldLogin(final Credentials credentials) {
77          given()
78              .a_swagger_petstore(client)
79              .with(configuration());
80  
81          when()
82              .logging_in_with(credentials);
83  
84          then()
85              .the_login_response(hasProperty("code", equalTo(200)))
86              .and().the_response_status(is(SUCCESSFUL));
87      }
88  
89      @AfterClass
90      protected void afterClassCloseRestClient() {
91          client.close();
92      }
93  
94      @BeforeClass
95      protected void beforeClassOpenRestClient() {
96          client = LoggingClientBuilder.newClient();
97      }
98  }