<commit_before>package uk.gov.register.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.IteratorUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import uk.gov.register.core.Entry;
import uk.gov.register.core.Item;
import uk.gov.register.core.Register;
import uk.gov.register.serialization.*;
import uk.gov.register.util.CanonicalJsonMapper;
import uk.gov.register.views.RegisterProof;

import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;


@RunWith(MockitoJUnitRunner.class)
public class RegisterSerialisationFormatServiceTest {

    private CanonicalJsonMapper canonicalJsonMapper = new CanonicalJsonMapper();

    @Mock
    private RegisterService registerService;

    @Mock
    private Register register;

    private Iterator<RegisterCommand> commands;
    private Item item;
    private Entry entry1;
    private Entry entry2;
    private RegisterProof emptyRegisterProof;

    private RegisterCommand addItemCommand;
    private RegisterCommand appendEntryCommand1;
    private RegisterCommand appendEntryCommand2;
    private RegisterCommand assertRootEmptyRegister;

    private RegisterSerialisationFormatService sutService;
    private RegisterSerialisationFormat rsf;

    @Before
    public void setUp() throws Exception {
        JsonNode content = new ObjectMapper().readTree("{\"address\":\"9AQZJ3M\",\"name\":\"ST LAWRENCE CHURCH\"}");

        item = new Item(content);
        entry1 = new Entry(0, getHash(content), Instant.now());
        entry2 = new Entry(1, getHash(content), Instant.now().plusMillis(100));
        emptyRegisterProof = new RegisterProof("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");

        addItemCommand = new AddItemCommand(item);
        appendEntryCommand1 = new AppendEntryCommand(entry1);
        appendEntryCommand2 = new AppendEntryCommand(entry2);
        assertRootEmptyRegister = new AssertRootHashCommand(emptyRegisterProof);


        commands = Arrays.asList(addItemCommand, appendEntryCommand1, appendEntryCommand2).iterator();

        rsf = new RegisterSerialisationFormat(commands);
        sutService = new RegisterSerialisationFormatService(registerService, register);
    }

    @Test
    public void processRegisterComponents() throws Exception {

        when(register.getTotalEntries()).thenReturn(0);

        doAnswer(new Answer<Void>() {
            public Void answer(InvocationOnMock invocation) {
                Consumer<Register> callback = (Consumer<Register>) invocation.getArguments()[0];
                callback.accept(register);
                return null;
            }
        }).when(registerService).asAtomicRegisterOperation(any(Consumer.class));

        sutService.processRegisterComponents(rsf);

        verify(register).putItem(item);
        verify(register).appendEntry(entry1);
    }

    @Test
    public void createRegisterSerialisationFormat_returnsRSFFromEntireRegister() throws NoSuchAlgorithmException {
        when(register.getItemIterator()).thenReturn(Arrays.asList(item).iterator());
        when(register.getEntryIterator()).thenReturn(Arrays.asList(entry1, entry2).iterator());

        RegisterProof expectedRegisterProof = new RegisterProof("1231234");
        when(register.getRegisterProof()).thenReturn(expectedRegisterProof);


        RegisterSerialisationFormat actualRSF = sutService.createRegisterSerialisationFormat();
        List<RegisterCommand> actualCommands = IteratorUtils.toList(actualRSF.getCommands());

        verify(register, times(1)).getItemIterator();
        verify(register, times(1)).getEntryIterator();
        verify(register, times(1)).getRegisterProof();

        AssertRootHashCommand expectedRootHashCommand = new AssertRootHashCommand(expectedRegisterProof);

        assertThat(actualCommands.size(), equalTo(5));
        assertThat(actualCommands, contains(
                assertRootEmptyRegister,
                addItemCommand,
                appendEntryCommand1,
                appendEntryCommand2,
                expectedRootHashCommand));<commit_msg>Update tests to check RSF execution of assert root hash command<commit_after>package uk.gov.register.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.IteratorUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import uk.gov.register.core.Entry;
import uk.gov.register.core.Item;
import uk.gov.register.core.Register;
import uk.gov.register.serialization.*;
import uk.gov.register.util.CanonicalJsonMapper;
import uk.gov.register.views.RegisterProof;

import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;


@RunWith(MockitoJUnitRunner.class)
public class RegisterSerialisationFormatServiceTest {

    private CanonicalJsonMapper canonicalJsonMapper = new CanonicalJsonMapper();

    @Mock
    private RegisterService registerService;

    @Mock
    private Register register;

    private Item item;
    private Entry entry1;
    private Entry entry2;
    private RegisterProof emptyRegisterProof;

    private RegisterCommand addItemCommand;
    private RegisterCommand appendEntryCommand1;
    private RegisterCommand appendEntryCommand2;
    private RegisterCommand assertRootEmptyRegister;

    private RegisterSerialisationFormatService sutService;

    @Before
    public void setUp() throws Exception {
        JsonNode content = new ObjectMapper().readTree("{\"address\":\"9AQZJ3M\",\"name\":\"ST LAWRENCE CHURCH\"}");

        item = new Item(content);
        entry1 = new Entry(0, getHash(content), Instant.now());
        entry2 = new Entry(1, getHash(content), Instant.now().plusMillis(100));
        emptyRegisterProof = new RegisterProof("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");

        addItemCommand = new AddItemCommand(item);
        appendEntryCommand1 = new AppendEntryCommand(entry1);
        appendEntryCommand2 = new AppendEntryCommand(entry2);
        assertRootEmptyRegister = new AssertRootHashCommand(emptyRegisterProof);

        sutService = new RegisterSerialisationFormatService(registerService, register);
    }

    @Test
    public void processRegisterComponents() throws Exception {
        when(register.getTotalEntries()).thenReturn(0);
        when(register.getRegisterProof()).thenReturn(emptyRegisterProof);

        doAnswer(new Answer<Void>() {
            public Void answer(InvocationOnMock invocation) {
                Consumer<Register> callback = (Consumer<Register>) invocation.getArguments()[0];
                callback.accept(register);
                return null;
            }
        }).when(registerService).asAtomicRegisterOperation(any(Consumer.class));

        RegisterSerialisationFormat rsf = new RegisterSerialisationFormat(Arrays.asList(
                assertRootEmptyRegister,
                addItemCommand,
                appendEntryCommand1,
                appendEntryCommand2).iterator());

        sutService.processRegisterComponents(rsf);

        verify(register).getRegisterProof();
        verify(register).putItem(item);
        verify(register).appendEntry(entry1);
        verify(register).appendEntry(entry2);
    }

    @Test
    public void createRegisterSerialisationFormat_returnsRSFFromEntireRegister() throws NoSuchAlgorithmException {
        when(register.getItemIterator()).thenReturn(Arrays.asList(item).iterator());
        when(register.getEntryIterator()).thenReturn(Arrays.asList(entry1, entry2).iterator());

        RegisterProof expectedRegisterProof = new RegisterProof("1231234");
        when(register.getRegisterProof()).thenReturn(expectedRegisterProof);


        RegisterSerialisationFormat actualRSF = sutService.createRegisterSerialisationFormat();
        List<RegisterCommand> actualCommands = IteratorUtils.toList(actualRSF.getCommands());

        verify(register, times(1)).getItemIterator();
        verify(register, times(1)).getEntryIterator();
        verify(register, times(1)).getRegisterProof();

        AssertRootHashCommand expectedRootHashCommand = new AssertRootHashCommand(expectedRegisterProof);

        assertThat(actualCommands.size(), equalTo(5));
        assertThat(actualCommands, contains(
                assertRootEmptyRegister,
                addItemCommand,
                appendEntryCommand1,
                appendEntryCommand2,
                expectedRootHashCommand));
{'fraction_non_alphanumeric': 0.09272689598010775, 'fraction_numerical': 0.01688769167012018, 'mean_word_length': 4.123673036093418, 'pattern_counts': {'":': 4, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5714605', 'n_tokens_mistral': 2622, 'n_tokens_neox': 2480, 'n_words': 459}