package cgeo.geocaching.connector.oc;

import cgeo.CGeoTestCase;
import cgeo.geocaching.Geocache;
import cgeo.geocaching.Settings;
import cgeo.geocaching.enumerations.CacheType;

import java.util.Collection;

public class OCXMLTest extends CGeoTestCase {

    public static void testOCGetCache() {
        final String geoCode = "OCDE76";

        final Geocache cache = OCXMLClient.getCache(geoCode);
        assertNotNull(cache);
        assertEquals(geoCode, cache.getGeocode());
        assertEquals("Gitarrenspielplatz", cache.getName());
        assertEquals(CacheType.TRADITIONAL, cache.getType());
        assertEquals(2.0, cache.getDifficulty(), 0.1);
        assertEquals(2.0, cache.getTerrain(), 0.1);
    }

    public static void testOCLogAttendedAsFound() {

        final String oldOCName = Settings.getOCConnectorUserName();
        try {
            Settings.setOCConnectorUserName("ra_sch");
            final String geoCode = "OCD541";
            final Geocache cache = OCXMLClient.getCache(geoCode);
            assertNotNull(cache);

            assertTrue(cache.isFound());
        } finally {
            Settings.setOCConnectorUserName(oldOCName);
        }
    }

    public static void testOCOwner() {
        final String oldOCName = Settings.getOCConnectorUserName();
        try {
            Settings.setOCConnectorUserName("andi12.2");
            final String geoCode = "OCC9BE";
            final Geocache cache = OCXMLClient.getCache(geoCode);
            assertNotNull(cache);

            assertTrue(cache.isOwner());
            assertEquals("180571", cache.getOwnerUserId());
        } finally {
            Settings.setOCConnectorUserName(oldOCName);
        }
    }

    public static void testOC0537Description() {
        final String geoCode = "OC0537";
        final Geocache cache = OCXMLClient.getCache(geoCode);
        assertNotNull(cache);

        assertFalse(cache.getDescription().length() < 100);
    }

    public static void testNoArchivedInNearby() {

        final boolean oldExcludeDisabled = Settings.isExcludeDisabledCaches();
        final boolean oldExcludeMine = Settings.isExcludeMyCaches();
        try {
            Settings.setExcludeDisabledCaches(false);
            Settings.setExcludeMine(false);
            // get an archived cache
            final Geocache cache = OCXMLClient.getCache("OCD541");
            assertNotNull(cache);
            assertTrue(cache.isArchived());
            // Get nearby for this cache
            final Collection<Geocache> caches = OCXMLClient.getCachesAround(cache.getCoords(), 0.5);
            // Should not be in the result!
            assertFalse(caches.contains(cache));
        } finally {
            Settings.setExcludeDisabledCaches(oldExcludeDisabled);
            Settings.setExcludeMine(oldExcludeMine);
        }
    }

    public static void testFetchTwiceDuplicatesDescription() {
        final String geoCode = "OCEFBA";
        final String description = "Bei dem Cache kannst du einen kleinen Schatz bergen. Bitte lege aber einen ander Schatz in das Döschen. Achtung vor Automuggels.";

        deleteCacheFromDB(geoCode);
        Geocache cache = OCXMLClient.getCache(geoCode);
        assertNotNull(cache);
        try {
            assertEquals(geoCode, cache.getGeocode());
            assertEquals(description, cache.getDescription());
            cache.store(null);

            // reload, make sure description is not duplicated
            cache = OCXMLClient.getCache(geoCode);
            assertNotNull(cache);
            assertEquals(description, cache.getDescription());
        } finally {
            deleteCacheFromDB(geoCode);
        }
    }

    public static void testRemoveMarkupCache() {
        final String geoCode = "OCEFBA";
        final String description = "Bei dem Cache kannst du einen kleinen Schatz bergen. Bitte lege aber einen ander Schatz in das Döschen. Achtung vor Automuggels.";

        Geocache cache = OCXMLClient.getCache(geoCode);
        assertNotNull(cache);
        assertEquals(description, cache.getDescription());
    }

    public static void testRemoveMarkup() {
        assertEquals("", OC11XMLParser.stripMarkup(""));
        assertEquals("Test", OC11XMLParser.stripMarkup("Test"));
        assertEquals("<b>bold and others not removed</b>", OC11XMLParser.stripMarkup("<b>bold and others not removed</b>"));
        assertEquals("unnecessary paragraph", OC11XMLParser.stripMarkup("<p>unnecessary paragraph</p>"));
        assertEquals("unnecessary span", OC11XMLParser.stripMarkup("<span>unnecessary span</span>"));
        assertEquals("nested", OC11XMLParser.stripMarkup("<span><span>nested</span></span>"));
        assertEquals("mixed", OC11XMLParser.stripMarkup("<span> <p> mixed </p> </span>"));
        assertEquals("<p>not</p><p>removable</p>", OC11XMLParser.stripMarkup("<p>not</p><p>removable</p>"));
    }
}

@@ -172,16 +172,89 @@
 eType;%0A%0A
+import org.apache.commons.lang3.StringUtils;%0A%0Aimport android.text.Html;%0A%0A
 import j
@@ -3364,32 +3364,106 @@
 .getGeocode());%0A
+            // ignore copyright as the date part will change all the time%0A
             asse
@@ -3476,32 +3476,55 @@
 ls(description, 
+removeCopyrightAndTags(
 cache.getDescrip
@@ -3522,32 +3522,33 @@
 etDescription())
+)
 ;%0A            ca
@@ -3743,32 +3743,55 @@
 ls(description, 
+removeCopyrightAndTags(
 cache.getDescrip
@@ -3793,24 +3793,25 @@
 scription())
+)
 ;%0A        %7D 
@@ -3878,37 +3878,38 @@
     %7D%0A%0A    p
-ublic
+rivate
  static 
 void testRem
@@ -3900,390 +3900,144 @@
 tic 
-void testRemoveMarkupCache() %7B%0A        final String geoCode = %22OCEFBA%22;%0A        final String description = %22Bei dem Cache kannst du einen kleinen Schatz bergen. Bitte lege aber einen ander Schatz in das D%C3%B6schen. Achtung vor Automuggels.%22;%0A%0A        Geocache cache = OCXMLClient.getCache(geoCode);%0A        assertNotNull(cache);%0A        assertEquals(description, cache.getDescription()
+String removeCopyrightAndTags(String input) %7B%0A        return Html.fromHtml(StringUtils.substringBefore(input, %22&copy%22)).toString().trim(
 );%0A 

fix tests after OC changes