Search in sources :

Example 1 with GlobalControllerExceptionHandler

use of org.molgenis.web.exception.GlobalControllerExceptionHandler in project molgenis by molgenis.

the class RestControllerTest method beforeMethod.

@BeforeMethod
public void beforeMethod() {
    MockitoAnnotations.initMocks(this);
    reset(permissionService, dataService, metaDataService, tokenService);
    when(dataService.getMeta()).thenReturn(metaDataService);
    @SuppressWarnings("unchecked") Repository<Entity> repo = mock(Repository.class);
    // test entity meta data
    EntityType entityType = mock(EntityType.class);
    Attribute attrId = when(mock(Attribute.class).getName()).thenReturn("id").getMock();
    when(attrId.getLabel()).thenReturn("id");
    when(attrId.getLabel("en")).thenReturn("id");
    when(attrId.getDataType()).thenReturn(STRING);
    when(attrId.isReadOnly()).thenReturn(true);
    when(attrId.isUnique()).thenReturn(true);
    when(attrId.isNillable()).thenReturn(false);
    when(attrId.isVisible()).thenReturn(false);
    when(attrId.getChildren()).thenReturn(emptyList());
    when(attrId.getEnumOptions()).thenReturn(emptyList());
    Attribute attrName = when(mock(Attribute.class).getName()).thenReturn("name").getMock();
    when(attrName.getLabel()).thenReturn("name");
    when(attrName.getLabel("en")).thenReturn("name");
    when(attrName.getDataType()).thenReturn(STRING);
    when(attrName.isNillable()).thenReturn(true);
    when(attrName.isVisible()).thenReturn(true);
    when(attrName.getChildren()).thenReturn(emptyList());
    when(attrName.getEnumOptions()).thenReturn(emptyList());
    Attribute attrEnum = when(mock(Attribute.class).getName()).thenReturn("enum").getMock();
    when(attrEnum.getLabel()).thenReturn("enum");
    when(attrEnum.getLabel("en")).thenReturn("enum");
    when(attrEnum.getDataType()).thenReturn(AttributeType.ENUM);
    when(attrEnum.getEnumOptions()).thenReturn(singletonList("enum0, enum1"));
    when(attrEnum.isNillable()).thenReturn(true);
    when(attrEnum.isVisible()).thenReturn(true);
    when(attrEnum.getChildren()).thenReturn(emptyList());
    Attribute attrInt = when(mock(Attribute.class).getName()).thenReturn("int").getMock();
    when(attrInt.getLabel()).thenReturn("int");
    when(attrInt.getLabel("en")).thenReturn("int");
    when(attrInt.getDataType()).thenReturn(AttributeType.INT);
    when(attrInt.isNillable()).thenReturn(true);
    when(attrInt.isVisible()).thenReturn(true);
    when(attrInt.getChildren()).thenReturn(emptyList());
    when(entityType.getAttribute("id")).thenReturn(attrId);
    when(entityType.getAttribute("name")).thenReturn(attrName);
    when(entityType.getAttribute("enum")).thenReturn(attrEnum);
    when(entityType.getAttribute("int")).thenReturn(attrInt);
    when(entityType.getMappedByAttributes()).thenReturn(Stream.empty());
    when(entityType.getIdAttribute()).thenReturn(attrId);
    // TODO: This upgrades the test to mockito 2 but actually shows an error in the test
    when(entityType.getLookupAttributes()).thenReturn(null);
    when(entityType.getAttributes()).thenReturn(asList(attrName, attrId, attrEnum, attrInt));
    when(entityType.getAtomicAttributes()).thenReturn(asList(attrName, attrId, attrEnum, attrInt));
    when(entityType.getId()).thenReturn(ENTITY_NAME);
    when(entityType.getLabel("en")).thenReturn(null);
    when(repo.getEntityType()).thenReturn(entityType);
    when(repo.getName()).thenReturn(ENTITY_NAME);
    when(dataService.getEntityType(ENTITY_NAME)).thenReturn(entityType);
    when(entityManager.create(entityType, POPULATE)).thenReturn(new DynamicEntity(entityType));
    // test entities
    Entity entityXref = new DynamicEntity(entityType);
    entityXref.set("id", ENTITY_UNTYPED_ID);
    entityXref.set("name", "PietXREF");
    Entity entity = new DynamicEntity(entityType);
    entity.set("id", ENTITY_UNTYPED_ID);
    entity.set("name", "Piet");
    entity.set("enum", "enum1");
    entity.set("int", 1);
    Entity entity2 = new DynamicEntity(entityType);
    entity2.set("id", "p2");
    entity2.set("name", "Klaas");
    entity2.set("int", 2);
    when(dataService.getEntityTypeIds()).thenReturn(Stream.of(ENTITY_NAME));
    when(dataService.getRepository(ENTITY_NAME)).thenReturn(repo);
    when(dataService.findOneById(ArgumentMatchers.eq(ENTITY_NAME), ArgumentMatchers.eq(ENTITY_UNTYPED_ID), any(Fetch.class))).thenReturn(entity);
    when(dataService.findOneById(ENTITY_NAME, ENTITY_UNTYPED_ID)).thenReturn(entity);
    Query<Entity> q = new QueryImpl<>().eq("name", "Piet").pageSize(10).offset(5);
    when(dataService.findAll(ENTITY_NAME, q)).thenReturn(Stream.of(entity));
    Query<Entity> q2 = new QueryImpl<>().sort(new Sort().on("name", Sort.Direction.DESC)).pageSize(100).offset(0);
    when(dataService.findAll(ENTITY_NAME, q2)).thenReturn(Stream.of(entity2, entity));
    when(localeResolver.resolveLocale(any())).thenReturn(ENGLISH);
    mockMvc = MockMvcBuilders.standaloneSetup(restController).setMessageConverters(gsonHttpMessageConverter, new CsvHttpMessageConverter()).setCustomArgumentResolvers(new TokenExtractor()).setControllerAdvice(new GlobalControllerExceptionHandler(), new FallbackExceptionHandler(), new SpringExceptionHandler()).setLocaleResolver(localeResolver).build();
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) Attribute(org.molgenis.data.meta.model.Attribute) DynamicEntity(org.molgenis.data.support.DynamicEntity) SpringExceptionHandler(org.molgenis.web.exception.SpringExceptionHandler) CsvHttpMessageConverter(org.molgenis.core.ui.messageconverter.CsvHttpMessageConverter) EntityType(org.molgenis.data.meta.model.EntityType) TokenExtractor(org.molgenis.security.token.TokenExtractor) GlobalControllerExceptionHandler(org.molgenis.web.exception.GlobalControllerExceptionHandler) FallbackExceptionHandler(org.molgenis.web.exception.FallbackExceptionHandler) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 2 with GlobalControllerExceptionHandler

use of org.molgenis.web.exception.GlobalControllerExceptionHandler in project molgenis by molgenis.

the class UserAccountControllerMockMvcTest method setUp.

@BeforeMethod
public void setUp() throws Exception {
    config.resetMocks();
    MockitoAnnotations.initMocks(this);
    mockMvc = MockMvcBuilders.standaloneSetup(userAccountController).setLocaleResolver(localeResolver).setMessageConverters(gsonHttpMessageConverter).setControllerAdvice(new GlobalControllerExceptionHandler(), new FallbackExceptionHandler(), new SpringExceptionHandler()).build();
}
Also used : SpringExceptionHandler(org.molgenis.web.exception.SpringExceptionHandler) GlobalControllerExceptionHandler(org.molgenis.web.exception.GlobalControllerExceptionHandler) FallbackExceptionHandler(org.molgenis.web.exception.FallbackExceptionHandler) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 3 with GlobalControllerExceptionHandler

use of org.molgenis.web.exception.GlobalControllerExceptionHandler in project molgenis by molgenis.

the class RestControllerV2Test method beforeMethod.

@BeforeMethod
public void beforeMethod() {
    reset(dataService);
    reset(repoCopier);
    EntityType refRefEntityType = entityTypeFactory.create(REF_REF_ENTITY_NAME).setLabel(REF_REF_ENTITY_NAME).addAttribute(attributeFactory.create().setName(REF_REF_ATTR_ID_NAME), ROLE_ID, ROLE_LOOKUP).addAttribute(attributeFactory.create().setName(REF_REF_ATTR_VALUE_NAME), ROLE_LABEL);
    EntityType selfRefEntityType = entityTypeFactory.create(SELF_REF_ENTITY_NAME).setLabel(SELF_REF_ENTITY_NAME).addAttribute(attributeFactory.create().setName("id"), ROLE_ID, ROLE_LABEL, ROLE_LOOKUP);
    selfRefEntityType.addAttribute(attributeFactory.create().setName("selfRef").setDataType(XREF).setRefEntity(selfRefEntityType));
    Entity selfRefEntity = new DynamicEntity(selfRefEntityType);
    selfRefEntity.set("id", "0");
    selfRefEntity.set("selfRef", selfRefEntity);
    EntityType refEntityType = entityTypeFactory.create(REF_ENTITY_NAME).setLabel(REF_ENTITY_NAME).addAttribute(attributeFactory.create().setName(REF_ATTR_ID_NAME), ROLE_ID, ROLE_LOOKUP).addAttribute(attributeFactory.create().setName(REF_ATTR_VALUE_NAME), ROLE_LABEL).addAttribute(attributeFactory.create().setName(REF_ATTR_REF_NAME).setDataType(XREF).setRefEntity(refRefEntityType));
    // required
    String attrIdName = "id";
    attrBoolName = "bool";
    String attrCategoricalName = "categorical";
    String attrCategoricalMrefName = "categorical_mref";
    attrCompoundName = "compound";
    attrCompoundAttr0Name = "compound_attr0";
    attrCompoundCategorical = "compound_categorical";
    attrCompoundAttrCompoundName = "compound_attrcompound";
    attrCompoundAttrCompoundAttr0Name = "compound_attrcompound_attr0";
    String attrDateName = "date";
    String attrDateTimeName = "date_time";
    String attrDecimalName = "decimal";
    String attrEmailName = "email";
    String attrEnumName = "enum";
    String attrHtmlName = "html";
    String attrHyperlinkName = "hyperlink";
    String attrIntName = "int";
    String attrLongName = "long";
    String attrMrefName = "mref";
    String attrScriptName = "script";
    attrStringName = "string";
    String attrTextName = "text";
    attrXrefName = "xref";
    // optional
    String attrBoolOptionalName = "boolOptional";
    String attrCategoricalOptionalName = "categoricalOptional";
    String attrCategoricalMrefOptionalName = "categorical_mrefOptional";
    String attrCompoundAttr0OptionalName = "compound_attr0Optional";
    String attrCompoundAttrCompoundAttr0OptionalName = "compound_attrcompound_attr0Optional";
    String attrDateOptionalName = "dateOptional";
    String attrDateTimeOptionalName = "date_timeOptional";
    String attrDecimalOptionalName = "decimalOptional";
    String attrEmailOptionalName = "emailOptional";
    String attrEnumOptionalName = "enumOptional";
    String attrHtmlOptionalName = "htmlOptional";
    String attrHyperlinkOptionalName = "hyperlinkOptional";
    String attrIntOptionalName = "intOptional";
    String attrLongOptionalName = "longOptional";
    String attrMrefOptionalName = "mrefOptional";
    String attrScriptOptionalName = "scriptOptional";
    String attrStringOptionalName = "stringOptional";
    String attrTextOptionalName = "textOptional";
    String attrXrefOptionalName = "xrefOptional";
    String enum0 = "enum0";
    String enum1 = "enum1";
    String enum2 = "enum2";
    // required
    entityType = entityTypeFactory.create(ENTITY_NAME).setLabel(ENTITY_NAME);
    Attribute attrId = attributeFactory.create().setName(attrIdName);
    entityType.addAttribute(attrId, ROLE_ID, ROLE_LABEL, ROLE_LOOKUP);
    Attribute attrBool = createAttributeMeta(entityType, attrBoolName, BOOL).setNillable(false);
    Attribute attrCategorical = createAttributeMeta(entityType, attrCategoricalName, CATEGORICAL, refEntityType).setNillable(false);
    Attribute attrCategoricalMref = createAttributeMeta(entityType, attrCategoricalMrefName, CATEGORICAL_MREF, refEntityType).setNillable(false);
    Attribute attrCompound = createAttributeMeta(entityType, attrCompoundName, COMPOUND);
    Attribute compoundAttr0 = createAttributeMeta(entityType, attrCompoundAttr0Name, STRING).setNillable(false).setParent(attrCompound);
    Attribute compoundAttrCategorical = createAttributeMeta(entityType, attrCompoundCategorical, CATEGORICAL, refEntityType).setNillable(false).setParent(attrCompound);
    Attribute compoundAttr0Optional = createAttributeMeta(entityType, attrCompoundAttr0OptionalName, STRING).setNillable(true).setParent(attrCompound);
    Attribute compoundAttrCompound = createAttributeMeta(entityType, attrCompoundAttrCompoundName, COMPOUND).setParent(attrCompound);
    Attribute compoundAttrCompoundAttr0 = createAttributeMeta(entityType, attrCompoundAttrCompoundAttr0Name, STRING).setNillable(false).setParent(compoundAttrCompound);
    Attribute compoundAttrCompoundAttr0Optional = createAttributeMeta(entityType, attrCompoundAttrCompoundAttr0OptionalName, STRING).setNillable(true).setParent(compoundAttrCompound);
    Attribute attrDate = createAttributeMeta(entityType, attrDateName, DATE).setNillable(false);
    Attribute attrDateTime = createAttributeMeta(entityType, attrDateTimeName, DATE_TIME).setNillable(false);
    Attribute attrDecimal = createAttributeMeta(entityType, attrDecimalName, DECIMAL, null).setReadOnly(true).setNillable(false);
    Attribute attrEmail = createAttributeMeta(entityType, attrEmailName, EMAIL).setNillable(false);
    Attribute attrEnum = createAttributeMeta(entityType, attrEnumName, ENUM).setEnumOptions(asList(enum0, enum1, enum2)).setNillable(false);
    Attribute attrHtml = createAttributeMeta(entityType, attrHtmlName, HTML).setNillable(false);
    Attribute attrHyperlink = createAttributeMeta(entityType, attrHyperlinkName, HYPERLINK).setNillable(false);
    Attribute attrInt = createAttributeMeta(entityType, attrIntName, INT).setNillable(false);
    Attribute attrLong = createAttributeMeta(entityType, attrLongName, LONG).setNillable(false);
    Attribute attrMref = createAttributeMeta(entityType, attrMrefName, MREF, refEntityType).setNillable(false);
    Attribute attrScript = createAttributeMeta(entityType, attrScriptName, SCRIPT).setNillable(false);
    Attribute attrString = createAttributeMeta(entityType, attrStringName, STRING).setNillable(false);
    Attribute attrText = createAttributeMeta(entityType, attrTextName, TEXT).setNillable(false);
    Attribute attrXref = createAttributeMeta(entityType, attrXrefName, XREF, refEntityType).setNillable(false);
    // optional
    Attribute attrBoolOptional = createAttributeMeta(entityType, attrBoolOptionalName, BOOL);
    Attribute attrCategoricalOptional = createAttributeMeta(entityType, attrCategoricalOptionalName, CATEGORICAL, refEntityType);
    Attribute attrCategoricalMrefOptional = createAttributeMeta(entityType, attrCategoricalMrefOptionalName, CATEGORICAL_MREF, refEntityType);
    Attribute attrDateOptional = createAttributeMeta(entityType, attrDateOptionalName, DATE);
    Attribute attrDateTimeOptional = createAttributeMeta(entityType, attrDateTimeOptionalName, DATE_TIME);
    Attribute attrDecimalOptional = createAttributeMeta(entityType, attrDecimalOptionalName, DECIMAL, null);
    Attribute attrEmailOptional = createAttributeMeta(entityType, attrEmailOptionalName, EMAIL);
    Attribute attrEnumOptional = createAttributeMeta(entityType, attrEnumOptionalName, ENUM).setEnumOptions(asList(enum0, enum1, enum2));
    Attribute attrHtmlOptional = createAttributeMeta(entityType, attrHtmlOptionalName, HTML);
    Attribute attrHyperlinkOptional = createAttributeMeta(entityType, attrHyperlinkOptionalName, HYPERLINK);
    Attribute attrIntOptional = createAttributeMeta(entityType, attrIntOptionalName, INT);
    Attribute attrLongOptional = createAttributeMeta(entityType, attrLongOptionalName, LONG);
    Attribute attrMrefOptional = createAttributeMeta(entityType, attrMrefOptionalName, MREF, refEntityType);
    Attribute attrScriptOptional = createAttributeMeta(entityType, attrScriptOptionalName, SCRIPT);
    Attribute attrStringOptional = createAttributeMeta(entityType, attrStringOptionalName, STRING);
    Attribute attrTextOptional = createAttributeMeta(entityType, attrTextOptionalName, TEXT);
    Attribute attrXrefOptional = createAttributeMeta(entityType, attrXrefOptionalName, XREF, refEntityType);
    Entity refRefEntity = new DynamicEntity(refRefEntityType);
    refRefEntity.set(REF_REF_ATTR_ID_NAME, REF_REF_ENTITY_ID);
    refRefEntity.set(REF_REF_ATTR_VALUE_NAME, "value");
    Entity refEntity0 = new DynamicEntity(refEntityType);
    refEntity0.set(REF_ATTR_ID_NAME, REF_ENTITY0_ID);
    refEntity0.set(REF_ATTR_VALUE_NAME, REF_ENTITY0_LABEL);
    refEntity0.set(REF_ATTR_REF_NAME, refRefEntity);
    Entity refEntity1 = new DynamicEntity(refEntityType);
    refEntity1.set(REF_ATTR_ID_NAME, REF_ENTITY1_ID);
    refEntity1.set(REF_ATTR_VALUE_NAME, REF_ENTITY1_LABEL);
    refEntity1.set(REF_ATTR_REF_NAME, refRefEntity);
    Entity entity = new DynamicEntity(entityType);
    // required
    entity.set(attrIdName, ENTITY_ID);
    entity.set(attrBoolName, true);
    entity.set(attrCategoricalName, refEntity0);
    entity.set(attrCategoricalMrefName, asList(refEntity0, refEntity1));
    entity.set(attrCompoundAttr0Name, "compoundAttr0Str");
    entity.set(attrCompoundCategorical, refEntity0);
    entity.set(attrCompoundAttrCompoundAttr0Name, "compoundAttrCompoundAttr0Str");
    entity.set(attrDateName, LocalDate.parse("2015-05-22"));
    entity.set(attrDateTimeName, Instant.parse("2015-05-22T06:12:13Z"));
    entity.set(attrDecimalName, 3.14);
    entity.set(attrEmailName, "my@mail.com");
    entity.set(attrEnumName, enum0);
    entity.set(attrHtmlName, "<h1>html</h1>");
    entity.set(attrHyperlinkName, "http://www.molgenis.org/");
    entity.set(attrIntName, 123);
    entity.set(attrLongName, Long.MAX_VALUE);
    entity.set(attrMrefName, asList(refEntity0, refEntity1));
    entity.set(attrScriptName, "print \"Hello world\"");
    entity.set(attrStringName, "str");
    String textValue = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam consectetur auctor lectus sed tincidunt. Fusce sodales quis mauris non aliquam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Integer maximus imperdiet velit quis vehicula. Mauris pulvinar amet.";
    entity.set(attrTextName, textValue);
    entity.set(attrXrefName, refEntity0);
    // optional
    entity.set(attrBoolOptionalName, null);
    entity.set(attrCategoricalOptionalName, null);
    entity.set(attrCategoricalMrefOptionalName, null);
    entity.set(attrCompoundAttr0OptionalName, null);
    entity.set(attrCompoundAttrCompoundAttr0OptionalName, null);
    entity.set(attrDateOptionalName, null);
    entity.set(attrDateTimeOptionalName, null);
    entity.set(attrDecimalOptionalName, null);
    entity.set(attrEmailOptionalName, null);
    entity.set(attrEnumOptionalName, null);
    entity.set(attrHtmlOptionalName, null);
    entity.set(attrHyperlinkOptionalName, null);
    entity.set(attrIntOptionalName, null);
    entity.set(attrLongOptionalName, null);
    entity.set(attrMrefOptionalName, null);
    entity.set(attrScriptOptionalName, null);
    entity.set(attrStringOptionalName, null);
    entity.set(attrTextOptionalName, null);
    entity.set(attrXrefOptionalName, null);
    Query<Entity> q = new QueryImpl<>().offset(0).pageSize(100);
    when(dataService.findOneById(ENTITY_NAME, ENTITY_ID)).thenReturn(entity);
    when(dataService.findOneById(eq(ENTITY_NAME), eq(ENTITY_ID), any(Fetch.class))).thenReturn(entity);
    when(dataService.findOneById(eq(SELF_REF_ENTITY_NAME), eq("0"), any(Fetch.class))).thenReturn(selfRefEntity);
    when(dataService.count(ENTITY_NAME, new QueryImpl<>())).thenReturn(2L);
    when(dataService.findAll(ENTITY_NAME, q)).thenReturn(Stream.of(entity));
    when(dataService.findAll(REF_ENTITY_NAME)).thenAnswer(invocation -> Stream.of(refEntity0, refEntity1));
    when(dataService.findOneById(REF_ENTITY_NAME, REF_ENTITY0_ID)).thenReturn(refEntity0);
    when(dataService.findOneById(REF_ENTITY_NAME, REF_ENTITY1_ID)).thenReturn(refEntity1);
    when(dataService.findOneById(REF_REF_ENTITY_NAME, REF_REF_ENTITY_ID)).thenReturn(refRefEntity);
    when(dataService.getEntityType(ENTITY_NAME)).thenReturn(entityType);
    when(dataService.getEntityType(REF_ENTITY_NAME)).thenReturn(refEntityType);
    when(dataService.getEntityType(REF_REF_ENTITY_NAME)).thenReturn(refRefEntityType);
    when(dataService.getEntityType(SELF_REF_ENTITY_NAME)).thenReturn(selfRefEntityType);
    assertEquals(entity.getIdValue(), ENTITY_ID);
    assertEquals(refEntity0.getIdValue(), REF_ENTITY0_ID);
    assertEquals(refEntity0.getLabelValue(), REF_ENTITY0_LABEL);
    assertEquals(refEntity1.getIdValue(), REF_ENTITY1_ID);
    assertEquals(refEntity1.getLabelValue(), REF_ENTITY1_LABEL);
    assertEquals(refRefEntity.getIdValue(), REF_REF_ENTITY_ID);
    assertEquals(selfRefEntity.getIdValue(), "0");
    when(entityManager.create(entityType, POPULATE)).thenAnswer(invocation -> new DynamicEntity(entityType));
    when(localeResolver.resolveLocale(any())).thenReturn(Locale.ENGLISH);
    mockMvc = MockMvcBuilders.standaloneSetup(restControllerV2).setLocaleResolver(localeResolver).setMessageConverters(gsonHttpMessageConverter).setControllerAdvice(new GlobalControllerExceptionHandler(), new SpringExceptionHandler(), new FallbackExceptionHandler()).setConversionService(conversionService).build();
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) QueryImpl(org.molgenis.data.support.QueryImpl) DynamicEntity(org.molgenis.data.support.DynamicEntity) SpringExceptionHandler(org.molgenis.web.exception.SpringExceptionHandler) GlobalControllerExceptionHandler(org.molgenis.web.exception.GlobalControllerExceptionHandler) FallbackExceptionHandler(org.molgenis.web.exception.FallbackExceptionHandler)

Aggregations

FallbackExceptionHandler (org.molgenis.web.exception.FallbackExceptionHandler)3 GlobalControllerExceptionHandler (org.molgenis.web.exception.GlobalControllerExceptionHandler)3 SpringExceptionHandler (org.molgenis.web.exception.SpringExceptionHandler)3 DynamicEntity (org.molgenis.data.support.DynamicEntity)2 BeforeMethod (org.testng.annotations.BeforeMethod)2 CsvHttpMessageConverter (org.molgenis.core.ui.messageconverter.CsvHttpMessageConverter)1 Attribute (org.molgenis.data.meta.model.Attribute)1 EntityType (org.molgenis.data.meta.model.EntityType)1 QueryImpl (org.molgenis.data.support.QueryImpl)1 TokenExtractor (org.molgenis.security.token.TokenExtractor)1