/**
 * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
 *
 * WSO2 LLC. licenses this file to you 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 com.wso2.openbanking.accelerator.identity.auth.extensions.request.validator;

import com.wso2.openbanking.accelerator.common.exception.ConsentManagementException;
import com.wso2.openbanking.accelerator.common.validator.OpenBankingValidator;
import com.wso2.openbanking.accelerator.consent.mgt.dao.models.DetailedConsentResource;
import com.wso2.openbanking.accelerator.consent.mgt.service.impl.ConsentCoreServiceImpl;
import com.wso2.openbanking.accelerator.identity.auth.extensions.request.validator.models.FapiRequestObject;
import com.wso2.openbanking.accelerator.identity.auth.extensions.request.validator.models.ValidationResponse;
import com.wso2.openbanking.accelerator.identity.internal.IdentityExtensionsDataHolder;
import com.wso2.openbanking.accelerator.identity.utils.TestUtils;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.Collections;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

@PrepareForTest({OpenBankingValidator.class, IdentityExtensionsDataHolder.class})
@PowerMockIgnore("jdk.internal.reflect.*")
public class FapiRequestObjectValidatorTest extends PowerMockTestCase {

    private static final String CLIENT_ID_1 = "2X0n9WSNmPiq3XTB8dtC0Shs5r8a";
    private static ConsentCoreServiceImpl consentCoreServiceMock;

    @BeforeClass
    public void initTest() {

        consentCoreServiceMock = PowerMockito.mock(ConsentCoreServiceImpl.class);
    }

    @BeforeMethod
    private void mockStaticClasses() throws ConsentManagementException {

        PowerMockito.mockStatic(IdentityExtensionsDataHolder.class);
        IdentityExtensionsDataHolder mock = PowerMockito.mock(IdentityExtensionsDataHolder.class);
        PowerMockito.when(IdentityExtensionsDataHolder.getInstance()).thenReturn(mock);
        PowerMockito.when(IdentityExtensionsDataHolder.getInstance().getConsentCoreService())
                .thenReturn(consentCoreServiceMock);
    }

    @Test
    public void testValidateOBConstraintsWithValidRequestObject() throws Exception {
        // mock
        DetailedConsentResource consentResourceMock = mock(DetailedConsentResource.class);
        doReturn(CLIENT_ID_1).when(consentResourceMock).getClientID();

        doReturn(consentResourceMock).when(consentCoreServiceMock).getDetailedConsent(anyString());

        OpenBankingValidator openBankingValidatorMock = mock(OpenBankingValidator.class);
        doReturn("").when(openBankingValidatorMock).getFirstViolation(Mockito.anyObject(),
                Mockito.anyObject());

        PowerMockito.mockStatic(OpenBankingValidator.class);
        PowerMockito.when(OpenBankingValidator.getInstance()).thenReturn(openBankingValidatorMock);

        // act
        FapiRequestObjectValidator uut = new FapiRequestObjectValidator();

        FapiRequestObject obRequestObject = TestUtils
                .getFapiRequestObject(ReqObjectTestDataProvider.VALID_FAPI_REQUEST_OBJ);
        ValidationResponse validationResponse = uut.validateOBConstraints(obRequestObject, Collections.emptyMap());

        // assert
        Assert.assertTrue(validationResponse.isValid());
    }

    @Test
    public void testValidateOBConstraintsWithInValidRequestObject() throws Exception {
        // mock
        DetailedConsentResource consentResourceMock = mock(DetailedConsentResource.class);
        doReturn(CLIENT_ID_1).when(consentResourceMock).getClientID();

        doReturn(consentResourceMock).when(consentCoreServiceMock).getDetailedConsent(anyString());

        OpenBankingValidator openBankingValidatorMock = mock(OpenBankingValidator.class);
        doReturn("dummy-error").when(openBankingValidatorMock).getFirstViolation(Mockito.anyObject(),
                Mockito.anyObject());

        PowerMockito.mockStatic(OpenBankingValidator.class);
        PowerMockito.when(OpenBankingValidator.getInstance()).thenReturn(openBankingValidatorMock);

        // act
        FapiRequestObjectValidator uut = new FapiRequestObjectValidator();

        FapiRequestObject obRequestObject = TestUtils
                .getFapiRequestObject(ReqObjectTestDataProvider.FAPI_REQ_OBJ_WITHOUT_CLIENT_ID);
        ValidationResponse validationResponse = uut.validateOBConstraints(obRequestObject, Collections.emptyMap());

        // assert
        Assert.assertFalse(validationResponse.isValid());
    }

    @Test
    public void testValidateOBConstraintsWithoutExpInRequestObject() throws Exception {
        // mock
        DetailedConsentResource consentResourceMock = mock(DetailedConsentResource.class);
        doReturn(CLIENT_ID_1).when(consentResourceMock).getClientID();

        doReturn(consentResourceMock).when(consentCoreServiceMock).getDetailedConsent(anyString());

        OpenBankingValidator openBankingValidatorMock = mock(OpenBankingValidator.class);
        doReturn("dummy-error").when(openBankingValidatorMock).getFirstViolation(Mockito.anyObject(),
                Mockito.anyObject());

        PowerMockito.mockStatic(OpenBankingValidator.class);
        PowerMockito.when(OpenBankingValidator.getInstance()).thenReturn(openBankingValidatorMock);

        // act
        FapiRequestObjectValidator uut = new FapiRequestObjectValidator();

        FapiRequestObject obRequestObject = TestUtils
                .getFapiRequestObject(ReqObjectTestDataProvider.FAP_REQ_OBJ_WITHOUT_EXP);
        ValidationResponse validationResponse = uut.validateOBConstraints(obRequestObject, Collections.emptyMap());

        // assert
        Assert.assertFalse(validationResponse.isValid());
    }

}
