package fi.vincit.multiusertest.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import org.junit.runners.model.FrameworkMethod;

import fi.vincit.multiusertest.annotation.MultiUserTestConfig;
import fi.vincit.multiusertest.annotation.RunWithUsers;
import fi.vincit.multiusertest.annotation.TestUsers;

public class TestMethodFilter {
    private final UserIdentifier producerIdentifier;
    private final UserIdentifier consumerIdentifier;

    public TestMethodFilter(UserIdentifier producerIdentifier, UserIdentifier consumerIdentifier) {
        Objects.requireNonNull(producerIdentifier);
        Objects.requireNonNull(consumerIdentifier);

        this.producerIdentifier = producerIdentifier;
        this.consumerIdentifier = consumerIdentifier;
    }

    public boolean shouldRun(FrameworkMethod frameworkMethod) {

        Optional<TestUsers> testUsersAnnotation =
                Optional.ofNullable(frameworkMethod.getAnnotation(TestUsers.class));
        Optional<RunWithUsers> runWithUsersAnnotation =
                Optional.ofNullable(frameworkMethod.getAnnotation(RunWithUsers.class));

        TestConfiguration configuration;
        if (testUsersAnnotation.isPresent()) {
            configuration = TestConfiguration.fromTestUsers(
                    testUsersAnnotation,
                    Optional.<MultiUserTestConfig>empty()
            );
        } else {
            configuration = TestConfiguration.fromRunWithUsers(
                    runWithUsersAnnotation,
                    Optional.<MultiUserTestConfig>empty()
            );
        }

        Collection<UserIdentifier> producers = configuration.getProducerIdentifiers();
        Collection<UserIdentifier> consumers = configuration.getConsumerIdentifiers();

        boolean shouldRun = true;
        if (!producers.isEmpty()) {
            shouldRun = producers.contains(producerIdentifier);
        }
        if (!consumers.isEmpty()) {
            shouldRun = shouldRun && consumers.contains(consumerIdentifier);
        }

        return shouldRun;
    }

    public List<FrameworkMethod> filter(List<FrameworkMethod> methods) {
        List<FrameworkMethod> methodsToRun = new ArrayList<>();
        for (FrameworkMethod frameworkMethod : methods) {
            if (shouldRun(frameworkMethod)) {
                methodsToRun.add(frameworkMethod);
            }
        }
        return methodsToRun;
    }
}

@@ -38,171 +38,8 @@
 %0D%0A%0D%0A
-import java.util.ArrayList;%0D%0Aimport java.util.Collection;%0D%0Aimport java.util.List;%0D%0Aimport java.util.Objects;%0D%0A%0D%0Aimport org.junit.runners.model.FrameworkMethod;%0D%0A%0D%0A
 impo
@@ -208,16 +208,177 @@
 tUsers;%0D
+%0Aimport org.junit.runners.model.FrameworkMethod;%0D%0A%0D%0Aimport java.util.ArrayList;%0D%0Aimport java.util.Collection;%0D%0Aimport java.util.List;%0D%0Aimport java.util.Objects;%0D
 %0A%0D%0Apubli
@@ -1249,20 +1249,23 @@
     if (
-test
+runWith
 UsersAnn
@@ -1338,20 +1338,23 @@
 ion.from
-Test
+RunWith
 Users(%0D%0A
@@ -1373,20 +1373,23 @@
         
-test
+runWith
 UsersAnn
@@ -1533,39 +1533,36 @@
 nfiguration.from
-RunWith
+Test
 Users(%0D%0A        
@@ -1565,39 +1565,36 @@
                 
-runWith
+test
 UsersAnnotation,

Fix #7 Fix order of RunWithUsers and TestUsers user annotation processing