package com.iqser.red.service.peristence.v1.server.integration.tests;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.iqser.red.service.peristence.v1.server.integration.utils.AbstractPersistenceServerServiceTest;
import com.iqser.red.service.peristence.v1.server.integration.utils.MultithreadedTestRunner;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.NotificationPreferencesPersistenceService;
import com.knecon.fforesight.tenantcommons.TenantContext;

import lombok.AccessLevel;
import lombok.SneakyThrows;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@FieldDefaults(level = AccessLevel.PRIVATE)
public class NotificationPreferencesServiceTest extends AbstractPersistenceServerServiceTest {

    @Autowired
    NotificationPreferencesPersistenceService notificationPreferencesPersistenceService;

    final MultithreadedTestRunner multithreadedTestRunner = new MultithreadedTestRunner(2, 100);


    @BeforeEach
    public void setup() {

        TenantContext.setTenantId("redaction");
    }


    @Test
    @SneakyThrows
    public void testNotificationPreferencesConcurrent() {

        final String userId = "1";
        Runnable test = () -> notificationPreferencesPersistenceService.getOrCreateNotificationPreferences(userId);
        Runnable afterTest = () -> notificationPreferencesPersistenceService.deleteNotificationPreferences(userId);
        var exceptions = multithreadedTestRunner.runMutlithreadedCollectingExceptions(test, afterTest);

        for (Exception ex : exceptions) {
            log.error("Exception during notification creation", ex);
        }

        assertThat(exceptions).isEmpty();
    }

}
