package de.gessnerfl.rabbitmq.queue.management.controller;

import de.gessnerfl.rabbitmq.queue.management.util.RabbitMqTestEnvironmentBuilder;
import de.gessnerfl.rabbitmq.queue.management.util.RabbitMqTestEnvironmentBuilderFactory;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

public class IndexControllerIntegrationTest extends AbstractControllerIntegrationTest {

    public static final String EXCHANGE_NAME = "exchange";
    public static final String QUEUE_1_NAME = "queue1";
    public static final String QUEUE_2_NAME = "queue2";
    @Autowired
    private RabbitMqTestEnvironmentBuilderFactory testEnvironmentBuilderFactor;

    @Test
    public void shouldRedirectToIndexPageOnRoot() throws Exception {
        mockMvc.perform(get("/"))
                .andExpect(status().is3xxRedirection())
                .andExpect(redirectedUrl("/index"));
    }

    @Test
    public void shouldReturnListOfQueuesOnIndexPage() throws Exception {
        RabbitMqTestEnvironmentBuilder builder = testEnvironmentBuilderFactor.create();
        var testEnvironment = builder.withExchange(EXCHANGE_NAME)
                .withQueue(QUEUE_1_NAME)
                .exchange(EXCHANGE_NAME)
                .build()
                .withQueue(QUEUE_2_NAME)
                .exchange(EXCHANGE_NAME)
                .build()
                .build();
        testEnvironment.setup();

        try {
            mockMvc.perform(get("/index"))
                    .andExpect(status().isOk())
                    .andExpect(model().attribute("queues", containsInAnyOrder(QUEUE_1_NAME, QUEUE_2_NAME)));
        } finally {
            testEnvironment.cleanup();
        }
    }
}

@@ -53,16 +53,161 @@
 oller;%0A%0A
+import de.gessnerfl.rabbitmq.queue.management.model.remoteapi.Queue;%0Aimport de.gessnerfl.rabbitmq.queue.management.util.RabbitMqTestEnvironment;%0A
 import d
@@ -371,16 +371,80 @@
 actory;%0A
+import org.hamcrest.CustomMatcher;%0Aimport org.hamcrest.Matcher;%0A
 import o
@@ -2040,16 +2040,29 @@
 nyOrder(
+matchesQueue(
 QUEUE_1_
@@ -2065,18 +2065,32 @@
 E_1_NAME
+)
 , 
+matchesQueue(
 QUEUE_2_
@@ -2096,16 +2096,17 @@
 _NAME)))
+)
 ;%0A      
@@ -2173,11 +2173,612 @@
  %7D%0A    %7D
+%0A%0A    private Matcher%3CQueue%3E matchesQueue(String name) %7B%0A        return matchesQueue(name, RabbitMqTestEnvironment.VHOST);%0A    %7D%0A%0A    private Matcher%3CQueue%3E matchesQueue(String name, String vhost) %7B%0A        return new CustomMatcher%3CQueue%3E(%22matches when name and vhost of queue matches%22) %7B%0A            @Override%0A            public boolean matches(Object actual) %7B%0A                if (actual instanceof Queue) %7B%0A                    return ((Queue) actual).getName().equals(name) && ((Queue) actual).getVhost().equals(vhost);%0A                %7D%0A                return false;%0A            %7D%0A        %7D;%0A    %7D
 %0A%7D%0A

fix IndexControllerIntegrationTest