Search in sources :

Example 1 with TokenExtractor

use of org.molgenis.security.token.TokenExtractor 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)

Aggregations

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