/**
 * This file is part of the Meeds project (https://meeds.io/).
 *
 * Copyright (C) 2020 - 2023 Meeds Association contact@meeds.io
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.exoplatform.web.security.codec;

import static org.junit.Assert.assertThrows;

import org.exoplatform.commons.api.settings.SettingService;
import org.exoplatform.component.test.AbstractKernelTest;
import org.exoplatform.component.test.ConfigurationUnit;
import org.exoplatform.component.test.ConfiguredBy;
import org.exoplatform.component.test.ContainerScope;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.container.xml.ValueParam;

@ConfiguredBy({
    @ConfigurationUnit(scope = ContainerScope.PORTAL, path = "conf/exo.portal.component.security-configuration-local.xml"),
})
public class CodecInitializerTest extends AbstractKernelTest {

  public void testStartFailsWhenKeyChanges() {
    CodecInitializer codecInitializer = getContainer().getComponentInstanceOfType(CodecInitializer.class);
    codecInitializer.start(); // Assert no exception when re-executing the start
                              // method

    SettingService settingService = getContainer().getComponentInstanceOfType(SettingService.class);
    InitParams initParams = new InitParams();
    ValueParam valueParam = new ValueParam();
    valueParam.setName(CodecInitializer.CONF_DIR_PARAM);
    valueParam.setValue(codecInitializer.getConfDir());
    initParams.addParam(valueParam);
    valueParam = new ValueParam();
    valueParam.setName(CodecInitializer.CODECKEY_RELATIVE_PATH_PARAM);
    valueParam.setValue("/codeckey_change.txt");
    initParams.addParam(valueParam);

    CodecInitializer testCodecInitializer = new CodecInitializer(settingService, initParams);
    assertThrows(CodecKeyChangedException.class, () -> testCodecInitializer.start());
  }

}
