package teams.lifecycle;

import org.junit.Test;
import teams.AbstractApplicationTest;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import static io.restassured.RestAssured.given;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import static org.junit.Assert.*;

public class UserLifeCycleControllerTest extends AbstractApplicationTest {

    private String personUrn = "urn:collab:person:example.com:unhappy";

    @Test
    public void preview() {
        LifeCycleResult result = given()
                .auth()
                .preemptive()
                .basic("life", "secret")
                .when()
                .get("deprovision/{user}", personUrn)
                .as(LifeCycleResult.class);
        assertLifeCycleResult(result);
    }

    @Test
    public void previewUnauthenticated() {
        given()
                .auth()
                .preemptive()
                .basic("life", "nope")
                .when()
                .get("deprovision/{user}", personUrn)
                .then()
                .statusCode(403);
    }

    @Test
    public void dryRun() {
        LifeCycleResult result = doDeprovision(true);
        assertLifeCycleResult(result);
    }

    @Test
    public void deprovision() {
        LifeCycleResult result = doDeprovision(false);
        assertLifeCycleResult(result);

        result = doDeprovision(false);
        assertEquals(0, result.getData().size());
    }

    private LifeCycleResult doDeprovision(boolean dryRun) {
        return given()
                .auth()
                .preemptive()
                .basic("life", "secret")
                .when()
                .delete("deprovision/{user}" + (dryRun ? "/dry-run" : ""), this.personUrn)
                .as(LifeCycleResult.class);
    }

    private void assertLifeCycleResult(LifeCycleResult result) {
        Map<String, String> map = result.getData().stream().collect(toMap(attr -> attr.getName(), attr -> attr.getValue()));
        assertEquals(6 ,map.size());
        assertEquals(map.get("email"), "unhappy@domain.net");
        assertEquals(map.get("joinRequest"), "orphans");
        assertEquals(map.get("lastLoginDate").substring(0,10), "2018-05-17");
        assertEquals(map.get("membership"), "orphans");
        assertEquals(map.get("name"), "Unhappy User");
        assertEquals(map.get("urn"), "urn:collab:person:example.com:unhappy");
    }

}
@@ -85,85 +85,8 @@
 t;%0A%0A
-import java.util.Arrays;%0Aimport java.util.Comparator;%0Aimport java.util.List;%0A
 impo
@@ -156,58 +156,8 @@
 en;%0A
-import static java.util.stream.Collectors.toList;%0A
 impo
@@ -236,9 +236,20 @@
 ert.
-*
+assertEquals
 ;%0A%0Ap
@@ -1131,16 +1131,32 @@
 ion(true
+, this.personUrn
 );%0A     
@@ -1282,32 +1282,48 @@
 eprovision(false
+, this.personUrn
 );%0A        asser
@@ -1385,16 +1385,209 @@
 on(false
+, this.personUrn);%0A        assertEquals(0, result.getData().size());%0A    %7D%0A%0A    @Test%0A    public void deprovisionNonExistentUser() %7B%0A        LifeCycleResult result = doDeprovision(false, %22nope%22
 );%0A     
@@ -1694,16 +1694,34 @@
 n dryRun
+, String personUrn
 ) %7B%0A    
@@ -1933,21 +1933,16 @@
  : %22%22), 
-this.
 personUr
@@ -2211,10 +2211,10 @@
 ls(6
- 
 ,
+ 
 map.
@@ -2399,16 +2399,17 @@
 tring(0,
+ 
 10), %2220

Test for non-existent urn