/**
 * Copyright (C) 2009 - 2010 by OpenGamma Inc.
 *
 * Please see distribution for license.
 */
package com.opengamma.financial.securities;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Set;

import javax.time.calendar.Clock;
import javax.time.calendar.LocalDate;
import javax.time.calendar.TimeZone;

import org.junit.Assert;
import org.junit.Test;

import com.google.common.io.Resources;
import com.opengamma.financial.security.InMemoryRegionRepository;
import com.opengamma.financial.security.Region;
import com.opengamma.financial.security.RegionRepository;
import com.opengamma.financial.security.RegionType;

/**
 * Unit tests for the InMemoryRegionRepository
 */

public class InMemoryRegionRepositoryTest {
  @Test
  public void testConstructor() throws URISyntaxException {
    URL countryCSV = Resources.getResource("com/opengamma/financial/securities/countrylist_test.csv");
    RegionRepository regionRepository = new InMemoryRegionRepository(new File(countryCSV.toURI()));
    System.err.println("Constructed and indexed");
    LocalDate now = Clock.system(TimeZone.UTC).today();
    System.err.println("Got now");
    Set<Region> allOfType = regionRepository.getAllOfType(now, "Political", RegionType.INDEPENDENT_STATE);
    System.err.println("getAllOfType");
    Assert.assertEquals(193, allOfType.size());
    Assert.assertEquals(272, regionRepository.getHierarchyRoot(now, "Political").getSubRegions().size());
    System.err.println("getHierarchyRoot");
    Region ukRegion = regionRepository.getHierarchyNode(now, "Political", "United Kingdom");
    System.err.println("getHierarchyNode");
    Assert.assertNotNull(ukRegion);
    Assert.assertTrue(allOfType.contains(ukRegion));
    Assert.assertEquals(RegionType.INDEPENDENT_STATE, ukRegion.getRegionType());
    Assert.assertEquals(regionRepository.getHierarchyRoot(now, "Political"), ukRegion.getSuperRegion());
    Assert.assertEquals(0, ukRegion.getSubRegions().size());
    
    Assert.assertEquals(regionRepository.getHierarchyRoot(now, "Politcal"), regionRepository.getHierarchyNode(now, "Political", "World"));
    System.err.println("more getHierarchyNode");
    
    Assert.assertNull(regionRepository.getHierarchyNode(now, "Incorrect Name", "World"));
    Assert.assertNull(regionRepository.getHierarchyRoot(now, "Incorrect Name"));
  }
}

@@ -136,16 +136,151 @@
 ities;%0A%0A
+import static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertNotNull;%0Aimport static org.junit.Assert.assertTrue;%0A%0A
 import j
@@ -486,33 +486,8 @@
 e;%0A%0A
-import org.junit.Assert;%0A
 impo
@@ -545,16 +545,64 @@
 ources;%0A
+import com.opengamma.OpenGammaRuntimeException;%0A
 import c
@@ -873,17 +873,16 @@
 ory%0A */%0A
-%0A
 public c
@@ -917,16 +917,17 @@
 yTest %7B%0A
+%0A
   @Test%0A
@@ -1260,16 +1260,30 @@
 e now = 
+LocalDate.now(
 Clock.sy
@@ -1304,15 +1304,8 @@
 UTC)
-.today(
 );%0A 
@@ -1485,31 +1485,24 @@
 Type%22);%0A    
-Assert.
 assertEquals
@@ -1522,39 +1522,32 @@
 pe.size());%0A    
-Assert.
 assertEquals(272
@@ -1806,31 +1806,24 @@
 Node%22);%0A    
-Assert.
 assertNotNul
@@ -1835,31 +1835,24 @@
 egion);%0A    
-Assert.
 assertTrue(a
@@ -1877,39 +1877,32 @@
 ukRegion));%0A    
-Assert.
 assertEquals(Reg
@@ -1951,39 +1951,32 @@
 ionType());%0A    
-Assert.
 assertEquals(reg
@@ -2053,31 +2053,24 @@
 ion());%0A    
-Assert.
 assertEquals
@@ -2112,31 +2112,24 @@
 );%0A    %0A    
-Assert.
 assertEquals
@@ -2174,16 +2174,17 @@
 , %22Polit
+i
 cal%22), r
@@ -2300,123 +2300,918 @@
 ;%0A  
-  %0A    Assert.assertNull(regionRepository.getHierarchyNode(now, %22Incorrect Name%22, %22World%22));%0A    Assert.assertNull(
+%7D%0A%0A  @Test(expected=OpenGammaRuntimeException.class)  // TODO: check if this should throw exception or return null%0A  public void test_getHierarchyNode_unknownName() throws URISyntaxException %7B%0A    URL countryCSV = Resources.getResource(%22com/opengamma/financial/securities/countrylist_test.csv%22);%0A    RegionRepository regionRepository = new InMemoryRegionRepository(new File(countryCSV.toURI()));%0A    LocalDate now = LocalDate.now(Clock.system(TimeZone.UTC));%0A    regionRepository.getHierarchyNode(now, %22Incorrect Name%22, %22World%22);%0A  %7D%0A%0A  @Test%0A  public void test_getHierarchyRoot_unknownName() throws URISyntaxException %7B%0A    URL countryCSV = Resources.getResource(%22com/opengamma/financial/securities/countrylist_test.csv%22);%0A    RegionRepository regionRepository = new InMemoryRegionRepository(new File(countryCSV.toURI()));%0A    LocalDate now = LocalDate.now(Clock.system(TimeZone.UTC));%0A    assertEquals(null, 
 regi
@@ -3269,10 +3269,11 @@
 ));%0A  %7D%0A
+%0A
 %7D%0A

Fix test case failure for regions