package com.atypon.task1;

import com.atypon.task1.controllers.PostsController;
import com.atypon.task1.services.LocatorService;

import com.atypon.task1.utils.RequestUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;

import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@WebMvcTest(PostsController.class)
class PostsControllerTests {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private LocatorService locatorService;

    @Test
    void getAddressWithoutThrottling() throws Exception {
        String uri = "/api/posts";
        HttpServletRequest request = RequestUtils.generateMockAtyponRequest(uri);
        mockMvc.perform(get(uri).requestAttr("jakarta.servlet.http.HttpServletRequest", request))
                .andExpect(status().isOk())
                .andExpect(content().string("http://atypon.com/api/posts"));
        verify(locatorService, times(1)).getUniformResourceLocator(request);
    }

}


    public static HttpServletRequest generateMockAtyponRequest(String uri) {
        HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
        when(request.getScheme()).thenReturn("http");
        when(request.getHeader("Host")).thenReturn("atypon.com");
        when(request.getRequestURI()).thenReturn(uri);
        return request;
    }


@RequestMapping("/api/posts")
@RestController
@RequiredArgsConstructor
public class PostsController {

    private @NonNull LocatorService locatorService;

    @GetMapping
    public ResponseEntity<String> getAddress(HttpServletRequest request) {
        return ResponseEntity.ok(locatorService.getUniformResourceLocator(request));
    }

}


plugins {
    id 'java'
    id 'org.springframework.boot' version '3.1.0'
    id 'io.spring.dependency-management' version '1.1.0'
}

group = 'com.atypon'
version = '1.0'
sourceCompatibility = '17'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.mockito:mockito-core:3.12.4'
}

test {
    useJUnitPlatform()
}

im getting:
Response content expected:<http://atypon.com/api/posts> but was:<>
Expected :http://atypon.com/api/posts
Actual   :
<Click to see difference>

java.lang.AssertionError: Response content expected:<http://atypon.com/api/posts> but was:<>
	at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:59)
	at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:122)
	at org.springframework.test.web.servlet.result.ContentResultMatchers.lambda$string$4(ContentResultMatchers.java:148)