package au.gov.dto.dibp.appointments.qflowintegration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

@Service
class ApiUserService {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    private final List<ApiUser> apiUsers;

    public ApiUserService() {
        int userCount = Integer.parseInt(getEnvironmentVariable("USER_COUNT"));
        apiUsers = new ArrayList<>();

        for(int i = 1; i<=userCount; i++){
            String username = getEnvironmentVariable("USER_USERNAME_"+i);
            String password = getEnvironmentVariable("USER_PASSWORD_"+i);
            String userId = getEnvironmentVariable("USER_ID_"+i);
            apiUsers.add(new ApiUser(username, password, userId));
        }
    }

    public ApiUserService(ApiUser... apiUsers) {
        this.apiUsers = Arrays.asList(apiUsers);
    }

    public List<ApiUser> initializeApiUsers(){
        return new ArrayList<>(apiUsers);
    }

    String getEnvironmentVariable(String name){
        String value =  System.getenv(name);
        if(StringUtils.isEmpty(value)){
            log.error("No value set for the environment variable "+ name);
        }
        return value;
    }
}

@@ -49,16 +49,61 @@
 ation;%0A%0A
+import org.apache.commons.lang3.StringUtils;%0A
 import o
@@ -201,53 +201,8 @@
 ice;
-%0Aimport org.springframework.util.StringUtils;
 %0A%0Aim
@@ -244,21 +244,26 @@
 va.util.
-Array
+Collection
 s;%0Aimpor
@@ -321,24 +321,31 @@
     private 
+static 
 final Logger
@@ -345,19 +345,19 @@
  Logger 
-log
+LOG
  = Logge
@@ -379,26 +379,32 @@
 ger(
-this.getC
+ApiUserService.c
 lass
-()
 );%0A
+%0A
     
@@ -439,16 +439,36 @@
 apiUsers
+ = new ArrayList%3C%3E()
 ;%0A%0A    p
@@ -504,119 +504,73 @@
     
+Str
 in
-t
+g
  user
-Count = Integer.parseInt(getEnvironmentVariable(%22USER_COUNT%22));%0A        apiUsers = new ArrayList%3C%3E()
+name;%0A        String password;%0A        String userId
 ;%0A
-%0A
     
@@ -576,16 +576,17 @@
      for
+ 
 (int i =
@@ -592,81 +592,69 @@
 = 1;
- i%3C=userCount; i++)%7B%0A            String username = getEnvironmentVariable
+%0A             StringUtils.isNotBlank(username = System.getenv
 (%22US
@@ -666,21 +666,27 @@
 ERNAME_%22
-+i);%0A
+ + i))%0A    
         
@@ -693,48 +693,63 @@
     
-String password = getEnvironmentVariable
+     && StringUtils.isNotBlank(password = System.getenv
 (%22US
@@ -761,21 +761,27 @@
 SSWORD_%22
-+i);%0A
+ + i))%0A    
         
@@ -780,31 +780,55 @@
             
-String 
+     && StringUtils.isNotBlank(
 userId = get
@@ -824,38 +824,29 @@
 serId = 
+System.
 get
-E
+e
 nv
-ironmentVariable
 (%22USER_I
@@ -852,13 +852,36 @@
 ID_%22
-+i
+ + i)
 );%0A
+             i++) %7B%0A
     
@@ -939,24 +939,136 @@
 , userId));%0A
+            LOG.info(%22Configuring API user number %7B%7D with username=%5B%7B%7D%5D and userid=%5B%7B%7D%5D%22, i, username, userId);%0A
         %7D%0A  
@@ -1133,38 +1133,42 @@
     
-this.apiUsers = Arrays.asList(
+Collections.addAll(this.apiUsers, 
 apiU
@@ -1226,16 +1226,17 @@
 iUsers()
+ 
 %7B%0A      
@@ -1280,254 +1280,7 @@
    %7D
-%0A%0A    String getEnvironmentVariable(String name)%7B%0A        String value =  System.getenv(name);%0A        if(StringUtils.isEmpty(value))%7B%0A            log.error(%22No value set for the environment variable %22+ name);%0A        %7D%0A        return value;%0A    %7D
 %0A%7D%0A

Remove need for USER_COUNT