package de.accso.flexinale.common.shared_kernel;

import org.junit.jupiter.api.Test;

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

class ThreadSafeCounterMapTest {

    @Test
    public void testInitialMap() {
        // arrange
        ThreadSafeCounterMap<String> sut = new ThreadSafeCounterMap<>();

        // act
        Long key = sut.get("nothere");

        // assert
        assertThat(key).isEqualTo(0L);
    }

    @Test
    public void testSetAndGet() {
        // arrange
        ThreadSafeCounterMap<String> sut = new ThreadSafeCounterMap<>();
        Long fortyTwo = 42L;

        // act
        sut.set("key", fortyTwo);

        // assert
        assertThat(sut.get("key")).isEqualTo(fortyTwo);
        assertThat(sut.get("notthere")).isEqualTo(0L);
    }

    @Test
    public void testIncrementInitial() {
        // arrange
        ThreadSafeCounterMap<String> sut = new ThreadSafeCounterMap<>();

        // act
        long value = sut.increment("key");

        // assert
        assertThat(value).isEqualTo(1);
        assertThat(sut.get("key")).isEqualTo(1);

        // act
        value = sut.increment("key");

        // assert
        assertThat(value).isEqualTo(1+1);
        assertThat(sut.get("key")).isEqualTo(1+1);
    }

    @Test
    public void testIncrementAfterSet() {
        // arrange
        ThreadSafeCounterMap<String> sut = new ThreadSafeCounterMap<>();
        Long fortyTwo = 42L;
        sut.set("key", fortyTwo);

        // act
        long value = sut.increment("key");

        // assert
        assertThat(value).isEqualTo(fortyTwo+1);
        assertThat(sut.get("key")).isEqualTo(fortyTwo+1);
    }
}