/*
 * Copyright 2016 Benedikt Ritter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.britter.springbootherokudemo;

import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.List;

import de.bechte.junit.runners.context.HierarchicalContextRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.ui.ModelMap;

@RunWith(HierarchicalContextRunner.class)
public class HomeControllerTest {

    private ModelMap map;
    private HomeController ctrl;
    private RecordRepository repository;

    @Before
    public void setUp() throws Exception {
        map = new ModelMap();
        repository = mock(RecordRepository.class);
        ctrl = new HomeController(repository);
    }

    public class Home {

        @Test
        public void shouldAddInsertRecordToModelMap() throws Exception {
            ctrl.home(map);

            assertThat(map, hasKey("insertRecord"));
            assertTrue(map.get("insertRecord") instanceof Record);
        }

        @Test
        public void shouldQueryRepositoryForAllRecords() throws Exception {
            ctrl.home(map);

            verify(repository, only()).findAll();
        }

        @Test
        public void shouldAddRecordsFromRepositoryToModelMap() throws Exception {
            when(repository.findAll()).thenReturn(Arrays.asList(new Record(), new Record(), new Record()));

            ctrl.home(map);

            assertThat(map, hasKey("records"));
            assertTrue(map.get("records") instanceof List);

            List<Record> records = getRecords();
            assertThat(records, hasSize(3));
        }

        @SuppressWarnings("unchecked")
        private List<Record> getRecords() {
            return (List<Record>) map.get("records");
        }
    }

}

@@ -731,16 +731,97 @@
 asSize;%0A
+import static org.hamcrest.Matchers.is;%0Aimport static org.hamcrest.Matchers.not;%0A
 import s
@@ -898,16 +898,56 @@
 rtTrue;%0A
+import static org.mockito.Matchers.any;%0A
 import s
@@ -1012,20 +1012,102 @@
 Mockito.
-only
+never;%0Aimport static org.mockito.Mockito.only;%0Aimport static org.mockito.Mockito.times
 ;%0Aimport
@@ -1208,16 +1208,42 @@
 Arrays;%0A
+import java.util.HashMap;%0A
 import j
@@ -1444,16 +1444,123 @@
 odelMap;
+%0Aimport org.springframework.validation.MapBindingResult;%0Aimport org.springframework.validation.ObjectError;
 %0A%0A@RunWi
@@ -2975,10 +2975,1347 @@
 %0A    %7D%0A%0A
+    public class InsertData %7B%0A%0A        private MapBindingResult bindingResult;%0A%0A        @Before%0A        public void setUp() throws Exception %7B%0A            bindingResult = new MapBindingResult(new HashMap%3C%3E(), %22insertRecord%22);%0A        %7D%0A%0A        @Test%0A        public void shouldSaveRecordWhenThereAreNoErrors() throws Exception %7B%0A            Record record = new Record();%0A            insertData(record);%0A%0A            verify(repository, times(1)).save(record);%0A        %7D%0A%0A        @Test%0A        public void shouldNotSaveRecordWhenThereAreErrors() throws Exception %7B%0A            bindingResult.addError(new ObjectError(%22%22, %22%22));%0A%0A            insertData(new Record());%0A%0A            verify(repository, never()).save(any(Record.class));%0A        %7D%0A%0A        @Test%0A        public void shouldAddNewInsertRecordToModelMap() throws Exception %7B%0A            Record record = new Record();%0A            insertData(record);%0A%0A            assertThat(map, hasKey(%22insertRecord%22));%0A            assertThat(map.get(%22insertRecord%22), is(not(record)));%0A        %7D%0A%0A        @Test%0A        public void shouldAddRecordsToModelMap() throws Exception %7B%0A            insertData(new Record());%0A%0A            assertThat(map, hasKey(%22records%22));%0A        %7D%0A%0A        private void insertData(Record record) %7B%0A            ctrl.insertData(map, record, bindingResult);%0A        %7D%0A    %7D%0A
 %7D%0A

Add test coverage for insertData method