/** Test not to allow bad signature entity references */
  @Test
  public void testBadEntityReference() {
    ApplicationContext badContext =
        new AnnotationConfigApplicationContext(
            new Class<?>[] {ContextConfigurationWithBadEntityRefSignature.class});
    try {
      customizationAssemblyFactory.setApplicationContext(badContext);
      Assert.fail("Should not allow testBad - bad signature");
    } catch (DashboardException de) {
      Assert.assertEquals(
          "Wrong signature for the method for testBad. Expected is "
              + Arrays.asList(
                  CustomizationAssemblyFactoryImpl.ENTITY_REFERENCE_METHOD_EXPECTED_SIGNATURE)
              + "!!!",
          de.getMessage());
    }

    badContext =
        new AnnotationConfigApplicationContext(
            new Class<?>[] {ContextConfigurationWithDuplicates.class});
    try {
      customizationAssemblyFactory.setApplicationContext(badContext);
      Assert.fail("Should not allow testBad - duplicate entity reference");
    } catch (DashboardException de) {
      Assert.assertEquals(
          "Duplicate entity mapping references found for test. Fix!!!", de.getMessage());
    }
  }
  /** Test getting a component for a test */
  @Test
  public void testDataComponent() {
    ApplicationContext goodContext =
        new AnnotationConfigApplicationContext(
            new Class<?>[] {ContextConfigurationWithGoodMananger.class});
    customizationAssemblyFactory.setApplicationContext(goodContext);
    // test good one
    Assert.assertEquals(
        simpleMaleStudentEntity,
        customizationAssemblyFactory.getDataComponentForTest(
            "panel", "1", configMap.get("panel").getData()));
    // test an entity mapping that throws an exception
    Assert.assertNull(
        customizationAssemblyFactory.getDataComponentForTest(
            "panelException", "1", configMap.get("panelException").getData()));

    // test non-existent one
    Config.Data fakeDataConfig = new Config.Data("fake", "fake", false, null);
    Assert.assertNotNull(fakeDataConfig);
    try {
      customizationAssemblyFactory.getDataComponentForTest("panel", "1", fakeDataConfig);
      Assert.fail("Must not be able to find fake entity reference and throw an exception");
    } catch (DashboardException de) {
      Assert.assertEquals(
          "No entity mapping references found for " + fakeDataConfig.getEntityRef() + ". Fix!!!",
          de.getMessage());
    }
  }
 /** Test entity reference map contains student mapping */
 @Test
 public void testEntityReferenceMap() {
   Assert.assertTrue(
       "Student entity reference must exists",
       customizationAssemblyFactory.hasCachedEntityMapperReference("student"));
   Assert.assertTrue(customizationAssemblyFactory.hasInvokableSet("student"));
 }
  /** Test conditional layout contains different number of items depending on entity */
  @Test
  public void testConditionalLayout() {
    ModelAndViewConfig viewAndConfig =
        customizationAssemblyFactory.getModelAndViewConfig(
            "studentProfile", simpleMaleStudentEntity.get("id"));

    Assert.assertEquals(3, viewAndConfig.getConfig().size());
    viewAndConfig =
        customizationAssemblyFactory.getModelAndViewConfig(
            "studentProfile", simpleFemaleStudentEntity.get("id"));
    Assert.assertEquals(1, viewAndConfig.getConfig().size());
  }
  @Test
  public void testCache() {
    ApplicationContext goodContext =
        new AnnotationConfigApplicationContext(
            new Class<?>[] {ContextConfigurationWithGoodMananger.class});
    customizationAssemblyFactory.setApplicationContext(goodContext);
    ModelAndViewConfig modelAndViewConfig =
        customizationAssemblyFactory.getModelAndViewConfig("test", "section", true);
    Config testConfig = modelAndViewConfig.getConfig().get("test");
    Assert.assertEquals("section", testConfig.getId());
    Assert.assertEquals("section", testConfig.getParentId());
    Assert.assertEquals("SLC - Section Profile", testConfig.getName());
    Assert.assertEquals("LAYOUT", testConfig.getType().toString());
    Item[] items = testConfig.getItems();
    Assert.assertEquals(2, items.length);
    Assert.assertEquals("tab3", items[0].getId());
    Assert.assertEquals("List of Students", items[0].getName());
    Item[] itemA = items[0].getItems();
    Assert.assertEquals(1, itemA.length);
    Assert.assertEquals("listOfStudents", itemA[0].getId());
    Item[] itemAA = itemA[0].getItems();
    Assert.assertEquals(1, itemAA.length);
    Assert.assertEquals("Default View", itemAA[0].getName());
    Item[] itemAAA = itemAA[0].getItems();
    Assert.assertEquals(5, itemAAA.length);
    Assert.assertEquals("Student", itemAAA[0].getName());
    Assert.assertEquals("", itemAAA[1].getName());
    Assert.assertEquals("Grade", itemAAA[2].getName());
    Assert.assertEquals("Absence Count", itemAAA[3].getName());
    Assert.assertEquals("Tardy Count", itemAAA[4].getName());

    Assert.assertEquals("tab4", items[1].getId());
    Assert.assertEquals("LOS", items[1].getName());
    Item[] itemB = items[1].getItems();
    Assert.assertEquals(1, itemB.length);
    Assert.assertEquals("listOfStudents", itemB[0].getId());
    Item[] itemBA = itemB[0].getItems();
    Assert.assertEquals(1, itemBA.length);
    Assert.assertEquals("Default View", itemBA[0].getName());
    Item[] itemBAA = itemBA[0].getItems();
    Assert.assertEquals(5, itemBAA.length);
    Assert.assertEquals("Student", itemBAA[0].getName());
    Assert.assertEquals("", itemBAA[1].getName());
    Assert.assertEquals("Grade", itemBAA[2].getName());
    Assert.assertEquals("Absence Count", itemBAA[3].getName());
    Assert.assertEquals("Tardy Count", itemBAA[4].getName());
  }
 /** Test not to allow infinite recursion */
 @Test
 public void testConfigTooDeep() {
   try {
     customizationAssemblyFactory.getModelAndViewConfig("deep", simpleMaleStudentEntity.get("id"));
     Assert.fail("Should not allow deep config structures");
   } catch (Throwable t) {
     Assert.assertEquals(
         "The items hierarchy is too deep - only allow 5 elements", t.getMessage());
   }
 }
  /** Test data domain condition */
  @Test
  public void testCondition() {
    Config panel = configMap.get("panel");
    Assert.assertTrue(
        "Must be true for a student with gender = 'male'",
        customizationAssemblyFactory.checkCondition(panel, panel, simpleMaleStudentEntity));
    Assert.assertFalse(
        "Must be true for a student with gender = 'male'",
        customizationAssemblyFactory.checkCondition(panel, panel, simpleFemaleStudentEntity));

    Assert.assertFalse(
        "Must be false for a student with no gender field available",
        customizationAssemblyFactory.checkCondition(panel, panel, simpleNoGenderInfoStudentEntity));

    panel = configMap.get("panel1");
    Assert.assertTrue(
        "Must be true for a student with gradeNumeric = 5",
        customizationAssemblyFactory.checkCondition(panel, panel, simpleMaleStudentEntity));
    Assert.assertFalse(
        "Must be false for a student with gradeNumeric = 7",
        customizationAssemblyFactory.checkCondition(panel, panel, simpleFemaleStudentEntity));
  }
 @Test
 public void testDynamicHeaders() {
   Config panel = configMap.get("dynamicHeaders");
   GenericEntity meta = new GenericEntity();
   meta.put("id", "Funky ID");
   GenericEntity name = new GenericEntity();
   name.put("first", "AAA");
   meta.put("name", name);
   GenericEntity entity = new GenericEntity();
   entity.put("meta", meta);
   Config.Item[] items =
       customizationAssemblyFactory.getUpdatedDynamicHeaderTemplate(panel, entity);
   Assert.assertEquals("AAA", items[0].getName());
   Assert.assertEquals("Funky ID", items[1].getName());
 }
 @Before
 public void setup() {
   customizationAssemblyFactory.setApplicationContext(applicationContext);
 }