/*
 * Copyright 2016 Red Hat, Inc. and/or its affiliates
 * and other contributors as indicated by the @author tags.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.keycloak.testsuite.migration;

import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.testsuite.AbstractKeycloakTest;
import org.keycloak.testsuite.Assert;
import org.keycloak.testsuite.arquillian.migration.Migration;

/**
 * @author <a href="mailto:vramik@redhat.com">Vlastislav Ramik</a>
 */
public class MigrationTest extends AbstractKeycloakTest {

    private final String migrationRealmName = "Migration";
        
    @Override
    public void addTestRealms(List<RealmRepresentation> testRealms) {
        log.info("Adding no test realms for migration test. Test realm should be migrated from previous vesrion.");
    }
    
    @Before
    public void beforeMigrationTest() {
        
    }
    
    @Test
    @Migration(versionFrom = "1.9.8.Final")
    public void migration198Test() {
        //test migrated data
        test198();
        
        //import realm from previous version and test imported data
        MigrationUtil.executeImport(testingClient);
        test198();
    }
    
    private void test198() {
        RealmResource realmResource = adminClient.realms().realm(migrationRealmName);
        
        Assert.assertNames(realmResource.roles().list(), "offline_access", "uma_authorization");
        Assert.assertNames(realmResource.clients().findAll(), "admin-cli", "realm-management", "security-admin-console", "broker", "account");
        
        //TODO - add more asserts
        
        //cleanup
        RealmRepresentation realmRep = realmResource.toRepresentation();
        log.info("removing '" + realmRep.getRealm() + "' realm");
        removeRealm(realmRep);
    }
}
