package fi.hsl.parkandride;

import static java.util.Arrays.asList;
import static org.assertj.core.util.Strings.isNullOrEmpty;

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

import org.springframework.test.context.ActiveProfilesResolver;

import com.google.common.collect.Lists;

public abstract class ActiveProfileAppender implements ActiveProfilesResolver {
    private final List<String> profilesToAppend;

    public ActiveProfileAppender(String... profilesToAppend) {
        this.profilesToAppend = (List<String>) Arrays.asList(profilesToAppend);
    }

    @Override
    public String[] resolve(Class<?> testClass) {
        List<String> profiles = currentActiveProfiles();

        profilesToAppend.forEach((p) -> {
            if (!profiles.contains(p)) {
                profiles.add(p);
            }
        });

        return profiles.toArray(new String[profiles.size()]);
    }

    private List<String> currentActiveProfiles() {
        String springProfilesActive = System.getProperty("spring.profiles.active");
        if (isNullOrEmpty(springProfilesActive)) {
            springProfilesActive = System.getenv("SPRING_PROFILES_ACTIVE");
        }

        List<String> profiles = new ArrayList<>();
        if (!isNullOrEmpty(springProfilesActive)) {
            profiles.addAll(asList(springProfilesActive.split(",")));
        }
        return profiles;
    }
}

@@ -193,17 +193,71 @@
 .util.Li
-s
+nkedHashSet;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0A%0Aimpo
@@ -456,19 +456,18 @@
 e final 
-Lis
+Se
 t%3CString
@@ -586,23 +586,28 @@
 d = 
-(List%3CString%3E) 
+new LinkedHashSet%3C%3E(
 Arra
@@ -633,16 +633,17 @@
 oAppend)
+)
 ;%0A    %7D%0A
@@ -707,35 +707,34 @@
 lass) %7B%0A        
-Lis
+Se
 t%3CString%3E profil
@@ -763,17 +763,16 @@
 iles();%0A
-%0A
         
@@ -783,134 +783,34 @@
 iles
-ToAppend.forEach((p) -%3E %7B%0A            if (!profiles.contains(p)) %7B%0A                profiles.add(p);%0A            %7D%0A        %7D);%0A
+.addAll(profilesToAppend);
 %0A   
@@ -887,19 +887,18 @@
 private 
-Lis
+Se
 t%3CString
@@ -1155,19 +1155,18 @@
         
-Lis
+Se
 t%3CString
@@ -1182,24 +1182,28 @@
 s = new 
-ArrayLis
+LinkedHashSe
 t%3C%3E();%0A 

remove profile duplicates by using set