View Javadoc
1   /*
2    * Copyright 2023 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.tutorial7;
18  
19  import static dev.aherscu.qa.testing.utils.StreamMatchersExtensions.*;
20  import static java.nio.charset.StandardCharsets.*;
21  import static java.util.Arrays.*;
22  
23  import org.testng.annotations.*;
24  
25  import dev.aherscu.qa.jgiven.rabbitmq.*;
26  import dev.aherscu.qa.jgiven.rabbitmq.model.*;
27  import dev.aherscu.qa.jgiven.rabbitmq.utils.*;
28  import dev.aherscu.qa.testing.utils.config.*;
29  import lombok.*;
30  
31  // TODO to make this test run under Github Actions
32  // see https://github.com/marketplace/actions/rabbitmq-in-github-actions
33  public class RabbitMqTest
34      extends AbstractRabbitMqTest<Integer, String> {
35  
36      /**
37       * Initializes the configuration type of this scenario by
38       * {@value AbstractConfiguration#CONFIGURATION_SOURCES}.
39       */
40      protected RabbitMqTest() {
41          super(TestConfiguration.class);
42      }
43  
44      @Test
45      @SneakyThrows
46      public void shouldRetrieve() {
47          given()
48              .a_queue(queueHandler);
49  
50          when()
51              .publishing(asList(
52                  Message.<String> builder()
53                      .content("hello")
54                      .build(),
55                  Message.<String> builder()
56                      .content("world")
57                      .build()))
58              .and().consuming();
59  
60          then()
61              .the_retrieved_messages(adaptedStream(message -> message.content,
62                  hasSpecificItems("world", "hello")));
63      }
64  
65      @BeforeMethod
66      @Override
67      @SneakyThrows
68      protected void beforeMethodInitiateQueueHandler() {
69          @SuppressWarnings("resource")
70          // according to docs channels are closed when connection is closed
71          val testingChannel = connection.createChannel();
72          queueHandler = QueueHandler.<Integer, String> builder()
73              .channel(testingChannel)
74              .queue(testingChannel.queueDeclare().getQueue())
75              .indexingBy(message -> message.content.hashCode())
76              .consumingBy(bytes -> new String(bytes, UTF_8))
77              .publishingBy(String::getBytes)
78              .build();
79      }
80  }