package cz.sparko.boxitory.test.integration;

import org.springframework.test.context.TestPropertySource;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8;
import static org.springframework.http.MediaType.TEXT_HTML;
import static org.springframework.http.MediaType.TEXT_PLAIN;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;

@TestPropertySource(properties = {
        "box.sort_desc=true",
        "box.host_prefix=test_prefix"
})
public class MultiProviderTest extends AbstractIntegrationTest {

    private final String VM = "vm";
    private final String VM_1_VBOX = VM + "_1_virtualbox.box";
    private final String VM_2_VBOX = VM + "_2_virtualbox.box";
    private final String VM_2_LVIRT = VM + "_2_libvirt.box";

    @Override
    void createFolderStructure() throws IOException {
        createRepositoryDir();
        File vmDir = createDirInRepository(VM);
        createFile(vmDir.getPath() + File.separator + VM_1_VBOX);
        createFile(vmDir.getPath() + File.separator + VM_2_VBOX);
        createFile(vmDir.getPath() + File.separator + VM_2_LVIRT);
    }

    @Test
    public void givenMultiProviders_whenIndex_thenReturnListWithVm() throws Exception {
        mockMvc.perform(get("/"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(content().contentType(TEXT_HTML + UTF8_CHARSET))
                .andExpect(view().name("index"))
                .andExpect(content().string(containsString(VM)));
    }

    @Test
    public void givenMultiProviders_whenBox_thenReturnListOfVmVersions() throws Exception {
        mockMvc.perform(get("/vm"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(content().contentType(APPLICATION_JSON_UTF8))
                .andExpect(jsonPath("$.name", is(VM)))
                .andExpect(jsonPath("$.description", is(VM)))
                .andExpect(jsonPath("$.versions", hasSize(2)))
                .andExpect(jsonPath("$.versions[0].version", is("2")))
                .andExpect(jsonPath("$.versions[0].providers", hasSize(2)))
                .andExpect(jsonPath("$.versions[0].providers[0].name", is("libvirt")))
                .andExpect(jsonPath("$.versions[0].providers[0].url", containsString(appProperties.getHost_prefix())))
                .andExpect(jsonPath("$.versions[0].providers[0].url", containsString(VM_2_LVIRT)))
                .andExpect(jsonPath("$.versions[0].providers[1].name", is("virtualbox")))
                .andExpect(jsonPath("$.versions[0].providers[1].url", containsString(appProperties.getHost_prefix())))
                .andExpect(jsonPath("$.versions[0].providers[1].url", containsString(VM_2_VBOX)))
                .andExpect(jsonPath("$.versions[1].version", is("1")))
                .andExpect(jsonPath("$.versions[1].providers", hasSize(1)))
                .andExpect(jsonPath("$.versions[1].providers[0].name", is("virtualbox")))
                .andExpect(jsonPath("$.versions[1].providers[0].url", containsString(appProperties.getHost_prefix())))
                .andExpect(jsonPath("$.versions[1].providers[0].url", containsString(VM_1_VBOX)));
    }

    @Test
    public void givenMultiProviders_whenLatestVersion_thenReturnLatestVersionNumber() throws Exception {
        mockMvc.perform(get("/vm/latestVersion"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(content().contentType(TEXT_PLAIN + UTF8_CHARSET))
                .andExpect(content().string("2"));
    }
}

@@ -2878,119 +2878,100 @@
 ers%5B
-0%5D
+?(@
 .name
-%22, is(%22libvirt%22)))%0A                .andExpect(jsonPath(%22$.versions%5B0%5D.providers%5B0%5D.url%22, containsString(
+ == %5C'libvirt%5C' && %22 +%0A                                            %22@.url =~ /.*%22 + 
 appP
@@ -2988,35 +2988,47 @@
 getHost_prefix()
-)))
+ + %22.*/i && %22 +
 %0A               
@@ -3032,88 +3032,83 @@
     
-.andExpect(jsonPath(%22$.versions%5B0%5D.providers%5B0%5D.url%22, containsString(VM_2_LVIRT)
+                            %22@.url =~ /.*%22 + VM_2_LVIRT + %22.*/i)%5D%22).exists(
 ))%0A 
@@ -3171,22 +3171,22 @@
 ers%5B
-1%5D
+?(@
 .name
-%22, is(%22
+ == %5C'
 virt
@@ -3183,36 +3183,41 @@
  == %5C'virtualbox
-%22)))
+%5C' && %22 +
 %0A               
@@ -3221,77 +3221,53 @@
     
-.andExpect(jsonPath(%22$.versions%5B0%5D.providers%5B1%5D.url%22, containsString(
+                            %22@.url =~ /.*%22 + 
 appP
@@ -3284,35 +3284,47 @@
 getHost_prefix()
-)))
+ + %22.*/i && %22 +
 %0A               
@@ -3328,87 +3328,82 @@
     
-.andExpect(jsonPath(%22$.versions%5B0%5D.providers%5B1%5D.url%22, containsString(VM_2_VBOX)
+                            %22@.url =~ /.*%22 + VM_2_VBOX + %22.*/i)%5D%22).exists(
 ))%0A 

generalize e2e multi-provider test assert