/*******************************************************************************
 * Copyright 2014, 2017 gwt-ol3
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package ol.format;

import com.google.gwt.core.client.JsonUtils;
import ol.Coordinate;
import ol.Feature;
import ol.FeatureOptions;
import ol.GwtOL3BaseTestCase;
import ol.geom.LineString;

/**
 *
 * @author Tino Desjardins
 *
 */
public class GeoJsonTest extends GwtOL3BaseTestCase {

	private GeoJson geoJsonFormat;
	
	
	
	@Override
	protected void gwtSetUp() throws Exception {
	    injectUrlAndTest(() -> geoJsonFormat = new GeoJson());
	}
	
	public void testFeatureToGeoJson() {

	    injectUrlAndTest(() -> {
            
            assertNotNull(geoJsonFormat);

            java.lang.Object geoJSON = geoJsonFormat.writeFeatureObject(createTestFeature(), null);
            assertNotNull(geoJSON);
            
        });

	}

    public void testGeoJsonToFeature() {
        
        injectUrlAndTest(() -> {
            java.lang.Object geoJson = geoJsonFormat.writeFeatureObject(createTestFeature(), null);

            // Convert Features from GeoJSON
            Feature featureGeoJson = geoJsonFormat.readFeature(geoJson, null);
            assertNotNull(featureGeoJson);
        });

    }
	
    public void testWriteGeoJSON() {
        
        injectUrlAndTest(() -> {
            String geoJson = geoJsonFormat.writeFeatures(createTestFeature(), null);
            assertNotNull(geoJson);

            java.lang.Object geoJsonObject = JsonUtils.safeEval(geoJson);
            assertNotNull(geoJsonObject);
        });

    }
	
    public void testReadFeatureFromGeoJson() {
        
        injectUrlAndTest(() -> {
            String geoJson = geoJsonFormat.writeFeature(createTestFeature(), null);
            assertNotNull(geoJson);
            Feature feature = geoJsonFormat.readFeature(geoJson, null);

            assertNotNull(feature);
        });

    }
    
    public void testReadFeatureCollectionFromGeoJson() {
        
        injectUrlAndTest(() -> {
            String geoJson = geoJsonFormat.writeFeatures(createTestFeature(), null);
            assertNotNull(geoJson);
            Feature[] features = geoJsonFormat.readFeatures(geoJson, null);

            assertNotNull(features);
            //assertTrue(features.length > 0);
        });

    }
	
	private Feature createTestFeature() {
		
		Coordinate coordinate1 = Coordinate.create(1, 1);
		Coordinate coordinate2 = Coordinate.create(5, 5);
		Coordinate[] coordinates = { coordinate1, coordinate2 };
		LineString lineString = new LineString(coordinates);

		// Create feature
		FeatureOptions featureOptions = new FeatureOptions();
		featureOptions.setGeometry(lineString);
		return new Feature(featureOptions);
		
	}

}

@@ -1031,17 +1031,20 @@
 Case %7B%0A%0A
-%09
+    
 private 
@@ -1070,15 +1070,13 @@
 at;%0A
-%09%0A%09%0A%09%0A%09
+%0A    
 @Ove
@@ -1081,17 +1081,20 @@
 verride%0A
-%09
+    
 protecte
@@ -1130,17 +1130,20 @@
 ption %7B%0A
-%09
+    
     inje
@@ -1197,14 +1197,19 @@
 ));%0A
-%09%7D%0A%09%0A%09
+    %7D%0A%0A    
 publ
@@ -1246,13 +1246,16 @@
  %7B%0A%0A
-%09
+   
     
+ 
 inje
@@ -1275,28 +1275,16 @@
 () -%3E %7B%0A
-            
 %0A       
@@ -1455,28 +1455,16 @@
 oJSON);%0A
-            
 %0A       
@@ -1468,18 +1468,20 @@
     %7D);%0A
-%0A%09
+    
 %7D%0A%0A    p
@@ -1512,32 +1512,24 @@
 Feature() %7B%0A
-        
 %0A        inj
@@ -1829,33 +1829,32 @@
      %7D);%0A%0A    %7D%0A
-%09
 %0A    public void
@@ -1871,32 +1871,24 @@
 GeoJSON() %7B%0A
-        
 %0A        inj
@@ -2166,17 +2166,16 @@
 %0A%0A    %7D%0A
-%09
 %0A    pub
@@ -2210,32 +2210,24 @@
 GeoJson() %7B%0A
-        
 %0A        inj
@@ -2496,20 +2496,16 @@
 %0A%0A    %7D%0A
-    
 %0A    pub
@@ -2554,24 +2554,16 @@
 son() %7B%0A
-        
 %0A       
@@ -2889,19 +2889,21 @@
 %0A%0A    %7D%0A
-%09%0A%09
+%0A    
 private 
@@ -2936,13 +2936,17 @@
 ) %7B%0A
-%09%09%0A%09%09
+%0A        
 Coor
@@ -2965,16 +2965,20 @@
 inate1 =
+ new
  Coordin
@@ -2984,23 +2984,16 @@
 nate
-.create
 (1, 1);%0A
 %09%09Co
@@ -2988,18 +2988,24 @@
 (1, 1);%0A
-%09%09
+        
 Coordina
@@ -3020,16 +3020,20 @@
 inate2 =
+ new
  Coordin
@@ -3039,23 +3039,16 @@
 nate
-.create
 (5, 5);%0A
 %09%09Co
@@ -3043,18 +3043,24 @@
 (5, 5);%0A
-%09%09
+        
 Coordina
@@ -3108,18 +3108,24 @@
 ate2 %7D;%0A
-%09%09
+        
 LineStri
@@ -3170,18 +3170,24 @@
 ates);%0A%0A
-%09%09
+        
 // Creat
@@ -3196,18 +3196,24 @@
 feature%0A
-%09%09
+        
 FeatureO
@@ -3258,18 +3258,24 @@
 ions();%0A
-%09%09
+        
 featureO
@@ -3306,18 +3306,24 @@
 tring);%0A
-%09%09
+        
 return n
@@ -3354,12 +3354,13 @@
 s);%0A
-%09%09%0A%09
+%0A    
 %7D%0A%0A%7D

Refactor GeoJsonTest