package uk.ac.ebi.quickgo.ontology.metadata;

import uk.ac.ebi.quickgo.common.loader.GZIPFiles;
import uk.ac.ebi.quickgo.rest.metadata.MetaData;

import java.nio.file.Path;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * Populate a MetaData instance with information about the data provided by this service
 *
 * @author Tony Wardell
 * Date: 07/03/2017
 * Time: 10:51
 * Created with IntelliJ IDEA.
 */
public class MetaDataProvider {
    private final Path ontologyPath;

    public MetaDataProvider(Path ontologyPath) {
        this.ontologyPath = ontologyPath;
    }

    public MetaData lookupMetaData() {
        List<MetaData> goLines = GZIPFiles.lines(ontologyPath)
                                          .skip(1)
                                          .filter(s -> s.startsWith("GO"))
                                          .limit(1)
                                          .map(s -> {
                                              String[] a = s.split("\t");
                                              return new MetaData(a[2], a[1]);
                                          })
                                          .collect(toList());
        return (goLines.size() == 1) ? goLines.get(0) : null;
    }
}

@@ -134,24 +134,131 @@
 a.MetaData;%0A
+import uk.ac.ebi.quickgo.rest.service.ServiceConfigException;%0A%0Aimport com.google.common.base.Preconditions;
 %0Aimport java
@@ -669,48 +669,296 @@
     
-this.ontologyPath = ontologyPath;%0A    %7D%0A
+Preconditions.checkArgument(ontologyPath != null, %22The path to the source of the Ontology metadata cannot %22 +%0A                %22be null.%22);%0A        this.ontologyPath = ontologyPath;%0A    %7D%0A%0A    /**%0A     * Lookup the MetaData for the Ontology service%0A     * @return MetaData instance%0A     */
 %0A   
@@ -989,24 +989,42 @@
 etaData() %7B%0A
+        try %7B%0A    
         List
@@ -1116,16 +1116,20 @@
         
+   
+ 
 .skip(1)
@@ -1125,24 +1125,28 @@
    .skip(1)%0A
+    
             
@@ -1216,56 +1216,8 @@
     
-                                      .limit(1)%0A
     
@@ -1312,16 +1312,20 @@
         
+    
 String%5B%5D
@@ -1340,24 +1340,28 @@
 plit(%22%5Ct%22);%0A
+    
             
@@ -1473,11 +1473,19 @@
     
-%7D)%0A
+    %7D)%0A    
     
@@ -1554,61 +1554,278 @@
     
+  
-return (goLines.size() == 1) ? goLines.get(0) : null;
+  Preconditions.checkState(goLines.size() == 1, %22Unable to read the correct number of lines for Ontology %22 +%0A                    %22metadata%22);%0A            return goLines.get(0);%0A        %7D catch (Exception e) %7B%0A            throw new ServiceConfigException(e);%0A        %7D%0A
 %0A   

Add preconditions, cope with exception during file read.