package io.teiler.server.endpoints;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import io.restassured.http.ContentType;
import io.teiler.server.util.enums.Currency;

public class GroupEndpointControllerTest extends BaseEndpointControllerTest {

    private static final String GROUP_URL = URL_VERSION + "groups";
    
    private static final String POST_GROUP_NAME = "Manamana";
    private static final String GET_GROUP_ID = "gettgrup";
    private static final String GET_GROUP_NAME = "GetGroup";
    private static final String PUT_GROUP_ID = "puttgrup";
    private static final String PUT_GROUP_NAME = "PutGroupABC";
    private static final String DELETE_GROUP_ID = "deltgrup";
    
    private static final String PARAM_NAME = "name";
    private static final String PARAM_ID = "id";
    private static final String PARAM_CURRENCY = "currency";
    private static final String PARAM_UPDATE_TIME = "update-time";
    private static final String PARAM_CREATE_TIME = "create-time";

    @Test
    public void testPostGroup() {
        Map<String, String> requestBody = new HashMap<>();
        requestBody.put(PARAM_NAME, POST_GROUP_NAME);
        
        given()
            .contentType(ContentType.JSON)
            .body(requestBody)
            .when()
                .post(GROUP_URL)
            .then()
                .statusCode(200)
                .body(PARAM_NAME, equalTo(POST_GROUP_NAME))
                .body(PARAM_ID, is(notNullValue()))
                .body(PARAM_CURRENCY, is(notNullValue()))
                .body(PARAM_UPDATE_TIME, is(notNullValue()))
                .body(PARAM_CREATE_TIME, is(notNullValue()));
    }

    @Test
    public void testGetGroup() {
        given()
            .when()
                .get(GROUP_URL + "/" + GET_GROUP_ID)
            .then()
                .statusCode(200)
                .body(PARAM_ID, equalTo(GET_GROUP_ID))
                .body(PARAM_NAME, equalTo(GET_GROUP_NAME))
                .body(PARAM_CURRENCY, equalTo(Currency.CHF.toString().toLowerCase()))
                .body(PARAM_UPDATE_TIME, is(notNullValue()))
                .body(PARAM_CREATE_TIME, is(notNullValue()));
    }

    @Test
    public void testPutGroup() {
        Map<String, String> requestBody = new HashMap<>();
        requestBody.put(PARAM_NAME, PUT_GROUP_NAME);
        requestBody.put(PARAM_CURRENCY, Currency.EUR.toString().toLowerCase());
        
        given()
            .contentType(ContentType.JSON)
            .body(requestBody)
            .when()
                .put(GROUP_URL + "/" + PUT_GROUP_ID)
            .then()
                .statusCode(200)
                .body(PARAM_ID, equalTo(PUT_GROUP_ID))
                .body(PARAM_NAME, equalTo(PUT_GROUP_NAME))
                .body(PARAM_CURRENCY, equalTo(Currency.EUR.toString().toLowerCase()))
                .body(PARAM_UPDATE_TIME, is(notNullValue()))
                .body(PARAM_CREATE_TIME, is(notNullValue()));
    }
    
    @Test
    public void testDeleteGroup() {
        given()
            .when()
                .delete(GROUP_URL + "/" + DELETE_GROUP_ID)
            .then()
                .statusCode(200);
    }

}

@@ -520,20 +520,16 @@
 roups%22;%0A
-    
 %0A    pri
@@ -888,24 +888,159 @@
 tgrup%22;%0A    
+private static final String INVALID_CURRENCY_GROUP_NAME = %22InvalidCurrency%22;%0A    private static final String INVALID_CURRENCY = %22XYZ%22;%0A
 %0A    private
@@ -3292,21 +3292,17 @@
 ;%0A    %7D%0A
-    
 %0A
+
     @Tes
@@ -3446,32 +3446,32 @@
         .then()%0A
-
                 
@@ -3495,10 +3495,477 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testInvalidCurrency() %7B%0A        Map%3CString, String%3E requestBody = new HashMap%3C%3E();%0A        requestBody.put(PARAM_NAME, INVALID_CURRENCY_GROUP_NAME);%0A        requestBody.put(PARAM_CURRENCY, INVALID_CURRENCY);%0A        %0A        given()%0A            .contentType(ContentType.JSON)%0A            .body(requestBody)%0A            .when()%0A                .put(GROUP_URL + %22/%22 + PUT_GROUP_ID)%0A            .then()%0A                .statusCode(416);%0A    %7D%0A%0A
 %7D%0A

add test for invalid currency