/*
 * Copyright 2018-2022 adorsys GmbH & Co KG
 *
 * 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. 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 https://www.gnu.org/licenses/.
 *
 * This project is also available under a separate commercial license. You can
 * contact us at psd2@adorsys.com.
 */

package de.adorsys.xs2a.adapter.impl.signing.header;

import de.adorsys.xs2a.adapter.api.RequestHeaders;
import de.adorsys.xs2a.adapter.impl.signing.algorithm.EncodingAlgorithm;
import de.adorsys.xs2a.adapter.impl.signing.algorithm.EncodingAlgorithm.EncodingService;
import de.adorsys.xs2a.adapter.impl.signing.algorithm.HashingAlgorithm;
import de.adorsys.xs2a.adapter.impl.signing.algorithm.HashingAlgorithm.HashingService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class DigestTest {
    private static final String REQUEST_BODY = "{\"hello\": \"world\"}";
    private static final Charset UTF8_CHARSET = StandardCharsets.UTF_8;
    private static final byte[] HASHED_REQUEST_BODY = REQUEST_BODY.getBytes();

    private static final String HASHING_ALGORITHM_NAME = "SHA-256";
    private static final String DIGEST_VALUE = "X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=";
    private static final String EQUAL_SEPARATOR = "=";

    private static final String EXPECTED_DIGEST_HEADER_VALUE = HASHING_ALGORITHM_NAME + EQUAL_SEPARATOR + DIGEST_VALUE;

    @Mock
    private EncodingAlgorithm encodingAlgorithm;
    @Mock
    private EncodingService encodingService;
    @Mock
    private HashingAlgorithm hashingAlgorithm;
    @Mock
    private HashingService hashingService;

    @Test
    void build() {
        when(hashingAlgorithm.getHashingService())
            .thenReturn(hashingService);

        when(hashingService.hash(REQUEST_BODY, UTF8_CHARSET))
            .thenReturn(HASHED_REQUEST_BODY);

        when(encodingAlgorithm.getEncodingService())
            .thenReturn(encodingService);

        when(encodingService.encode(HASHED_REQUEST_BODY))
            .thenReturn(DIGEST_VALUE);

        when(hashingAlgorithm.getAlgorithmName())
            .thenReturn(HASHING_ALGORITHM_NAME);

        Digest digest = Digest.builder()
            .requestBody(REQUEST_BODY)
            .hashingAlgorithm(hashingAlgorithm)
            .encodingAlgorithm(encodingAlgorithm)
            .charset(UTF8_CHARSET)
            .build();

        assertThat(digest.getHeaderName()).isEqualTo(RequestHeaders.DIGEST);
        assertThat(digest.getHeaderValue()).isEqualTo(EXPECTED_DIGEST_HEADER_VALUE);
    }
}
