/*
 * Copyright (C) 2000 - 2016 Silverpeas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * As a special exception to the terms and conditions of version 3.0 of
 * the GPL, you may redistribute this Program in connection with Free/Libre
 * Open Source Software ("FLOSS") applications as described in Silverpeas's
 * FLOSS exception. You should have recieved a copy of the text describing
 * the FLOSS exception, and it is also available here:
 * "http://www.silverpeas.org/docs/core/legal/floss_exception.html"
 *
 * 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.silverpeas.thumbnail;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.silverpeas.core.admin.OrganisationController;
import org.silverpeas.core.admin.OrganisationControllerFactory;
import org.silverpeas.test.rule.MockByReflectionRule;

import static com.silverpeas.thumbnail.ThumbnailSettings.APP_PARAM_HEIGHT;
import static com.silverpeas.thumbnail.ThumbnailSettings.APP_PARAM_WIDTH;
import static com.silverpeas.thumbnail.ThumbnailSettings.DEFAULT_SIZE;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.when;

/**
 * BE CAREFUL, this unit test verifies especially that a default width and a default height are
 * returned.<br/>
 * @author Yohann Chastagnier
 */
public class ThumbnailSettingsTest {

  private static final String COMPONENT_INSTANCE_ID = "componentInstanceId";

  private OrganisationController mockedOrganisationController;

  @Rule
  public MockByReflectionRule mockByReflectionRule = new MockByReflectionRule();

  @Before
  public void setup() {
    mockedOrganisationController = mockByReflectionRule
        .mockField(OrganisationControllerFactory.class, OrganisationController.class,
            "instance.organisationController");
    when(mockedOrganisationController
        .getComponentParameterValue(COMPONENT_INSTANCE_ID, APP_PARAM_WIDTH)).thenReturn("");
    // the height about component instance parameters is not handled with the mock in order to
    // verify the behavior when null is returned instead of ""
  }

  @Test
  public void shouldReturnDefaultSizeWhenComponentParametersAndDefaultPropertiesAreNotDefined() {
    ThumbnailSettings settings = ThumbnailSettings.getInstance(COMPONENT_INSTANCE_ID, -1, -1);
    assertThat(settings.getWidth(), is(DEFAULT_SIZE));
    assertThat(settings.getHeight(), is(DEFAULT_SIZE));
  }

  @Test
  public void defaultWidthPropertyShouldBeTakenIntoAccountAndAppliedOnHeightToo() {
    ThumbnailSettings settings = ThumbnailSettings.getInstance(COMPONENT_INSTANCE_ID, 26, -1);
    assertThat(settings.getWidth(), is(26));
    assertThat(settings.getHeight(), is(26));
  }

  @Test
  public void defaultHeightPropertyShouldBeTakenIntoAccountAndAppliedOnWidthToo() {
    ThumbnailSettings settings = ThumbnailSettings.getInstance(COMPONENT_INSTANCE_ID, -1, 38);
    assertThat(settings.getWidth(), is(38));
    assertThat(settings.getHeight(), is(38));
  }

  @Test
  public void defaultSizePropertiesShouldBeTakenIntoAccount() {
    ThumbnailSettings settings = ThumbnailSettings.getInstance(COMPONENT_INSTANCE_ID, 26, 38);
    assertThat(settings.getWidth(), is(26));
    assertThat(settings.getHeight(), is(38));
  }

  @Test
  public void defaultComponentInstanceWidthParameterShouldBeTakenIntoAccountAndAppliedOnHeightToo
      () {
    when(mockedOrganisationController
        .getComponentParameterValue(COMPONENT_INSTANCE_ID, APP_PARAM_WIDTH)).thenReturn("26");
    ThumbnailSettings settings = ThumbnailSettings.getInstance(COMPONENT_INSTANCE_ID, -1, -1);
    assertThat(settings.getWidth(), is(26));
    assertThat(settings.getHeight(), is(26));
  }

  @Test
  public void defaultComponentInstanceHeightParameterShouldBeTakenIntoAccountAndAppliedOnWidthToo
      () {
    when(mockedOrganisationController
        .getComponentParameterValue(COMPONENT_INSTANCE_ID, APP_PARAM_HEIGHT)).thenReturn("38");
    ThumbnailSettings settings = ThumbnailSettings.getInstance(COMPONENT_INSTANCE_ID, -1, -1);
    assertThat(settings.getWidth(), is(38));
    assertThat(settings.getHeight(), is(38));
  }

  @Test
  public void defaultComponentInstanceSizeParametersShouldBeTakenIntoAccount() {
    when(mockedOrganisationController
        .getComponentParameterValue(COMPONENT_INSTANCE_ID, APP_PARAM_WIDTH)).thenReturn("26");
    when(mockedOrganisationController
        .getComponentParameterValue(COMPONENT_INSTANCE_ID, APP_PARAM_HEIGHT)).thenReturn("38");
    ThumbnailSettings settings = ThumbnailSettings.getInstance(COMPONENT_INSTANCE_ID, -1, -1);
    assertThat(settings.getWidth(), is(26));
    assertThat(settings.getHeight(), is(38));
  }

  @Test
  public void
  componentInstanceWidthParameterShouldBeGivenPriorityOverTheDefaultWidthAndHeightOfProperties() {
    when(mockedOrganisationController
        .getComponentParameterValue(COMPONENT_INSTANCE_ID, APP_PARAM_WIDTH)).thenReturn("26");
    ThumbnailSettings settings = ThumbnailSettings.getInstance(COMPONENT_INSTANCE_ID, 1000, 1000);
    assertThat(settings.getWidth(), is(26));
    assertThat(settings.getHeight(), is(26));
  }

  @Test
  public void
  componentInstanceHeightParameterShouldBeGivenPriorityOverTheDefaultWidthAndHeightOfProperties() {
    when(mockedOrganisationController
        .getComponentParameterValue(COMPONENT_INSTANCE_ID, APP_PARAM_HEIGHT)).thenReturn("38");
    ThumbnailSettings settings = ThumbnailSettings.getInstance(COMPONENT_INSTANCE_ID, 1000, 1000);
    assertThat(settings.getWidth(), is(38));
    assertThat(settings.getHeight(), is(38));
  }
}
@@ -1272,33 +1272,33 @@
 ore.admin.Organi
-s
+z
 ationController;
@@ -1324,253 +1324,34 @@
 eas.
-core.admin.OrganisationControllerFactory;%0Aimport org.silverpeas.test.rule.MockByReflectionRule;%0A%0Aimport static com.silverpeas.thumbnail.ThumbnailSettings.APP_PARAM_HEIGHT;%0Aimport static com.silverpeas.thumbnail.ThumbnailSettings.APP_PARAM_WIDTH;
+test.rule.CommonAPI4Test;%0A
 %0Aimp
@@ -1404,28 +1404,17 @@
 ettings.
-DEFAULT_SIZE
+*
 ;%0Aimport
@@ -1500,16 +1500,56 @@
 ers.is;%0A
+import static org.mockito.Mockito.mock;%0A
 import s
@@ -1861,25 +1861,25 @@
 ivate Organi
-s
+z
 ationControl
@@ -1934,76 +1934,58 @@
 lic 
-MockByReflectionRule mockByReflectionRule = new MockByReflectionRule
+CommonAPI4Test commonAPI4Test = new CommonAPI4Test
 ();%0A
@@ -2062,51 +2062,16 @@
 mock
-ByReflectionRule%0A        .mockField
 (Organi
-s
+z
 atio
@@ -2085,98 +2085,14 @@
 ller
-Factory.class, OrganisationController.class,%0A            %22instance.organisationController%22
+.class
 );%0A 
@@ -2217,24 +2217,104 @@
 Return(%22%22);%0A
+    commonAPI4Test.injectIntoMockedBeanContainer(mockedOrganisationController);%0A
     // the h

Fix a new test fetched from core-5.15.x by taking into account the peculiarities of 6.0