package io.mangoo.routing.routes;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import controllers.WebSocketController;
import io.mangoo.TestExtension;
import io.mangoo.routing.Router;

@ExtendWith({TestExtension.class})
class WebSocketRouteTest {
    
    @Test
    void testTo() {
        //given
        WebSocketRoute webSocketRoute = new WebSocketRoute();
        
        //when
        webSocketRoute.to("foo");
        
        //then
        assertThat(webSocketRoute.getUrl(), equalTo("/foo"));
        
        //when
        webSocketRoute.to("/bar");
        
        //then
        assertThat(webSocketRoute.getUrl(), equalTo("/bar"));
    }
    
    @Test
    void testWithController() {
        //given
        WebSocketRoute webSocketRoute = new WebSocketRoute();
        
        //when
        webSocketRoute.onController(WebSocketController.class);
        
        //then
        assertThat(webSocketRoute.getControllerClass(), equalTo(WebSocketController.class));
    }
    
    @Test
    void testAddingRoute() {
        //given
        String url = "/" + UUID.randomUUID().toString();
        WebSocketRoute webSocketRoute = new WebSocketRoute();
        
        //when
        webSocketRoute.to(url);
        
        //then
        Set<WebSocketRoute> webSocketRoutes = Router.getRoutes().stream()
                .filter(WebSocketRoute.class::isInstance)
                .map(WebSocketRoute.class::cast)
                .collect(Collectors.toSet());
        
        Set<WebSocketRoute> collectedRoutes = webSocketRoutes.stream()
                .filter(r -> r.getUrl().equals(url))
                .collect(Collectors.toSet());
        
        assertThat(collectedRoutes.size(), equalTo(1));
    }
    
    @Test
    void testWithAuthentication() {
        //given
        String url = "/" + UUID.randomUUID().toString();
        WebSocketRoute webSocketRoute = new WebSocketRoute();
        
        //when
        webSocketRoute.to(url).withAuthentication();
        
        //then
        Set<WebSocketRoute> webSocketRoutes = Router.getRoutes().stream()
                .filter(WebSocketRoute.class::isInstance)
                .map(WebSocketRoute.class::cast)
                .collect(Collectors.toSet());
        
        Set<WebSocketRoute> collectedRoutes = webSocketRoutes.stream()
                .filter(r -> r.getUrl().equals(url))
                .collect(Collectors.toSet());
        
        assertThat(collectedRoutes.size(), equalTo(1));
        assertThat(collectedRoutes.iterator().next().hasAuthentication(), equalTo(true));
    }
}
