package org.jfrog.artifactory.client;

import org.jfrog.artifactory.client.model.RepoPath;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.Arrays.asList;
import static org.apache.commons.lang.StringUtils.countMatches;
import static org.testng.Assert.*;

/**
 * @author jbaruch
 * @author rnaegele
 * @since 03/08/12
 */
public class SearchTests extends ArtifactoryTestsBase {

    @Test
    public void testLimitlessQuickSearch() throws IOException {
        List<RepoPath> results = artifactory.searches().artifactsByName("junit").doSearch();
        assertEquals(results.size(), countMatches(curl("api/search/artifact?name=junit"), "{") - 1);
    }

    @Test
    public void testQuickSearchWithWrongSingleLimit() throws IOException {
        List<RepoPath> list = artifactory.searches().artifactsByName("junit").repositories(NEW_LOCAL).doSearch();
        assertTrue(list.isEmpty());
    }

    @Test
    public void testQuickSearchWithRightSingleLimit() throws IOException {
        List<RepoPath> results = artifactory.searches().artifactsByName("junit").repositories(JCENTER_CACHE).doSearch();
        assertEquals(results.size(), countMatches(curl("api/search/artifact?name=junit&repos="+JCENTER_CACHE), "{") - 1);
    }

    @Test(expectedExceptions = IllegalArgumentException.class)
    public void testQuickSearchWithoutSearchTerm() throws IOException {
        artifactory.searches().doSearch();
    }

    @Test
    public void testQuickSearchWithMultipleLimits() throws IOException {
        List<RepoPath> results =
                artifactory.searches().artifactsByName("junit").repositories(LIBS_RELEASE_LOCAL, JCENTER_CACHE)
                        .doSearch();
        assertEquals(results.size(),
                countMatches(curl("api/search/artifact?name=junit&repos="+ LIBS_RELEASE_LOCAL +","+JCENTER_CACHE), "{") - 1);
    }

    @Test
    public void testArtifactsCreatedSinceSearch() throws IOException {
        long aWeekAgo = System.currentTimeMillis() - 86400000L * 7;
        List<RepoPath> results = artifactory.searches().artifactsCreatedSince(aWeekAgo).doSearch();
        assertFalse(results.isEmpty());
    }

    @Test
    public void testArtifactsCreatedInDateRangeSearch() throws IOException {
        long aWeekAgo = System.currentTimeMillis() - 86400000L * 7;
        long now = System.currentTimeMillis();
        List<RepoPath> results = artifactory.searches().artifactsCreatedInDateRange(aWeekAgo, now).doSearch();
        assertFalse(results.isEmpty());
    }

    @Test(dependsOnGroups = "uploadBasics")
    public void testSearchByProperty() throws IOException {
        List<RepoPath> results = artifactory.searches().itemsByProperty().property("colors", "red").doSearch();
        assertEquals(results.size(), 1);
        assertTrue(results.get(0).getItemPath().contains("a/b/c.txt"));
    }

    @Test(dependsOnGroups = "uploadBasics")
    public void testSearchByPropertyWithMapNotation() throws IOException {
        Map<String, Boolean> properties = new HashMap<>();
        properties.put("released", false);
        List<RepoPath> results = artifactory.searches().itemsByProperty().properties(properties).doSearch();
        assertEquals(results.size(), 1);
        assertTrue(results.get(0).getItemPath().contains("a/b/c.txt"));
    }

    @Test(dependsOnGroups = "uploadBasics")
    public void testSearchByPropertyAndRepoFilter() throws IOException {
        List<RepoPath> results =
                artifactory.searches().itemsByProperty().property("released", false).repositories(NEW_LOCAL).doSearch();
        assertEquals(results.size(), 1);
        assertTrue(results.get(0).getItemPath().contains("a/b/c.txt"));
    }

    @Test(dependsOnGroups = "uploadBasics")
    public void testSearchByPropertyAndWrongRepoFilter() throws IOException {
        List<RepoPath> list = artifactory.searches().repositories(JCENTER).itemsByProperty().property("released",
                false).doSearch();
        assertTrue(list.isEmpty());
    }

    @Test(dependsOnGroups = "uploadBasics")
    public void testSearchByPropertyWithMulipleValue() throws IOException {
        List<RepoPath> list = artifactory.searches().itemsByProperty().property("colors", "red", "green",
                "blue").doSearch();
        assertTrue(list.isEmpty());
    }

    @Test(dependsOnGroups = "uploadBasics")
    public void testSearchByPropertyMapNotationWithMulipleValue() throws IOException {
        Map<String, List<String>> property = new HashMap<>();
        property.put("colors", asList("red", "green", "blue"));
        List<RepoPath> list = artifactory.searches().itemsByProperty().properties(property).doSearch();
        assertTrue(list.isEmpty());
    }

    @Test(dependsOnGroups = "uploadBasics")
    public void testSearchByPropertyWithoutValue() throws IOException {
        List<RepoPath> results =
                artifactory.searches().itemsByProperty().property("released").property("build", 28).doSearch();
        assertEquals(results.size(), 1);
        assertTrue(results.get(0).getItemPath().contains("a/b/c.txt"));
    }

    @Test(dependsOnGroups = "uploadBasics")
    public void testSearchByPropertyWithWildCards() throws IOException {
        List<RepoPath> results = artifactory.searches().itemsByProperty().property("colors", "r*?").doSearch();
        assertEquals(results.size(), 1);
        assertTrue(results.get(0).getItemPath().contains("a/b/c.txt"));
    }

    @Test(dependsOnGroups = "uploadBasics")
    public void testSearchByGavc() throws IOException {
        List<RepoPath> results = artifactory.searches().artifactsByGavc()
                .groupId("com.example")
                .artifactId("com.example.test")
                .version("1.0.0")
                .classifier("zip")
                .doSearch();
        assertEquals(results.size(), 1);
        assertTrue(results.get(0).getItemPath().contains("com.example.test-1.0.0-zip.jar"));
    }

    @Test(dependsOnGroups = "uploadBasics")
    public void testSearchByGavcAndRepository() throws IOException {
        List<RepoPath> results = artifactory.searches().artifactsByGavc()
                .groupId("com.example")
                .artifactId("com.example.test")
                .repositories(NEW_LOCAL)
                .doSearch();
        assertEquals(results.size(), 1);
        assertTrue(results.get(0).getItemPath().contains("com.example.test-1.0.0-zip.jar"));
    }
}

@@ -2040,76 +2040,8 @@
 n %7B%0A
-        long aWeekAgo = System.currentTimeMillis() - 86400000L * 7;%0A
     
@@ -2114,24 +2114,18 @@
 edSince(
-aWeekAgo
+0L
 ).doSear
@@ -2268,76 +2268,8 @@
 n %7B%0A
-        long aWeekAgo = System.currentTimeMillis() - 86400000L * 7;%0A
     
@@ -2399,16 +2399,10 @@
 nge(
-aWeekAgo
+0L
 , no

Fix tests