// Path: breakerbox-turbine/src/main/java/com/yammer/breakerbox/turbine/MarathonInstanceDiscovery.java
// public class MarathonInstanceDiscovery implements InstanceDiscovery {
//     private static final Logger LOGGER = LoggerFactory.getLogger(MarathonInstanceDiscovery.class);
//     private final ObjectMapper mapper;
//     private  MarathonClient marathonClient;
//     private final List<MarathonClientConfiguration> marathonClientConfigurations;
//     private Map<MarathonClientConfiguration,Invocation.Builder> marathonClientConfigurationBuilderMap;
// 
//     public MarathonInstanceDiscovery(ObjectMapper mapper, List<MarathonClientConfiguration> marathonClientConfigurations) {
//         this.mapper = mapper;
//         this.marathonClientConfigurations = marathonClientConfigurations;
//          constructMarathonClientConfigurationBuilderMap();
//     }
// 
//     private void constructMarathonClientConfigurationBuilderMap() {
//         marathonClientConfigurationBuilderMap = new HashMap<>();
//         marathonClientConfigurations.parallelStream().forEach(marathonClientConfiguration -> {
//             marathonClient = new MarathonClient(marathonClientConfiguration);
//             Invocation.Builder builder = marathonClient.getServiceInstanceDetails();
//             marathonClientConfigurationBuilderMap.put(marathonClientConfiguration,builder);
//         });
// 
//     }
// 
//     @Override
//     public Collection<Instance> getInstanceList() throws Exception {
//         List<Instance> instances = new ArrayList<>();
//         marathonClientConfigurationBuilderMap.entrySet().parallelStream().forEach(entry -> {
//             Response response = null;
//             try {
//                 response = entry.getValue().get();
//                 if (response.getStatus() == HttpStatus.SC_OK) {
//                    instances.addAll(createServiceInstanceList(response.readEntity(String.class), entry.getKey()));
//                 }
//             } finally {
//                 if (response != null) {
//                     response.close();
//                 }
//             }
//         });
//         return instances;
//     }
// 
//      public List<Instance> createServiceInstanceList(String marathonApiResponse,MarathonClientConfiguration marathonClientConfiguration) {
//          MarathonClientResponse marathonClientResponse = null;
//          try {
//              marathonClientResponse = mapper.readValue(marathonApiResponse, MarathonClientResponse.class);
//          } catch (IOException e) {
//              LOGGER.error("io exception",e);
//          }
//          if (marathonClientResponse != null && marathonClientResponse.getApp() != null) {
//             List<PortMapping> portMappingList = marathonClientResponse.getApp().getContainer().getDocker().getPortMappings();
//             int portIndex = -1;
//             for (int i = 0; i < portMappingList.size(); i++) {
//                 if (portMappingList.get(i).getContainerPort().equals(marathonClientConfiguration.getMarathonAppPort())) {
//                     portIndex = i;
//                     break;
//                 }
//             }
//             if (portIndex < 0) {
//                 LOGGER.error("marathon app port non present in port mapping");
//                 return Collections.emptyList();
//             }
// 
//             List<Task> tasks = marathonClientResponse.getApp().getTasks();
//             int finalPortIndex = portIndex;
//             return tasks.stream().map(task -> new Instance(task.getHost() + ":" + task.getPorts().get(finalPortIndex), marathonClientConfiguration.getCluster(), true)).collect(Collectors.toList());
//         } else {
//             LOGGER.error("tasks not available for the given namespace");
//             return Collections.emptyList();
//         }
//     }
// }
// 
// Path: breakerbox-turbine/src/main/java/com/yammer/breakerbox/turbine/client/MarathonClient.java
// public class MarathonClient {
// 
//     private MarathonClientConfiguration marathonClientConfiguration;
// 
//     public MarathonClient(MarathonClientConfiguration marathonClientConfiguration){
//         this.marathonClientConfiguration = marathonClientConfiguration;
//     }
// 
//      public Invocation.Builder getServiceInstanceDetails() {
//             Client client = JerseyClientBuilder.newClient();
//             WebTarget target= client.target(marathonClientConfiguration.getMarathonApiUrl() + "/v2/apps"+ marathonClientConfiguration.getMarathonAppNameSpace());
//             return target.request();
//     }
// 
// }
// 
// Path: breakerbox-turbine/src/main/java/com/yammer/breakerbox/turbine/config/MarathonClientConfiguration.java
// public class MarathonClientConfiguration {
//     @NotEmpty
//     private String marathonApiUrl;
//     @NotEmpty
//     private String marathonAppNameSpace;
//     @NotEmpty
//     private Integer marathonAppPort;
//     // this is the custom clustername that you can have for the services cluster
//     @NotEmpty
//     private String cluster;
// 
//     public String getCluster() { return cluster; }
// 
//     public String getMarathonApiUrl() {
//         return marathonApiUrl;
//     }
// 
//     public String getMarathonAppNameSpace() {
//         return marathonAppNameSpace;
//     }
// 
//     public Integer getMarathonAppPort() {
//         return marathonAppPort;
//     }
// 
// 
//     public void setMarathonApiUrl(String marathonApiUrl) {
//         this.marathonApiUrl = marathonApiUrl;
//     }
// 
//     public void setMarathonAppNameSpace(String marathonAppNameSpace) {
//         this.marathonAppNameSpace = marathonAppNameSpace;
//     }
// 
//     public void setMarathonAppPort(Integer marathonAppPort) {
//         this.marathonAppPort = marathonAppPort;
//     }
// 
//     public void setCluster(String cluster) {
//         this.cluster = cluster;
//     }
// }


import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.turbine.discovery.Instance;
import com.yammer.breakerbox.turbine.MarathonInstanceDiscovery;
import com.yammer.breakerbox.turbine.client.MarathonClient;
import com.yammer.breakerbox.turbine.config.MarathonClientConfiguration;
import io.dropwizard.jackson.Jackson;
import io.dropwizard.testing.FixtureHelpers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import static org.mockito.Mockito.mock;
package com.yammer.breakerbox.turbine.tests;




/**
 * Created by supreeth.vp on 24/05/17.
 */
public class MarathonInstanceDiscoveryTest {
    private static final ObjectMapper MAPPER = Jackson.newObjectMapper();
    private MarathonClient marathonClient = mock(MarathonClient.class);
    private MarathonClientConfiguration marathonClientConfiguration;
// Path: breakerbox-turbine/src/main/java/com/yammer/breakerbox/turbine/MarathonInstanceDiscovery.java
// public class MarathonInstanceDiscovery implements InstanceDiscovery {
//     private static final Logger LOGGER = LoggerFactory.getLogger(MarathonInstanceDiscovery.class);
//     private final ObjectMapper mapper;
//     private  MarathonClient marathonClient;
//     private final List<MarathonClientConfiguration> marathonClientConfigurations;
//     private Map<MarathonClientConfiguration,Invocation.Builder> marathonClientConfigurationBuilderMap;
// 
//     public MarathonInstanceDiscovery(ObjectMapper mapper, List<MarathonClientConfiguration> marathonClientConfigurations) {
//         this.mapper = mapper;
//         this.marathonClientConfigurations = marathonClientConfigurations;
//          constructMarathonClientConfigurationBuilderMap();
//     }
// 
//     private void constructMarathonClientConfigurationBuilderMap() {
//         marathonClientConfigurationBuilderMap = new HashMap<>();
//         marathonClientConfigurations.parallelStream().forEach(marathonClientConfiguration -> {
//             marathonClient = new MarathonClient(marathonClientConfiguration);
//             Invocation.Builder builder = marathonClient.getServiceInstanceDetails();
//             marathonClientConfigurationBuilderMap.put(marathonClientConfiguration,builder);
//         });
// 
//     }
// 
//     @Override
//     public Collection<Instance> getInstanceList() throws Exception {
//         List<Instance> instances = new ArrayList<>();
//         marathonClientConfigurationBuilderMap.entrySet().parallelStream().forEach(entry -> {
//             Response response = null;
//             try {
//                 response = entry.getValue().get();
//                 if (response.getStatus() == HttpStatus.SC_OK) {
//                    instances.addAll(createServiceInstanceList(response.readEntity(String.class), entry.getKey()));
//                 }
//             } finally {
//                 if (response != null) {
//                     response.close();
//                 }
//             }
//         });
//         return instances;
//     }
// 
//      public List<Instance> createServiceInstanceList(String marathonApiResponse,MarathonClientConfiguration marathonClientConfiguration) {
//          MarathonClientResponse marathonClientResponse = null;
//          try {
//              marathonClientResponse = mapper.readValue(marathonApiResponse, MarathonClientResponse.class);
//          } catch (IOException e) {
//              LOGGER.error("io exception",e);
//          }
//          if (marathonClientResponse != null && marathonClientResponse.getApp() != null) {
//             List<PortMapping> portMappingList = marathonClientResponse.getApp().getContainer().getDocker().getPortMappings();
//             int portIndex = -1;
//             for (int i = 0; i < portMappingList.size(); i++) {
//                 if (portMappingList.get(i).getContainerPort().equals(marathonClientConfiguration.getMarathonAppPort())) {
//                     portIndex = i;
//                     break;
//                 }
//             }
//             if (portIndex < 0) {
//                 LOGGER.error("marathon app port non present in port mapping");
//                 return Collections.emptyList();
//             }
// 
//             List<Task> tasks = marathonClientResponse.getApp().getTasks();
//             int finalPortIndex = portIndex;
//             return tasks.stream().map(task -> new Instance(task.getHost() + ":" + task.getPorts().get(finalPortIndex), marathonClientConfiguration.getCluster(), true)).collect(Collectors.toList());
//         } else {
//             LOGGER.error("tasks not available for the given namespace");
//             return Collections.emptyList();
//         }
//     }
// }
// 
// Path: breakerbox-turbine/src/main/java/com/yammer/breakerbox/turbine/client/MarathonClient.java
// public class MarathonClient {
// 
//     private MarathonClientConfiguration marathonClientConfiguration;
// 
//     public MarathonClient(MarathonClientConfiguration marathonClientConfiguration){
//         this.marathonClientConfiguration = marathonClientConfiguration;
//     }
// 
//      public Invocation.Builder getServiceInstanceDetails() {
//             Client client = JerseyClientBuilder.newClient();
//             WebTarget target= client.target(marathonClientConfiguration.getMarathonApiUrl() + "/v2/apps"+ marathonClientConfiguration.getMarathonAppNameSpace());
//             return target.request();
//     }
// 
// }
// 
// Path: breakerbox-turbine/src/main/java/com/yammer/breakerbox/turbine/config/MarathonClientConfiguration.java
// public class MarathonClientConfiguration {
//     @NotEmpty
//     private String marathonApiUrl;
//     @NotEmpty
//     private String marathonAppNameSpace;
//     @NotEmpty
//     private Integer marathonAppPort;
//     // this is the custom clustername that you can have for the services cluster
//     @NotEmpty
//     private String cluster;
// 
//     public String getCluster() { return cluster; }
// 
//     public String getMarathonApiUrl() {
//         return marathonApiUrl;
//     }
// 
//     public String getMarathonAppNameSpace() {
//         return marathonAppNameSpace;
//     }
// 
//     public Integer getMarathonAppPort() {
//         return marathonAppPort;
//     }
// 
// 
//     public void setMarathonApiUrl(String marathonApiUrl) {
//         this.marathonApiUrl = marathonApiUrl;
//     }
// 
//     public void setMarathonAppNameSpace(String marathonAppNameSpace) {
//         this.marathonAppNameSpace = marathonAppNameSpace;
//     }
// 
//     public void setMarathonAppPort(Integer marathonAppPort) {
//         this.marathonAppPort = marathonAppPort;
//     }
// 
//     public void setCluster(String cluster) {
//         this.cluster = cluster;
//     }
// }


// Path: breakerbox-turbine/src/test/java/com/yammer/breakerbox/turbine/tests/MarathonInstanceDiscoveryTest.java
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.turbine.discovery.Instance;
import com.yammer.breakerbox.turbine.MarathonInstanceDiscovery;
import com.yammer.breakerbox.turbine.client.MarathonClient;
import com.yammer.breakerbox.turbine.config.MarathonClientConfiguration;
import io.dropwizard.jackson.Jackson;
import io.dropwizard.testing.FixtureHelpers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import static org.mockito.Mockito.mock;

package com.yammer.breakerbox.turbine.tests;




/**
 * Created by supreeth.vp on 24/05/17.
 */
public class MarathonInstanceDiscoveryTest {
    private static final ObjectMapper MAPPER = Jackson.newObjectMapper();
    private MarathonClient marathonClient = mock(MarathonClient.class);
    private MarathonClientConfiguration marathonClientConfiguration;
    private MarathonInstanceDiscovery marathonInstanceDiscovery;