// Path: src/main/java/com/coding4people/mosquitoreport/api/Env.java
// public class Env {
//     public final static Env instance = new Env();
//     
//     private Map<String, String> properties = Maps.newHashMap();
//     
//     public Optional<String> get(String key) {
//         return Optional.ofNullable(properties.getOrDefault(key, System.getenv(key)));
//     }
//     
//     public Env register(String key, String value) {
//         properties.put(key, value);
//         
//         return this;
//     }
// }
// 
// Path: src/test/java/com/coding4people/mosquitoreport/api/WithService.java
// abstract public class WithService implements BaseTest {
//     @Before
//     public void initialize() {
//         MockitoAnnotations.initMocks(this);
//     }
// 
//     protected ResourceConfig configure() {
//         return new Config().configureFramework().register(new MockBinder(this));
//     }
//     
//     protected <T> T getService(Class<T> clazz) {
//         return new ApplicationHandler(configure().register(new GenericBinder<T>() {
//             public Class<T> getType() {
//                 return clazz;
//             }
//         })).getServiceLocator().getService(clazz);
//     }
// }
// 
// Path: src/main/java/com/coding4people/mosquitoreport/api/models/Searchable.java
// public interface Searchable {
//     public String getSearchId();
// }


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import javax.inject.Inject;
import org.glassfish.hk2.api.MultiException;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import com.amazonaws.services.cloudsearchdomain.AmazonCloudSearchDomain;
import com.amazonaws.services.cloudsearchdomain.AmazonCloudSearchDomainClient;
import com.amazonaws.services.cloudsearchdomain.model.Hit;
import com.amazonaws.services.cloudsearchdomain.model.Hits;
import com.amazonaws.services.cloudsearchdomain.model.SearchRequest;
import com.amazonaws.services.cloudsearchdomain.model.SearchResult;
import com.amazonaws.services.cloudsearchdomain.model.UploadDocumentsRequest;
import com.amazonaws.services.cloudsearchv2.AmazonCloudSearch;
import com.amazonaws.services.cloudsearchv2.model.DescribeDomainsResult;
import com.amazonaws.services.cloudsearchv2.model.DomainStatus;
import com.amazonaws.services.cloudsearchv2.model.ServiceEndpoint;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import com.coding4people.mosquitoreport.api.Env;
import com.coding4people.mosquitoreport.api.WithService;
import com.coding4people.mosquitoreport.api.models.Searchable;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jersey.repackaged.com.google.common.collect.Lists;
import jersey.repackaged.com.google.common.collect.Maps;
    @Test
    public void testIndexJsonProcessingException() throws Exception {
        when(amazonCloudSearch.describeDomains(any())).thenReturn(new DescribeDomainsResult()
                .withDomainStatusList(Lists.newArrayList(new DomainStatus().withSearchService(new ServiceEndpoint().withEndpoint("http://localhost")))));
        
        Model model = new Model();
        model.setGuid("00000000-0000-0000-0000-000000000000");
        
        ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class);
        
        getService(ModelIndexer.class).index(Lists.newArrayList(model));
        
        verify(executor).submit(runnableCaptor.capture());
        
        when(objectMapper.writeValueAsString(any())).thenThrow(new StubJsonProcessingException());
        
        runnableCaptor.getValue().run();
        
        verify(domain, never()).uploadDocuments(any());
    }
    
    public static class StubJsonProcessingException extends JsonProcessingException {
        private static final long serialVersionUID = -3023763421872884564L;

        public StubJsonProcessingException() {
            super("");
        }
    }

    @DynamoDBTable(tableName = "model")
// Path: src/main/java/com/coding4people/mosquitoreport/api/Env.java
// public class Env {
//     public final static Env instance = new Env();
//     
//     private Map<String, String> properties = Maps.newHashMap();
//     
//     public Optional<String> get(String key) {
//         return Optional.ofNullable(properties.getOrDefault(key, System.getenv(key)));
//     }
//     
//     public Env register(String key, String value) {
//         properties.put(key, value);
//         
//         return this;
//     }
// }
// 
// Path: src/test/java/com/coding4people/mosquitoreport/api/WithService.java
// abstract public class WithService implements BaseTest {
//     @Before
//     public void initialize() {
//         MockitoAnnotations.initMocks(this);
//     }
// 
//     protected ResourceConfig configure() {
//         return new Config().configureFramework().register(new MockBinder(this));
//     }
//     
//     protected <T> T getService(Class<T> clazz) {
//         return new ApplicationHandler(configure().register(new GenericBinder<T>() {
//             public Class<T> getType() {
//                 return clazz;
//             }
//         })).getServiceLocator().getService(clazz);
//     }
// }
// 
// Path: src/main/java/com/coding4people/mosquitoreport/api/models/Searchable.java
// public interface Searchable {
//     public String getSearchId();
// }


// Path: src/test/java/com/coding4people/mosquitoreport/api/indexers/IndexerTest.java
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import javax.inject.Inject;
import org.glassfish.hk2.api.MultiException;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import com.amazonaws.services.cloudsearchdomain.AmazonCloudSearchDomain;
import com.amazonaws.services.cloudsearchdomain.AmazonCloudSearchDomainClient;
import com.amazonaws.services.cloudsearchdomain.model.Hit;
import com.amazonaws.services.cloudsearchdomain.model.Hits;
import com.amazonaws.services.cloudsearchdomain.model.SearchRequest;
import com.amazonaws.services.cloudsearchdomain.model.SearchResult;
import com.amazonaws.services.cloudsearchdomain.model.UploadDocumentsRequest;
import com.amazonaws.services.cloudsearchv2.AmazonCloudSearch;
import com.amazonaws.services.cloudsearchv2.model.DescribeDomainsResult;
import com.amazonaws.services.cloudsearchv2.model.DomainStatus;
import com.amazonaws.services.cloudsearchv2.model.ServiceEndpoint;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import com.coding4people.mosquitoreport.api.Env;
import com.coding4people.mosquitoreport.api.WithService;
import com.coding4people.mosquitoreport.api.models.Searchable;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jersey.repackaged.com.google.common.collect.Lists;
import jersey.repackaged.com.google.common.collect.Maps;

    @Test
    public void testIndexJsonProcessingException() throws Exception {
        when(amazonCloudSearch.describeDomains(any())).thenReturn(new DescribeDomainsResult()
                .withDomainStatusList(Lists.newArrayList(new DomainStatus().withSearchService(new ServiceEndpoint().withEndpoint("http://localhost")))));
        
        Model model = new Model();
        model.setGuid("00000000-0000-0000-0000-000000000000");
        
        ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class);
        
        getService(ModelIndexer.class).index(Lists.newArrayList(model));
        
        verify(executor).submit(runnableCaptor.capture());
        
        when(objectMapper.writeValueAsString(any())).thenThrow(new StubJsonProcessingException());
        
        runnableCaptor.getValue().run();
        
        verify(domain, never()).uploadDocuments(any());
    }
    
    public static class StubJsonProcessingException extends JsonProcessingException {
        private static final long serialVersionUID = -3023763421872884564L;

        public StubJsonProcessingException() {
            super("");
        }
    }

    @DynamoDBTable(tableName = "model")
    public static class Model implements Searchable {