/*
 * Copyright 2008-2026 Async-IO.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.atmosphere.interceptor;

import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.WriteListener;
import jakarta.servlet.http.HttpServletResponse;
import org.atmosphere.cpr.AsyncSupport;
import org.atmosphere.cpr.AtmosphereConfig;
import org.atmosphere.cpr.AtmosphereFramework;
import org.atmosphere.cpr.AtmosphereRequest;
import org.atmosphere.cpr.AtmosphereRequestImpl;
import org.atmosphere.cpr.AtmosphereResourceImpl;
import org.atmosphere.cpr.AtmosphereResponse;
import org.atmosphere.cpr.AtmosphereResponseImpl;
import org.atmosphere.cpr.HeaderConfig;
import org.mockito.Mockito;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Enumeration;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class SSEAtmosphereInterceptorTest {
    private AtmosphereFramework framework;
    private AtmosphereConfig config;

    @BeforeEach
    public void setup() throws Exception {
        framework = new AtmosphereFramework();
        framework.setAsyncSupport(Mockito.mock(AsyncSupport.class));
        framework.init(new ServletConfig() {
            @Override
            public String getServletName() {
                return "void";
            }

            @Override
            public ServletContext getServletContext() {
                return Mockito.mock(ServletContext.class);
            }

            @Override
            public String getInitParameter(String name) {
                return null;
            }

            @Override
            public Enumeration<String> getInitParameterNames() {
                return null;
            }
        });
        config = framework.getAtmosphereConfig();
    }

    @Test
    public void testDataWriter() throws Exception {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ServletResponse resp = Mockito.mock(HttpServletResponse.class);
        Mockito.when(resp.getOutputStream()).thenReturn(new ServletOutputStream() {
            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setWriteListener(WriteListener writeListener) {
            }

            @Override
            public void write(int b) throws IOException {
                baos.write(b);
            }
            @Override
            public void write(byte[] b) throws IOException {
                baos.write(b);
            }
            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                baos.write(b, off, len);
            }
        });

        AtmosphereRequest request = AtmosphereRequestImpl.newInstance();
        request.header(HeaderConfig.X_ATMOSPHERE_TRANSPORT, "SSE");
        AtmosphereResponse response = AtmosphereResponseImpl.newInstance(request);
        response.request(request);
        response.setResponse(resp);
        AtmosphereResourceImpl resource = new AtmosphereResourceImpl();
        resource.initialize(framework.getAtmosphereConfig(),
                framework.getBroadcasterFactory().get(),
                request, response, Mockito.mock(AsyncSupport.class), null);
        resource.suspend();
        
        SSEAtmosphereInterceptor interceptor = new SSEAtmosphereInterceptor();
        interceptor.configure(config);
        interceptor.inspect(resource);
        
        // no newline
        response.write("Good Morning".getBytes());
        assertEquals("data:Good Morning\r\n\r\n", baos.toString());
        baos.reset();
        
        // \n
        response.write("Hello World!\nHave a nice day!".getBytes());
        assertEquals("data:Hello World!\r\ndata:Have a nice day!\r\n\r\n", baos.toString());
        baos.reset();

        // \r
        response.write("Hello World!\rHave a nice day!".getBytes());
        assertEquals("data:Hello World!\r\ndata:Have a nice day!\r\n\r\n", baos.toString());
        baos.reset();

        // \r\n
        response.write("Hello World!\r\nHave a nice day!".getBytes());
        assertEquals("data:Hello World!\r\ndata:Have a nice day!\r\n\r\n", baos.toString());
    }
}
