Coverage Report - org.codeforamerica.open311.facade.data.ServiceDiscoveryInfo
 
Classes in this File Line Coverage Branch Coverage Complexity
ServiceDiscoveryInfo
90%
19/21
70%
7/10
2
 
 1  
 package org.codeforamerica.open311.facade.data;
 2  
 
 3  
 import java.io.Serializable;
 4  
 import java.util.Date;
 5  
 import java.util.LinkedList;
 6  
 import java.util.List;
 7  
 
 8  
 import org.codeforamerica.open311.facade.EndpointType;
 9  
 
 10  
 /**
 11  
  * Contains information regarding to a city (endpoints, allowed formats...).
 12  
  * 
 13  
  * @author Santiago MunĂ­n <santimunin@gmail.com>
 14  
  * 
 15  
  */
 16  
 public class ServiceDiscoveryInfo implements Serializable {
 17  
 
 18  
         private static final long serialVersionUID = 5804902138488110319L;
 19  
         private final static String GEO_REPORT_V2_SPECIFICATION_URL = "http://wiki.open311.org/GeoReport_v2";
 20  
         private Date changeset;
 21  
         private String contact;
 22  
         private String keyService;
 23  
         List<Endpoint> endpoints;
 24  
 
 25  
         public ServiceDiscoveryInfo(Date changeset, String contact,
 26  
                         String keyService, List<Endpoint> endpoints) {
 27  6
                 super();
 28  6
                 this.changeset = changeset;
 29  6
                 this.contact = contact;
 30  6
                 this.keyService = keyService;
 31  6
                 this.endpoints = endpoints;
 32  6
         }
 33  
 
 34  
         public Date getChangeset() {
 35  1
                 return changeset;
 36  
         }
 37  
 
 38  
         public String getContact() {
 39  1
                 return contact;
 40  
         }
 41  
 
 42  
         public String getKeyService() {
 43  1
                 return keyService;
 44  
         }
 45  
 
 46  
         public List<Endpoint> getEndpoints() {
 47  1
                 return endpoints;
 48  
         }
 49  
 
 50  
         /**
 51  
          * Searches through the list of endpoints and tries to find a valid endpoint
 52  
          * of the given type. Tries to get the GeoReport latest version (currently
 53  
          * v2).
 54  
          * 
 55  
          * @param endpointType
 56  
          *            Type of the desired endpoint.
 57  
          * @return <code>null</code> if a suitable endpoint couldn't be found.
 58  
          */
 59  
         public Endpoint getMoreSuitableEndpoint(EndpointType endpointType) {
 60  6
                 List<Endpoint> typeFilteredEndpoints = new LinkedList<Endpoint>();
 61  6
                 for (Endpoint endpoint : endpoints) {
 62  24
                         if (endpoint.getType() == endpointType) {
 63  12
                                 typeFilteredEndpoints.add(endpoint);
 64  
                         }
 65  
                 }
 66  6
                 Endpoint candidate = null;
 67  6
                 for (Endpoint endpoint : typeFilteredEndpoints) {
 68  
                         // Some endpoints don't follows the specification so it does this
 69  
                         // "double check", which has been proved working.
 70  6
                         if (endpoint.getSpecificationUrl().equals(GEO_REPORT_V2_SPECIFICATION_URL)) {
 71  6
                                 if (endpoint.getUrl().contains("v2")) {
 72  6
                                         return endpoint;
 73  
                                 }
 74  0
                                 candidate = endpoint;
 75  
                         }
 76  
                 }
 77  0
                 return candidate;
 78  
         }
 79  
 }