/*
 * Copyright (C) 2004-2019, GoodData(R) Corporation. All rights reserved.
 * This source code is licensed under the BSD-style license found in the
 * LICENSE.txt file in the root directory of this source tree.
 */
package com.gooddata.sdk.service.featureflag;

import com.gooddata.sdk.service.AbstractGoodDataAT;
import com.gooddata.sdk.model.featureflag.FeatureFlag;
import com.gooddata.sdk.model.featureflag.FeatureFlags;
import com.gooddata.sdk.model.featureflag.ProjectFeatureFlag;
import com.gooddata.sdk.model.featureflag.ProjectFeatureFlags;
import org.hamcrest.Matchers;
import org.testng.annotations.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

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

    private static final String PROJECT_FEATURE_FLAG = "flag1";
    private static final String SECOND_FEATURE_FLAG = "flag2";

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

        checkProjectFeatureFlag(featureFlag, PROJECT_FEATURE_FLAG, true);
        checkProjectFeatureFlag(secondFeatureFlag, SECOND_FEATURE_FLAG, false);
    }

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

        assertThat(flags, hasItems(
                new ProjectFeatureFlag(SECOND_FEATURE_FLAG, false),
                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, Matchers.hasItems(
                new FeatureFlag(SECOND_FEATURE_FLAG, false),
                new 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, PROJECT_FEATURE_FLAG, 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);
        // can not check value because of eventual consistency of this API
        // checkProjectFeatureFlag(disabledFlag, false);

        // enable again
        featureFlag.setEnabled(true);
        final ProjectFeatureFlag enabledFlag = gd.getFeatureFlagService().updateProjectFeatureFlag(featureFlag);
        // can not check value because of eventual consistency of this API
        // checkProjectFeatureFlag(enabledFlag, true);
    }

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

        gd.getFeatureFlagService().deleteProjectFeatureFlag(featureFlag);
        // intentionally don't check the flag is really deleted because of eventual consistency of this API
    }

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

}

@@ -281,38 +281,25 @@
 ata.
-sdk.service.AbstractGoodDataAT
+GoodDataException
 ;%0Aim
@@ -528,24 +528,76 @@
 atureFlags;%0A
+import com.gooddata.sdk.service.AbstractGoodDataAT;%0A
 import org.h
@@ -651,16 +651,72 @@
 .Test;%0A%0A
+import static org.hamcrest.CoreMatchers.containsString;%0A
 import s
@@ -796,17 +796,152 @@
 atchers.
-*
+hasItems;%0Aimport static org.hamcrest.Matchers.is;%0Aimport static org.hamcrest.Matchers.notNullValue;%0Aimport static org.testng.Assert.fail
 ;%0A%0A/**%0A 
@@ -3494,86 +3494,8 @@
     
- // can not check value because of eventual consistency of this API%0A        //
  che
@@ -3528,16 +3528,38 @@
 ledFlag,
+ PROJECT_FEATURE_FLAG,
  false);
@@ -3746,86 +3746,8 @@
     
- // can not check value because of eventual consistency of this API%0A        //
  che
@@ -3779,16 +3779,39 @@
 ledFlag,
+ PROJECT_FEATURE_FLAG ,
  true);%0A
@@ -4236,107 +4236,841 @@
     
-// intentionally don't check the flag is really deleted because of eventual consistency of this API
+try %7B%0A            gd.getFeatureFlagService().getProjectFeatureFlag(project, %22settingName1%22);%0A            fail();%0A        %7D catch (GoodDataException e) %7B%0A            assertThat(e.getMessage(), containsString(%22Unable to get project feature flag:%22));%0A        %7D%0A    %7D%0A%0A    @Test(groups = %22featureFlag%22, dependsOnMethods = %22updateProjectFeatureFlag%22)%0A    public void changeProjectFeatureFlag() throws Exception %7B%0A        final FeatureFlagService featureFlagService = gd.getFeatureFlagService();%0A        ProjectFeatureFlag featureFlag = featureFlagService.getProjectFeatureFlag(project, PROJECT_FEATURE_FLAG);%0A        boolean value = !featureFlag.isEnabled();%0A        featureFlag.setEnabled(value);%0A        featureFlagService.updateProjectFeatureFlag(featureFlag);%0A        checkProjectFeatureFlag(featureFlag, PROJECT_FEATURE_FLAG, value);
 %0A   

test project feature flag change