Search in sources :

Example 1 with DefaultEntity

use of com.qcadoo.model.internal.DefaultEntity in project qcadoo by qcadoo.

the class MenuServiceImplTest method shouldCreateCategory.

@Test
public final void shouldCreateCategory() {
    // given
    MenuCategoryDefinition menuCategoryDefinition = new MenuCategoryDefinition(PLUGIN_IDENTIFIER, "categoryName", ROLE_VISIBLE);
    given(menuCrudService.getCategory(menuCategoryDefinition)).willReturn(null);
    given(menuCrudService.createEntity(QcadooViewConstants.MODEL_CATEGORY)).willAnswer(new Answer<Entity>() {

        @Override
        public Entity answer(final InvocationOnMock invocation) throws Throwable {
            DataDefinition dataDefinition = mock(DataDefinition.class);
            return new DefaultEntity(dataDefinition);
        }
    });
    given(menuCrudService.getTotalNumberOfCategories()).willReturn(3);
    // when
    menuService.createCategory(menuCategoryDefinition);
    // then
    verify(menuCrudService).save(entityCaptor.capture());
    Entity savedItem = entityCaptor.getValue();
    assertEquals(PLUGIN_IDENTIFIER, savedItem.getStringField(MenuCategoryFields.PLUGIN_IDENTIFIER));
    assertEquals("categoryName", savedItem.getStringField(MenuCategoryFields.NAME));
    assertEquals(3, savedItem.getField(MenuCategoryFields.SUCCESSION));
    assertEquals(ROLE_VISIBLE, savedItem.getStringField(MenuCategoryFields.AUTH_ROLE));
}
Also used : Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) MenuCategoryDefinition(com.qcadoo.view.internal.menu.definitions.MenuCategoryDefinition) DataDefinition(com.qcadoo.model.api.DataDefinition) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with DefaultEntity

use of com.qcadoo.model.internal.DefaultEntity in project qcadoo by qcadoo.

the class MenuServiceImplTest method shouldCreateViewForUrlItem.

@Test
public final void shouldCreateViewForUrlItem() {
    // given
    MenuItemDefinition menuUrlItemDefinition = MenuItemDefinition.create(PLUGIN_IDENTIFIER, "itemName", "categoryName", ROLE_VISIBLE, true).forUrl("someUrl");
    given(menuCrudService.getView(menuUrlItemDefinition)).willReturn(null);
    given(menuCrudService.createEntity(QcadooViewConstants.MODEL_VIEW)).willAnswer(new Answer<Entity>() {

        @Override
        public Entity answer(final InvocationOnMock invocation) throws Throwable {
            DataDefinition dataDefinition = mock(DataDefinition.class);
            return new DefaultEntity(dataDefinition);
        }
    });
    // when
    menuService.addView(menuUrlItemDefinition);
    // then
    verify(menuCrudService).save(entityCaptor.capture());
    Entity savedView = entityCaptor.getValue();
    assertEquals("somePlugin", savedView.getStringField(ViewFields.PLUGIN_IDENTIFIER));
    assertEquals("itemName", savedView.getStringField(ViewFields.NAME));
    assertNull(savedView.getStringField(ViewFields.VIEW));
    assertEquals("someUrl", savedView.getStringField(ViewFields.URL));
}
Also used : Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) MenuItemDefinition(com.qcadoo.view.internal.menu.definitions.MenuItemDefinition) DataDefinition(com.qcadoo.model.api.DataDefinition) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with DefaultEntity

use of com.qcadoo.model.internal.DefaultEntity in project qcadoo by qcadoo.

the class MenuServiceImplTest method shouldCreateDeactivatedItem.

@Test
public final void shouldCreateDeactivatedItem() {
    // given
    MenuItemDefinition menuItemDefinition = MenuItemDefinition.create(PLUGIN_IDENTIFIER, "itemName", "categoryName", ROLE_VISIBLE, false).forView(PLUGIN_IDENTIFIER, "viewName");
    given(menuCrudService.getItem(menuItemDefinition)).willReturn(null);
    Entity categoryEntity = mockCategory(PLUGIN_IDENTIFIER, "categoryName", ROLE_VISIBLE, Collections.<Entity>emptyList());
    given(menuCrudService.getCategory(menuItemDefinition)).willReturn(categoryEntity);
    Entity viewEntity = mockViewEntity(PLUGIN_IDENTIFIER, "viewName");
    given(menuCrudService.getView(menuItemDefinition)).willReturn(viewEntity);
    given(menuCrudService.createEntity(QcadooViewConstants.MODEL_ITEM)).willAnswer(new Answer<Entity>() {

        @Override
        public Entity answer(final InvocationOnMock invocation) throws Throwable {
            DataDefinition dataDefinition = mock(DataDefinition.class);
            return new DefaultEntity(dataDefinition);
        }
    });
    // when
    menuService.createItem(menuItemDefinition);
    // then
    verify(menuCrudService).save(entityCaptor.capture());
    Entity savedItem = entityCaptor.getValue();
    assertEquals(PLUGIN_IDENTIFIER, savedItem.getStringField(MenuItemFields.PLUGIN_IDENTIFIER));
    assertEquals("itemName", savedItem.getStringField(MenuItemFields.NAME));
    assertFalse(savedItem.getBooleanField(MenuItemFields.ACTIVE));
    assertEquals(viewEntity, savedItem.getField(MenuItemFields.VIEW));
    assertEquals(categoryEntity, savedItem.getField(MenuItemFields.CATEGORY));
    assertEquals(1, savedItem.getField(MenuItemFields.SUCCESSION));
    assertEquals(ROLE_VISIBLE, savedItem.getStringField(MenuItemFields.AUTH_ROLE));
}
Also used : Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) MenuItemDefinition(com.qcadoo.view.internal.menu.definitions.MenuItemDefinition) DataDefinition(com.qcadoo.model.api.DataDefinition) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with DefaultEntity

use of com.qcadoo.model.internal.DefaultEntity in project qcadoo by qcadoo.

the class HookTest method shouldBeTriggeredInOrderOfAdding2.

@Test
public final void shouldBeTriggeredInOrderOfAdding2() {
    // given
    Entity entity = new DefaultEntity(dataDefinition);
    entity.setField("name", "a");
    dataDefinition.addSaveHook(buildHook("appendC"));
    dataDefinition.addSaveHook(buildHook("appendB"));
    dataDefinition.addSaveHook(buildHook("appendD"));
    // when
    entity = dataDefinition.save(entity);
    // then
    assertEquals("acbd", entity.getStringField("name"));
}
Also used : Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) DataAccessTest(com.qcadoo.model.internal.DataAccessTest) Test(org.junit.Test)

Example 5 with DefaultEntity

use of com.qcadoo.model.internal.DefaultEntity in project qcadoo by qcadoo.

the class HookTest method shouldCreateHookNotSeeNewValueOfReadOnlyField.

@Test
public void shouldCreateHookNotSeeNewValueOfReadOnlyField() throws Exception {
    // given
    Entity entity = new DefaultEntity(dataDefinition, null);
    entity.setField("name", null);
    entity.setField("age", null);
    entity.setField("readOnly", "youCanNotSeeMe!");
    dataDefinition.addCreateHook(new EntityHookDefinitionImpl(CustomEntityService.class.getName(), "rewriteReadOnlyField", PLUGIN_IDENTIFIER, applicationContext));
    // when
    entity = dataDefinition.save(entity);
    // then
    assertNull(entity.getField("readOnly"));
    assertNull(entity.getField("name"));
}
Also used : Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) EntityHookDefinitionImpl(com.qcadoo.model.internal.hooks.EntityHookDefinitionImpl) DataAccessTest(com.qcadoo.model.internal.DataAccessTest) Test(org.junit.Test)

Aggregations

DefaultEntity (com.qcadoo.model.internal.DefaultEntity)92 Test (org.junit.Test)91 Entity (com.qcadoo.model.api.Entity)90 DataAccessTest (com.qcadoo.model.internal.DataAccessTest)66 SampleSimpleDatabaseObject (com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject)53 EntityHookDefinitionImpl (com.qcadoo.model.internal.hooks.EntityHookDefinitionImpl)16 AbstractStateTest (com.qcadoo.view.internal.states.AbstractStateTest)12 DataDefinition (com.qcadoo.model.api.DataDefinition)10 RangeValidator (com.qcadoo.model.internal.validators.RangeValidator)9 CustomEntityService (com.qcadoo.model.beans.sample.CustomEntityService)8 LengthValidator (com.qcadoo.model.internal.validators.LengthValidator)8 UnscaledValueValidator (com.qcadoo.model.internal.validators.UnscaledValueValidator)8 JSONObject (org.json.JSONObject)8 BigDecimal (java.math.BigDecimal)7 FieldDefinition (com.qcadoo.model.api.FieldDefinition)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 ScaleValidator (com.qcadoo.model.internal.validators.ScaleValidator)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 FieldHookDefinitionImpl (com.qcadoo.model.internal.hooks.FieldHookDefinitionImpl)4 CustomEntityValidator (com.qcadoo.model.internal.validators.CustomEntityValidator)4