package uk.gov.register.core;

import org.junit.Before;
import org.junit.Test;
import uk.gov.register.exceptions.NoSuchFieldException;
import uk.gov.register.service.ItemValidator;
import uk.gov.register.store.BackingStoreDriver;

import java.util.ArrayList;

import static org.mockito.Mockito.*;

public class PostgresRegisterTest {
    private RegisterData registerData;
    private BackingStoreDriver backingStoreDriver;
    private ItemValidator itemValidator;

    @Before
    public void setup() {
        ArrayList<String> fields = new ArrayList<>();
        fields.add("name");

        RegisterMetadata registerMetadata = mock(RegisterMetadata.class);
        when(registerMetadata.getFields()).thenReturn(fields);

        registerData = mock(RegisterData.class);
        when(registerData.getRegister()).thenReturn(registerMetadata);
        backingStoreDriver = mock(BackingStoreDriver.class);
        itemValidator = mock(ItemValidator.class);
    }

    @Test(expected = NoSuchFieldException.class)
    public void findMax100RecordsByKeyValueShouldFailWhenKeyDoesNotExist() {
        PostgresRegister register = new PostgresRegister(registerData, backingStoreDriver, itemValidator);
        register.max100RecordsFacetedByKeyValue("citizen-name", "British");
    }

    @Test
    public void findMax100RecordsByKeyValueShouldReturnValueWhenKeyExists() {
        PostgresRegister register = new PostgresRegister(registerData, backingStoreDriver, itemValidator);
        register.max100RecordsFacetedByKeyValue("name", "United Kingdom");
        verify(backingStoreDriver, times(1)).findMax100RecordsByKeyValue("name", "United Kingdom");
    }
}
@@ -24,16 +24,68 @@
 .core;%0A%0A
+import com.fasterxml.jackson.databind.ObjectMapper;%0A
 import o
@@ -101,16 +101,16 @@
 Before;%0A
-
 import o
@@ -180,16 +180,79 @@
 eption;%0A
+import uk.gov.register.exceptions.NoSuchItemForEntryException;%0A
 import u
@@ -339,16 +339,42 @@
 river;%0A%0A
+import java.time.Instant;%0A
 import j
@@ -392,16 +392,43 @@
 rayList;
+%0Aimport java.util.Optional;
 %0A%0Aimport
@@ -1817,9 +1817,2365 @@
 ;%0A    %7D%0A
-%7D
+%0A    @Test(expected = NoSuchItemForEntryException.class)%0A    public void appendEntryShouldThrowExceptionIfNoCorrespondingItemExists() %7B%0A        Entry entryDangling = new Entry(106, %22item-hash-2%22, Instant.now());%0A%0A        when(backingStoreDriver.getItemBySha256(anyString())).thenReturn(Optional.empty());%0A%0A        PostgresRegister register = new PostgresRegister(registerData, backingStoreDriver, itemValidator);%0A        register.appendEntry(entryDangling);%0A    %7D%0A%0A    @Test%0A    public void appendEntryShouldNotInsertDanglingEntry() %7B%0A        Item item = new Item(%22item-hash-1%22, new ObjectMapper().createObjectNode());%0A        Entry entryNotDangling = new Entry(105, %22item-hash-1%22, Instant.now());%0A        Entry entryDangling = new Entry(106, %22item-hash-2%22, Instant.now());%0A%0A        when(backingStoreDriver.getItemBySha256(anyString()))%0A                .thenReturn(Optional.of(item))%0A                .thenReturn(Optional.empty());%0A%0A        PostgresRegister register = new PostgresRegister(registerData, backingStoreDriver, itemValidator);%0A%0A        try %7B%0A            register.appendEntry(entryNotDangling);%0A            register.appendEntry(entryDangling);%0A        %7D catch (NoSuchItemForEntryException ex) %7B%0A        %7D%0A%0A        verify(backingStoreDriver, times(1)).insertEntry(any());%0A    %7D%0A%0A    @Test%0A    public void appendEntryShouldNotInsertRecordForDanglingEntry() %7B%0A        Item item = new Item(%22item-hash-1%22, new ObjectMapper().createObjectNode());%0A        Entry entryNotDangling = new Entry(105, %22item-hash-1%22, Instant.now());%0A        Entry entryDangling = new Entry(106, %22item-hash-2%22, Instant.now());%0A%0A        when(backingStoreDriver.getItemBySha256(anyString()))%0A                .thenReturn(Optional.of(item))%0A                .thenReturn(Optional.empty());%0A%0A        RegisterMetadata registerMetadata = mock(RegisterMetadata.class);%0A        when(registerMetadata.getRegisterName()).thenReturn(%22country%22);%0A        when(registerData.getRegister()).thenReturn(registerMetadata);%0A%0A        PostgresRegister register = new PostgresRegister(registerData, backingStoreDriver, itemValidator);%0A%0A        try %7B%0A            register.appendEntry(entryNotDangling);%0A            register.appendEntry(entryDangling);%0A        %7D catch (NoSuchItemForEntryException ex) %7B%0A        %7D%0A%0A        verify(backingStoreDriver, times(1)).insertRecord(any(), eq(%22country%22));%0A    %7D%0A%7D%0A

Add tests for append-entry in PostgresRegister