package com.coding4people.mosquitoreport.api.repositories;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.Test;
import org.mockito.Mock;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import com.coding4people.mosquitoreport.api.WithService;

public class RepositoryTest extends WithService {
    @Mock
    AmazonDynamoDB client;

    @Mock
    DynamoDBMapper mapper;

    @Mock
    DynamoDB dynamoDB;
    
    @Mock
    Table table;

    @Test
    public void testSave() {
        CreateTableRequest request = new CreateTableRequest();
        
        when(mapper.generateCreateTableRequest(Model.class)).thenReturn(request);
        when(dynamoDB.createTable(request)).thenReturn(table);
        
        ModelRepository repository = getService(ModelRepository.class);
        
        Model model = new Model();
        
        repository.save(model);
        
        verify(mapper).save(model);
    }
    
    public static class Model {}
    
    public static class ModelRepository extends Repository<Model> {
        @Override
        protected Class<Model> getType() {
            return Model.class;
        }
    }
}

@@ -53,16 +53,61 @@
 ories;%0A%0A
+import static org.junit.Assert.assertEquals;%0A
 import s
@@ -181,16 +181,55 @@
 .when;%0A%0A
+import javax.ws.rs.NotFoundException;%0A%0A
 import o
@@ -797,20 +797,16 @@
 namoDB;%0A
-    
 %0A    @Mo
@@ -924,32 +924,24 @@
 eRequest();%0A
-        
 %0A        whe
@@ -1074,24 +1074,16 @@
 table);%0A
-        
 %0A       
@@ -1147,24 +1147,16 @@
 class);%0A
-        
 %0A       
@@ -1183,24 +1183,16 @@
 odel();%0A
-        
 %0A       
@@ -1216,24 +1216,16 @@
 model);%0A
-        
 %0A       
@@ -1259,20 +1259,26 @@
 ;%0A    %7D%0A
+%0A
     
+@Test
 %0A    pub
@@ -1285,34 +1285,1877 @@
 lic 
-static class Model %7B%7D%0A    
+void testLoadByHash() %7B%0A        CreateTableRequest request = new CreateTableRequest();%0A%0A        when(mapper.generateCreateTableRequest(Model.class)).thenReturn(request);%0A        when(dynamoDB.createTable(request)).thenReturn(table);%0A%0A        ModelRepository repository = getService(ModelRepository.class);%0A%0A        repository.load(%22hash%22);%0A%0A        verify(mapper).load(Model.class, %22hash%22);%0A    %7D%0A%0A    @Test%0A    public void testLoadByHashAndRange() %7B%0A        CreateTableRequest request = new CreateTableRequest();%0A%0A        when(mapper.generateCreateTableRequest(Model.class)).thenReturn(request);%0A        when(dynamoDB.createTable(request)).thenReturn(table);%0A%0A        ModelRepository repository = getService(ModelRepository.class);%0A%0A        repository.load(%22hash%22, %22range%22);%0A%0A        verify(mapper).load(Model.class, %22hash%22, %22range%22);%0A    %7D%0A%0A    @Test(expected = NotFoundException.class)%0A    public void testLoadOrNotFound() %7B%0A        CreateTableRequest request = new CreateTableRequest();%0A%0A        when(mapper.generateCreateTableRequest(Model.class)).thenReturn(request);%0A        when(dynamoDB.createTable(request)).thenReturn(table);%0A%0A        ModelRepository repository = getService(ModelRepository.class);%0A%0A        repository.loadOrNotFound(%22hash%22);%0A    %7D%0A    %0A    @Test%0A    public void testLoadOrNotFound200() %7B%0A        CreateTableRequest request = new CreateTableRequest();%0A        Model model = new Model();%0A%0A        when(mapper.generateCreateTableRequest(Model.class)).thenReturn(request);%0A        when(dynamoDB.createTable(request)).thenReturn(table);%0A        when(mapper.load(Model.class, %22hash%22)).thenReturn(model);%0A%0A        ModelRepository repository = getService(ModelRepository.class);%0A%0A        assertEquals(model, repository.loadOrNotFound(%22hash%22));%0A        %0A        verify(mapper).load(Model.class, %22hash%22);%0A    %7D%0A%0A    public static class Model %7B%0A    %7D%0A
 %0A   

Test repository loaders