package org.commonjava.indy.subsys.prefetch;

import org.commonjava.indy.model.core.RemoteRepository;
import org.commonjava.indy.pkg.maven.model.MavenPackageTypeDescriptor;
import org.commonjava.indy.subsys.prefetch.PrefetchRepoComparator;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.*;

/**
 * Copyright (C) 2013 Red Hat, Inc.
 *
 * 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.
 */

public class PrefetchRepoComparatorTest
{
    private final PrefetchRepoComparator<RemoteRepository> repoComparator = new PrefetchRepoComparator<>();

    @Test
    public void simpleTest()
    {
        RemoteRepository repo1 =
                new RemoteRepository( MavenPackageTypeDescriptor.MAVEN_PKG_KEY, "repo1", "http://localhost/test1" );
        RemoteRepository repo2 =
                new RemoteRepository( MavenPackageTypeDescriptor.MAVEN_PKG_KEY, "repo2", "http://localhost/test2" );

        repo1.setPrefetchPriority( 2 );
        repo2.setPrefetchPriority( 3 );
        assertTrue( repoComparator.compare( repo1, repo2 ) > 0 );

        repo2.setPrefetchRescanTimestamp( "2018-06-08 11:09:42 'UTC'Z" );
        assertTrue( repoComparator.compare( repo1, repo2 ) < 0 );

        repo1.setPrefetchRescanTimestamp( "2018-06-08 10:09:42 'UTC'Z" );
        repo2.setPrefetchRescanTimestamp( null );
        assertTrue( repoComparator.compare( repo1, repo2 ) > 0 );

        repo2.setPrefetchPriority( 1 );
        repo2.setPrefetchRescanTimestamp( "2018-06-08 11:09:42 'UTC'Z" );
        assertTrue( repoComparator.compare( repo1, repo2 ) < 0 );

    }

    @Test
    public void collectionTest()
    {

        RemoteRepository repo1 =
                new RemoteRepository( MavenPackageTypeDescriptor.MAVEN_PKG_KEY, "repo1", "http://localhost/test1" );
        RemoteRepository repo2 =
                new RemoteRepository( MavenPackageTypeDescriptor.MAVEN_PKG_KEY, "repo2", "http://localhost/test2" );
        RemoteRepository repo3 =
                new RemoteRepository( MavenPackageTypeDescriptor.MAVEN_PKG_KEY, "repo3", "http://localhost/test3" );

        repo1.setPrefetchPriority( 1 );
        repo2.setPrefetchPriority( 2 );
        repo3.setPrefetchPriority( 3 );

        List<RemoteRepository> list = new ArrayList<>( 3 );

        list.add( repo1 );
        list.add( repo2 );
        list.add( repo3 );
        list.sort( repoComparator );
        assertEquals( repo3, list.get( 0 ) );

        repo3.setPrefetchRescanTimestamp("2018-06-08 11:09:42 'UTC'Z"  );
        list.sort( repoComparator );
        assertEquals( repo2, list.get( 0 ) );

        repo2.setPrefetchRescanTimestamp("2018-06-08 09:09:42 'UTC'Z"  );
        list.sort( repoComparator );
        assertEquals( repo1, list.get( 0 ) );

        repo1.setPrefetchRescanTimestamp("2018-06-08 08:09:42 'UTC'Z"  );
        list.sort( repoComparator );
        assertEquals( repo1, list.get( 0 ) );

        repo2.setPrefetchRescanTimestamp("2018-06-08 08:09:42 'UTC'Z"  );
        list.sort( repoComparator );
        assertEquals( repo2, list.get( 0 ) );

        repo3.setPrefetchRescanTimestamp("2018-06-08 08:09:42 'UTC'Z"  );
        list.sort( repoComparator );
        assertEquals( repo3, list.get( 0 ) );
    }
}

@@ -253,24 +253,56 @@
 unit.Test;%0A%0A
+import java.time.ZonedDateTime;%0A
 import java.
@@ -1682,38 +1682,39 @@
 -06-08 11:09:42 
-'
 UTC
-'Z
++000
 %22 );%0A        ass
@@ -1828,30 +1828,32 @@
 08 10:09:42 
-'
 UTC
-'Z
++0000
 %22 );%0A       
@@ -2065,22 +2065,24 @@
 1:09:42 
-'
 UTC
-'Z
++0000
 %22 );%0A   
@@ -3058,30 +3058,32 @@
 08 11:09:42 
-'
 UTC
-'Z
++0000
 %22  );%0A      
@@ -3218,30 +3218,32 @@
 08 09:09:42 
-'
 UTC
-'Z
++0000
 %22  );%0A      
@@ -3374,38 +3374,40 @@
 -06-08 08:09:42 
-'
 UTC
-'Z
++0000
 %22  );%0A        li
@@ -3534,38 +3534,40 @@
 -06-08 08:09:42 
-'
 UTC
-'Z
++0000
 %22  );%0A        li
@@ -3706,14 +3706,16 @@
 :42 
-'
 UTC
-'Z
++0000
 %22  )

Fix a unit test error