/*
 * Copyright (c) 2005-2011 Grameen Foundation USA
 * All rights reserved.
 *
 * 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.
 *
 * See also http://www.apache.org/licenses/LICENSE-2.0.html for an
 * explanation of the license and how it is applied.
 */

package org.mifos.test.acceptance.rest.api;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import junit.framework.Assert;

import org.apache.commons.lang.StringUtils;
import org.aspectj.util.FileUtil;
import org.mifos.test.acceptance.framework.AppLauncher;
import org.mifos.test.acceptance.framework.login.LoginPage;
import org.springframework.core.io.ClassPathResource;

import com.thoughtworks.selenium.Selenium;

public class RESTAPITestHelper {

    private final Selenium selenium;

    public static class Type {
        public static final String CLIENT = "client";
    }

    public static class By {
        public static final String GLOBAL_NUMBER = "num";
    }

    public RESTAPITestHelper(Selenium selenium) {
        this.selenium = selenium;
    }

    public void navigateToJsonAjaxPage() {
        LoginPage loginPage = new AppLauncher(selenium).launchMifos();
        loginPage.loginSuccessfullyUsingDefaultCredentials();
        selenium.open("jsonAjax.ftl");
    }

    public String getJSONFromUI(String type, String by, String value) throws InterruptedException {
        String url = String.format("%s/%s-%s.json", type, by, value);
        selenium.type("resturl", url);
        selenium.click("getJSON");
        Thread.sleep(1000);
        return selenium.getText("restdata");
    }

    public String getJSONFromDataSet(String type, String by, String value) throws IOException {
        String path = String.format("/dataSets/rest/%s-%s-%s.json", type, by, value);
        ClassPathResource resource = new ClassPathResource(path);
        File file = resource.getFile();
        if (file == null) {
            throw new FileNotFoundException("Couldn't find file");
        }
        return FileUtil.readAsString(file);
    }

    public void assertEquals(String expectedJSON, String actualJSON) {
        expectedJSON = compress(expectedJSON);
        actualJSON = compress(actualJSON);
        if(!expectedJSON.equals(actualJSON)) {
            int diffIndex = findDiff(expectedJSON,actualJSON);
            Assert.fail("json different at index " + diffIndex +"\n "+expectedJSON.substring(diffIndex) +"\n "+actualJSON.substring(diffIndex));
        }
    }

    private String compress(String str) {
        str = StringUtils.remove(str, ' ');
        str = StringUtils.remove(str, '\t');
        str = StringUtils.remove(str, '\n');
        str = StringUtils.remove(str, '\r');
        return str;
    }

    private int findDiff(String expectedJSON, String actualJSON) {
        return StringUtils.indexOfDifference(expectedJSON, actualJSON);
    }

}

@@ -2632,16 +2632,23 @@
         
+String 
 expected
@@ -2648,19 +2648,29 @@
 xpectedJ
-SON
+sonCompressed
  = compr
@@ -2696,26 +2696,43 @@
         
-actualJSON
+String actualJsonCompressed
  = compr
@@ -2740,32 +2740,41 @@
 ss(actualJSON);%0A
+        %0A
         if(!expe
@@ -2778,19 +2778,29 @@
 xpectedJ
-SON
+sonCompressed
 .equals(
@@ -2806,19 +2806,29 @@
 (actualJ
-SON
+sonCompressed
 )) %7B%0A   
@@ -2874,22 +2874,43 @@
 tedJ
-SON,actualJSON
+sonCompressed, actualJsonCompressed
 );%0A 
@@ -2987,19 +2987,29 @@
 xpectedJ
-SON
+sonCompressed
 .substri
@@ -3036,19 +3036,29 @@
 +actualJ
-SON
+sonCompressed
 .substri
@@ -3082,32 +3082,61 @@
        %7D%0A    %7D%0A%0A
+    @SuppressWarnings(%22PMD%22)%0A
     private Stri
@@ -3158,14 +3158,47 @@
 ing 
-str) %7B
+compress) %7B%0A    %09String str = compress;
 %0A   
@@ -3549,7 +3549,5 @@
   %7D%0A
-%0A%7D%0A
+%7D

fix pmd warnings in rest test helper.