@Test
  public void testFindUniqueAttributes() {
    final AttributeDescriptorModel attributeDescriptor1 = new AttributeDescriptorModel();
    attributeDescriptor1.setUnique(Boolean.TRUE);

    final Set<String> unique = new HashSet<String>();
    unique.add("unique1");
    final ItemModelConverter modelConverter = mock(ItemModelConverter.class);
    when(modelConverter.getUniqueAttributes()).thenReturn(unique);

    when(converterRegistry.getModelConverterBySourceType("model1")).thenReturn(modelConverter);
    final Set<String> actualUnique = typeService.getUniqueAttributes("model1");
    Assert.assertEquals("One attribute expected", 1, actualUnique.size());
    Assert.assertEquals("Unexpected attribute got", "unique1", actualUnique.iterator().next());
  }
  @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 removeAllWithoutTransaction() throws Exception {
    // given
    doReturn(sessionService).when(modelService).lookupSessionService();
    doReturn(interceptorRegistry).when(modelService).lookupInterceptorRegistry();
    doReturn(converterRegistry).when(modelService).lookupConverterRegistry();
    given(converterRegistry.getModelConverterByModelType((Class) anyObject()))
        .willReturn(modelConverter);
    given(modelConverter.getType(model1)).willReturn("ItemModel");
    given(sessionService.getAttribute(DefaultModelService.ENABLE_TRANSACTIONAL_SAVES))
        .willReturn(Boolean.FALSE);

    // when (execute 2 remove* methods)
    modelService.remove(model1);
    modelService.removeAll(Arrays.asList(model1, model2));

    // then (should be 0 interactions with transaction mock)
    verify(transaction, times(0)).execute((TransactionCallback) anyObject());
  }
  @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 shouldExecuteSaveVaragsObjectsWithoutTransactionWhenGlobalTransactionFlagIsFalse()
      throws Exception {
    // given
    modelService.setTransactional(false);
    doReturn(sessionService).when(modelService).lookupSessionService();
    doReturn(interceptorRegistry).when(modelService).lookupInterceptorRegistry();
    doReturn(converterRegistry).when(modelService).lookupConverterRegistry();
    given(converterRegistry.getModelConverterByModelType((Class) anyObject()))
        .willReturn(modelConverter);
    given(modelConverter.getType(model1)).willReturn("ItemModel");

    // when (execute all 4 save* methods)
    modelService.save(model1);
    modelService.saveAll(model1, model2);
    modelService.saveAll(Collections.singletonList(model1));
    modelService.saveAll();

    // then (should be 0 interactions with transaction mock)
    verify(transaction, times(0)).execute((TransactionCallback) anyObject());
  }
  @Test
  public void testFindMandatoryAttributes() {
    final AttributeDescriptorModel attributeDescriptor1 = new AttributeDescriptorModel();
    attributeDescriptor1.setModifiers(Integer.valueOf(AttributeDescriptor.WRITE_FLAG));

    final Set<String> mandatory = new HashSet<String>();
    mandatory.add("mandatory1");
    final ItemModelConverter modelConverter = mock(ItemModelConverter.class);
    when(modelConverter.getMandatoryAttributes()).thenReturn(mandatory);
    when(modelConverter.getMandatoryAttributesForCreation()).thenReturn(Collections.EMPTY_SET);

    when(converterRegistry.getModelConverterBySourceType("model1")).thenReturn(modelConverter);

    Set<String> actualMandatory = typeService.getMandatoryAttributes("model1", false);
    Assert.assertEquals("One attribute expected", 1, actualMandatory.size());
    Assert.assertEquals(
        "Unexpected attribute got", "mandatory1", actualMandatory.iterator().next());
    actualMandatory = typeService.getMandatoryAttributes("model1", true);
    Assert.assertEquals(
        "Zero attributes expected with force create set", 0, actualMandatory.size());
  }
  @Test
  public void
      shouldExecuteSaveVaragsObjectsWithoutTransactionWhenGlobalTransactionFlagIsTrueButLocallyTransactionsAreDisabled()
          throws Exception {
    // given
    modelService.setTransactional(true);
    doReturn(sessionService).when(modelService).lookupSessionService();
    doReturn(interceptorRegistry).when(modelService).lookupInterceptorRegistry();
    doReturn(converterRegistry).when(modelService).lookupConverterRegistry();
    given(converterRegistry.getModelConverterByModelType((Class) anyObject()))
        .willReturn(modelConverter);
    given(modelConverter.getType(model1)).willReturn("ItemModel");
    given(sessionService.getAttribute(DefaultModelService.ENABLE_TRANSACTIONAL_SAVES))
        .willReturn(Boolean.FALSE);

    // when (execute all 4 save* methods)
    modelService.save(model1);
    modelService.saveAll(model1, model2);
    modelService.saveAll(Collections.singletonList(model1));
    modelService.saveAll();

    // then (should be 0 interactions with transaction mock)
    verify(transaction, times(0)).execute((TransactionCallback) anyObject());
  }