/*
 * Copyright (C) 2007-2015, GoodData(R) Corporation. All rights reserved.
 */

package com.gooddata.featureflag;

import com.gooddata.AbstractGoodDataAT;
import com.gooddata.GoodDataException;
import com.gooddata.GoodDataRestException;
import org.springframework.http.HttpStatus;
import org.testng.annotations.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.hasItems;
import static org.testng.AssertJUnit.fail;

/**
 * Feature flag acceptance tests.
 */
public class FeatureFlagServiceAT extends AbstractGoodDataAT {

    private static final String PROJECT_FEATURE_FLAG = "testFeatureFlag";

    @Test(groups = "featureFlag", dependsOnGroups = "project")
    public void createProjectFeatureFlag() throws Exception {
        final ProjectFeatureFlag featureFlag = gd.getFeatureFlagService()
                .createProjectFeatureFlag(project, new ProjectFeatureFlag(PROJECT_FEATURE_FLAG));
        checkProjectFeatureFlag(featureFlag, true);
    }

    @Test(groups = "featureFlag", dependsOnMethods = "createProjectFeatureFlag")
    public void listProjectFeatureFlags() throws Exception {
        gd.getFeatureFlagService().createProjectFeatureFlag(project, new ProjectFeatureFlag("mostRecentFeatureFlag"));

        final ProjectFeatureFlags flags = gd.getFeatureFlagService().listProjectFeatureFlags(project);

        assertThat(flags, hasItems(
                new ProjectFeatureFlag("mostRecentFeatureFlag", true),
                new ProjectFeatureFlag(PROJECT_FEATURE_FLAG, true)));
    }

    @Test(groups = "featureFlag", dependsOnMethods = "listProjectFeatureFlags")
    public void listFeatureFlags() throws Exception {
        final FeatureFlags flags = gd.getFeatureFlagService().listFeatureFlags(project);

        assertThat(flags, hasItems(
                new com.gooddata.featureflag.FeatureFlag("mostRecentFeatureFlag", true),
                new com.gooddata.featureflag.FeatureFlag(PROJECT_FEATURE_FLAG, true)));
    }

    @Test(groups = "featureFlag", dependsOnMethods = "createProjectFeatureFlag")
    public void getProjectFeatureFlag() throws Exception {
        final ProjectFeatureFlag featureFlag =
                gd.getFeatureFlagService().getProjectFeatureFlag(project, PROJECT_FEATURE_FLAG);
        checkProjectFeatureFlag(featureFlag, true);
    }

    @Test(groups = "featureFlag", dependsOnMethods = "getProjectFeatureFlag")
    public void updateProjectFeatureFlag() throws Exception {
        final ProjectFeatureFlag featureFlag = gd.getFeatureFlagService().getProjectFeatureFlag(project,
                PROJECT_FEATURE_FLAG);

        // disable (update) feature flag
        featureFlag.setEnabled(false);
        final ProjectFeatureFlag disabledFlag = gd.getFeatureFlagService().updateProjectFeatureFlag(featureFlag);
        checkProjectFeatureFlag(disabledFlag, false);

        // enable again
        featureFlag.setEnabled(true);
        final ProjectFeatureFlag enabledFlag = gd.getFeatureFlagService().updateProjectFeatureFlag(featureFlag);
        checkProjectFeatureFlag(enabledFlag, true);
    }

    @Test(groups = "featureFlag", dependsOnMethods = "createProjectFeatureFlag")
    public void deleteProjectFeatureFlag() throws Exception {
        final ProjectFeatureFlag featureFlag =
                gd.getFeatureFlagService().createProjectFeatureFlag(project,
                        new ProjectFeatureFlag("temporaryFeatureFlag"));

        gd.getFeatureFlagService().deleteFeatureFlag(featureFlag);

        try {
            gd.getFeatureFlagService().getProjectFeatureFlag(project, featureFlag.getName());
            fail("Feature flag has not been deleted properly. HTTP status NOT FOUND expected.");
        } catch (GoodDataException e) {
            assertThat(((GoodDataRestException)e.getCause()).getStatusCode(), is(HttpStatus.NOT_FOUND.value()));
        }
    }


    private void checkProjectFeatureFlag(ProjectFeatureFlag featureFlag, boolean expectedValue) {
        assertThat(featureFlag, is(notNullValue()));
        assertThat(featureFlag.getName(), is(PROJECT_FEATURE_FLAG));
        assertThat(featureFlag.isEnabled(), is(expectedValue));
    }

}

@@ -406,106 +406,9 @@
 ers.
-is;%0Aimport static org.hamcrest.Matchers.notNullValue;%0Aimport static org.hamcrest.Matchers.hasItems
+*
 ;%0Aim
@@ -628,16 +628,95 @@
 reFlag%22;
+%0A    private static final String SECOND_FEATURE_FLAG = %22mostRecentFeatureFlag%22;
 %0A%0A    @T
@@ -1018,48 +1018,335 @@
     
-checkProjectFeatureFlag(featureFlag, tru
+final ProjectFeatureFlag secondFeatureFlag = gd.getFeatureFlagService()%0A                .createProjectFeatureFlag(project, new ProjectFeatureFlag(SECOND_FEATURE_FLAG, false));%0A%0A        checkProjectFeatureFlag(featureFlag, PROJECT_FEATURE_FLAG, true);%0A        checkProjectFeatureFlag(secondFeatureFlag, SECOND_FEATURE_FLAG, fals
 e);%0A
@@ -1498,128 +1498,8 @@
 n %7B%0A
-        gd.getFeatureFlagService().createProjectFeatureFlag(project, new ProjectFeatureFlag(%22mostRecentFeatureFlag%22));%0A%0A
     
@@ -1677,36 +1677,33 @@
 lag(
-%22mostRecentFeatureFlag%22, tru
+SECOND_FEATURE_FLAG, fals
 e),%0A
@@ -2063,122 +2063,69 @@
 new 
-com.gooddata.featureflag.FeatureFlag(%22mostRecentFeatureFlag%22, true),%0A                new com.gooddata.featureflag.
+FeatureFlag(SECOND_FEATURE_FLAG, false),%0A                new 
 Feat
@@ -2494,24 +2494,46 @@
 featureFlag,
+ PROJECT_FEATURE_FLAG,
  true);%0A    
@@ -3013,32 +3013,110 @@
 reFlag);%0A       
+ // can not check value because of eventual consistency of this API%0A        //
  checkProjectFea
@@ -3321,32 +3321,110 @@
 reFlag);%0A       
+ // can not check value because of eventual consistency of this API%0A        //
  checkProjectFea
@@ -4285,16 +4285,22 @@
 ureFlag(
+final 
 ProjectF
@@ -4322,16 +4322,90 @@
 ureFlag,
+ final String expectedName,%0A                                         final
  boolean
@@ -4520,36 +4520,28 @@
 e(), is(
-PROJECT_FEATURE_FLAG
+expectedName
 ));%0A    

Modify FeatureFlagServiceAT