@Test
  public void testFindComposedTypeForClass() {
    when(modelService.getModelType(java.lang.String.class)).thenReturn("String");
    when(typeDao.findComposedTypeByCode("String")).thenReturn(model1);

    final ComposedTypeModel actualModel = typeService.getComposedTypeForCode("String");
    Assert.assertEquals("Method returned unexpected model", model1, actualModel);
  }
 @Test
 public void testFindTypeForNonExistingCode() {
   when(typeDao.findTypeByCode("noCodeFound"))
       .thenThrow(new UnknownIdentifierException("Identifier not found"));
   try {
     typeService.getTypeForCode("noCodeFound");
     Assert.fail("Non existing component model found!");
   } catch (final UnknownIdentifierException e) {
     // perfectly ok
   }
 }
  @Test
  public void testFindTypeForAmbiguousCode() {
    final List<TypeModel> multiModel = new ArrayList<TypeModel>();
    multiModel.add(model1);
    multiModel.add(model2);
    when(typeDao.findTypeByCode("multiCodeFound"))
        .thenThrow(new AmbiguousIdentifierException("Ambiguous identifier multiCodeFound"));

    try {
      typeService.getTypeForCode("multiCodeFound");
      Assert.fail("Not failed after more than one component model found!");
    } catch (final AmbiguousIdentifierException e) {
      // perfectly ok
    }
  }
  @Test
  public void testGetUniqueAttributesWithoutModelConverter() {
    final AttributeDescriptorModel attributeDescriptor1 = new AttributeDescriptorModel();
    attributeDescriptor1.setUnique(Boolean.TRUE);

    final ModelConverter nonItemModelConverter = mock(ModelConverter.class);
    final ComposedTypeModel nonItemModel = mock(ComposedTypeModel.class);
    when(converterRegistry.getModelConverterBySourceType("nonItemModel"))
        .thenReturn(nonItemModelConverter);
    when((nonItemModel).getDeclaredattributedescriptors())
        .thenReturn(Collections.singletonList(attributeDescriptor1));
    when(typeDao.findComposedTypeByCode("nonItemModel")).thenReturn(nonItemModel);

    final Set<String> actualUnique = typeService.getUniqueAttributes("nonItemModel");
    Assert.assertEquals("One attribute expected", 1, actualUnique.size());
  }
  @Test
  public void testFindMandatoryAttributesWithoutModelConverter() {
    final AttributeDescriptorModel attributeDescriptor1 = new AttributeDescriptorModel();
    attributeDescriptor1.setModifiers(Integer.valueOf(AttributeDescriptor.WRITE_FLAG));

    final ModelConverter nonItemModelConverter = mock(ModelConverter.class);
    final ComposedTypeModel nonItemModel = mock(ComposedTypeModel.class);
    when(converterRegistry.getModelConverterBySourceType("nonItemModel"))
        .thenReturn(nonItemModelConverter);
    when((nonItemModel).getDeclaredattributedescriptors())
        .thenReturn(Collections.singletonList(attributeDescriptor1));
    when(typeDao.findComposedTypeByCode("nonItemModel")).thenReturn(nonItemModel);

    final Set<String> actualMandatory = typeService.getMandatoryAttributes("nonItemModel", false);
    Assert.assertEquals("One attribute expected", 1, actualMandatory.size());
  }
  @Test
  public void testFindUniqueModelRootType() {
    final Set<String> unique = new HashSet<String>();
    unique.add("unique1");
    final ItemModelConverter modelConverter = mock(ItemModelConverter.class);
    when(modelConverter.getUniqueAttributes()).thenReturn(unique);

    when(converterRegistry.getModelConverterBySourceType("type1")).thenReturn(modelConverter);
    when(converterRegistry.getModelConverterBySourceType("type2")).thenReturn(modelConverter);

    final ComposedTypeModel composedType1 = mock(ComposedTypeModel.class);
    final ComposedTypeModel composedType2 = mock(ComposedTypeModel.class);
    when((composedType1).getCode()).thenReturn("type1");
    when((composedType2).getCode()).thenReturn("type2");
    when((composedType2).getSuperType()).thenReturn(composedType1);
    when(typeDao.findComposedTypeByCode("type2")).thenReturn(composedType2);

    final String actualUnique = typeService.getUniqueModelRootType("type2");
    Assert.assertEquals("Got unexpected unique root type", "type1", actualUnique);
  }
  @Test
  public void testGetAttributesForModiferCriteria() {
    final AttributeDescriptorModel declaredDescriptor = mock(AttributeDescriptorModel.class);
    when(declaredDescriptor.getModifiers()).thenReturn(Integer.valueOf(AttributeModifiers.READ));

    final AttributeDescriptorModel privateDescriptor = mock(AttributeDescriptorModel.class);
    when(privateDescriptor.getModifiers())
        .thenReturn(Integer.valueOf(AttributeModifiers.READ | AttributeModifiers.PRIVATE));
    when(privateDescriptor.getPrivate()).thenReturn(Boolean.TRUE);

    final AttributeDescriptorModel inheritedDescriptor = mock(AttributeDescriptorModel.class);
    when(inheritedDescriptor.getModifiers())
        .thenReturn(Integer.valueOf(AttributeModifiers.READ | AttributeModifiers.INHERITED));

    final AttributeDescriptorModel writableDescriptor = mock(AttributeDescriptorModel.class);
    when(writableDescriptor.getModifiers())
        .thenReturn(Integer.valueOf(AttributeModifiers.READ | AttributeModifiers.WRITE));
    when(writableDescriptor.getWritable()).thenReturn(Boolean.TRUE);

    final AttributeDescriptorModel partofDescriptor = mock(AttributeDescriptorModel.class);
    when(partofDescriptor.getModifiers())
        .thenReturn(
            Integer.valueOf(
                AttributeModifiers.READ | AttributeModifiers.PARTOF | AttributeModifiers.WRITE));
    when(partofDescriptor.getWritable()).thenReturn(Boolean.TRUE);
    when(partofDescriptor.getPartOf()).thenReturn(Boolean.TRUE);

    final Set<AttributeDescriptorModel> declaredDescriptorModels =
        new HashSet<AttributeDescriptorModel>();
    declaredDescriptorModels.add(declaredDescriptor);
    declaredDescriptorModels.add(writableDescriptor);
    declaredDescriptorModels.add(partofDescriptor);

    final Set<AttributeDescriptorModel> inheritedDescriptorModels =
        new HashSet<AttributeDescriptorModel>();
    inheritedDescriptorModels.add(inheritedDescriptor);

    final ComposedTypeModel typeModel = mock(ComposedTypeModel.class);
    when((typeModel).getDeclaredattributedescriptors()).thenReturn(declaredDescriptorModels);
    when((typeModel).getInheritedattributedescriptors()).thenReturn(inheritedDescriptorModels);

    final PK descriptorPk = PK.fromLong(1234567890);
    final AttributeDescriptor privateDescriptorItem = mock(AttributeDescriptor.class);
    when(privateDescriptorItem.getPK()).thenReturn(descriptorPk);
    when(Boolean.valueOf(privateDescriptorItem.isPrivate())).thenReturn(Boolean.TRUE);

    final Set<AttributeDescriptor> privateDescriptorItems = new HashSet<AttributeDescriptor>();
    privateDescriptorItems.add(privateDescriptorItem);

    final ComposedType composedType = mock(ComposedType.class);
    when(modelService.getSource(typeModel)).thenReturn(composedType);
    when(composedType.getAttributeDescriptorsIncludingPrivate()).thenReturn(privateDescriptorItems);
    when(modelService.get(privateDescriptorItem)).thenReturn(privateDescriptor);

    when(typeDao.findComposedTypeByCode("modifierTest")).thenReturn(typeModel);

    final AttributeModifierCriteria modifierCriteria =
        new AttributeModifierCriteria(0, AttributeModifiers.ALL, 0);
    Assert.assertEquals(
        "Five declared descriptors expected",
        5,
        typeService.getAttributesForModifiers("modifierTest", modifierCriteria).size());

    modifierCriteria.addDisallowed(AttributeModifiers.PRIVATE);
    Assert.assertEquals(
        "Four declared descriptors expected",
        4,
        typeService.getAttributesForModifiers("modifierTest", modifierCriteria).size());

    modifierCriteria.addDisallowed(AttributeModifiers.INHERITED);
    Assert.assertEquals(
        "Three declared descriptors expected",
        3,
        typeService.getAttributesForModifiers("modifierTest", modifierCriteria).size());

    modifierCriteria.addRequired(AttributeModifiers.WRITE);
    Assert.assertEquals(
        "Two declared descriptors expected",
        2,
        typeService.getAttributesForModifiers("modifierTest", modifierCriteria).size());

    modifierCriteria.addRequired(AttributeModifiers.PARTOF);
    Assert.assertEquals(
        "One declared descriptor expected",
        1,
        typeService.getAttributesForModifiers("modifierTest", modifierCriteria).size());
  }
 @Test
 public void testFindAtomicTypeForCode() {
   when(typeDao.findAtomicTypeByCode("atomicCode")).thenReturn(atomicModel1);
   final AtomicTypeModel actualModel = typeService.getAtomicTypeForCode("atomicCode");
   Assert.assertEquals("Method returned unexpected model", atomicModel1, actualModel);
 }
 @Test
 public void testFindComposedTypeForCode() {
   when(typeDao.findComposedTypeByCode("singleCodeFound")).thenReturn(model1);
   Assert.assertNotNull(
       "One composed type should be found", typeService.getComposedTypeForCode("singleCodeFound"));
 }