/**
 * This file is part of the Meeds project (https://meeds.io/).
 *
 * Copyright (C) 2020 - 2025 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.commons.info.test;

import static org.junit.Assert.assertNotEquals;

import javax.ws.rs.core.Response;

import org.apache.commons.lang3.StringUtils;

import org.exoplatform.commons.info.PlatformInformationRESTService;
import org.exoplatform.commons.info.PlatformInformationRESTService.JsonPlatformInfo;
import org.exoplatform.commons.info.ProductInformations;
import org.exoplatform.commons.testing.BaseResourceTestCase;
import org.exoplatform.component.test.*;
import org.exoplatform.portal.config.UserACL;
import org.exoplatform.services.rest.impl.ContainerResponse;
import org.exoplatform.services.rest.wadl.research.HTTPMethods;
import org.exoplatform.services.security.*;

@ConfiguredBy(
  {
      @ConfigurationUnit(scope = ContainerScope.ROOT, path = "conf/configuration.xml"),
      @ConfigurationUnit(scope = ContainerScope.PORTAL, path = "conf/portal/configuration.xml"),
      @ConfigurationUnit(scope = ContainerScope.PORTAL, path = "conf/commons-component-product-configuration.xml"),
  }
)
public class PlatformInformationRESTServiceTEST extends BaseResourceTestCase {

  @Override
  public void tearDown() throws Exception {
    super.tearDown();
    endSession();
  }

  @Override
  public void setUp() throws Exception {
    super.setUp();
    assertNotNull(getContainer().getComponentInstanceOfType(PlatformInformationRESTService.class));
    registry(new PlatformInformationRESTService(getService(ProductInformations.class), getService(UserACL.class)));
  }

  public void testGetPlatformInformationAnonymous() throws Exception { // NOSONAR
    ContainerResponse response = service(HTTPMethods.GET.toString(), "/platform/info", StringUtils.EMPTY, null, null);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    JsonPlatformInfo jsonPlatformInfo = (JsonPlatformInfo) response.getEntity();
    assertNotNull(jsonPlatformInfo);
    assertNull(jsonPlatformInfo.getBuildNumber());
    assertNull(jsonPlatformInfo.getDateOfKeyGeneration());
    assertNull(jsonPlatformInfo.getDuration());
    assertNull(jsonPlatformInfo.getIsMobileCompliant());
    assertNull(jsonPlatformInfo.getNbUsers());
    assertNull(jsonPlatformInfo.getPlatformBuildNumber());
    assertNull(jsonPlatformInfo.getPlatformEdition());
    assertNull(jsonPlatformInfo.getPlatformRevision());
    assertNull(jsonPlatformInfo.getProductCode());
    assertNull(jsonPlatformInfo.getRunningProfile());
    assertNull(jsonPlatformInfo.getUnlockKey());
    assertEquals(PlatformInformationRESTService.MIN_MOBILE_SUPPORTED_VERSION, jsonPlatformInfo.getPlatformVersion());
  }

  public void testGetPlatformInformationAuthenticatedUser() throws Exception { // NOSONAR
    startSessionAs("mary");
    ContainerResponse response = service(HTTPMethods.GET.toString(), "/platform/info", StringUtils.EMPTY, null, null);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    JsonPlatformInfo jsonPlatformInfo = (JsonPlatformInfo) response.getEntity();
    assertNotNull(jsonPlatformInfo);
    assertNull(jsonPlatformInfo.getBuildNumber());
    assertNull(jsonPlatformInfo.getDateOfKeyGeneration());
    assertNull(jsonPlatformInfo.getDuration());
    assertNull(jsonPlatformInfo.getIsMobileCompliant());
    assertNull(jsonPlatformInfo.getNbUsers());
    assertNull(jsonPlatformInfo.getPlatformBuildNumber());
    assertNull(jsonPlatformInfo.getPlatformEdition());
    assertNull(jsonPlatformInfo.getPlatformRevision());
    assertNull(jsonPlatformInfo.getProductCode());
    assertNull(jsonPlatformInfo.getRunningProfile());
    assertNull(jsonPlatformInfo.getUnlockKey());
    assertEquals(PlatformInformationRESTService.MIN_MOBILE_SUPPORTED_VERSION, jsonPlatformInfo.getPlatformVersion());
  }

  public void testGetPlatformInformationAuthenticatedAdminUser() throws Exception { // NOSONAR
    startSessionAs("root");
    ContainerResponse response = service(HTTPMethods.GET.toString(), "/platform/info", StringUtils.EMPTY, null, null);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    JsonPlatformInfo jsonPlatformInfo = (JsonPlatformInfo) response.getEntity();
    assertNotNull(jsonPlatformInfo);
    assertNotNull(jsonPlatformInfo.getPlatformBuildNumber());
    assertNotNull(jsonPlatformInfo.getPlatformEdition());
    assertNotNull(jsonPlatformInfo.getPlatformRevision());
    assertNotNull(jsonPlatformInfo.getPlatformVersion());
    assertNotEquals(PlatformInformationRESTService.MIN_MOBILE_SUPPORTED_VERSION, jsonPlatformInfo.getPlatformVersion());
  }

  private void startSessionAs(String user) {
    try {
      Authenticator authenticator = getContainer().getComponentInstanceOfType(Authenticator.class);
      Identity userIdentity = authenticator.createIdentity(user);
      ConversationState.setCurrent(new ConversationState(userIdentity));
    } catch (Exception e) {
      throw new IllegalStateException(e);
    }
  }

  private void endSession() {
    ConversationState.setCurrent(null);
  }

  private void registry(Object resource) {
    resourceBinder.addResource(resource, null);
  }

}
