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();
}
Aggregations