Search in sources :

Example 1 with ModelValidationHelper

use of org.eclipse.vorto.repository.core.impl.utils.ModelValidationHelper in project vorto by eclipse.

the class UnitTestBase method mockServices.

protected void mockServices() throws Exception {
    ModelRepositoryEventListener supervisor = new ModelRepositoryEventListener();
    IndexingEventListener indexingSupervisor = new IndexingEventListener(indexingService);
    Collection<ApplicationListener<AppEvent>> listeners = new ArrayList<>();
    listeners.add(supervisor);
    listeners.add(indexingSupervisor);
    ApplicationEventPublisher eventPublisher = new MockAppEventPublisher(listeners);
    mockAccountService(eventPublisher);
    mockUserService(eventPublisher);
    mockModelParserFactory();
    mockModelRepositoryFactory(eventPublisher);
    supervisor.setRepositoryFactory(repositoryFactory);
    modelParserFactory.setModelRepositoryFactory(repositoryFactory);
    searchService = new SimpleSearchService(namespaceRepository, repositoryFactory);
    supervisor.setSearchService(searchService);
    this.modelValidationHelper = new ModelValidationHelper(repositoryFactory, this.accountService, userRepositoryRoleService, userNamespaceRoleService);
    mockImporter(repositoryFactory);
    this.workflow = new DefaultWorkflowService(repositoryFactory, accountService, notificationService, namespaceService, userNamespaceRoleService, roleService);
}
Also used : ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) ApplicationListener(org.springframework.context.ApplicationListener) ModelValidationHelper(org.eclipse.vorto.repository.core.impl.utils.ModelValidationHelper) MockAppEventPublisher(org.eclipse.vorto.repository.utils.MockAppEventPublisher) DefaultWorkflowService(org.eclipse.vorto.repository.workflow.impl.DefaultWorkflowService) ModelRepositoryEventListener(org.eclipse.vorto.repository.core.impl.ModelRepositoryEventListener) IndexingEventListener(org.eclipse.vorto.repository.search.IndexingEventListener) SimpleSearchService(org.eclipse.vorto.repository.search.impl.SimpleSearchService)

Example 2 with ModelValidationHelper

use of org.eclipse.vorto.repository.core.impl.utils.ModelValidationHelper in project vorto by eclipse.

the class SearchTestInfrastructure method setupMocking.

private void setupMocking() throws Exception {
    when(namespaceService.resolveWorkspaceIdForNamespace(anyString())).thenReturn(Optional.of("playground"));
    when(namespaceService.findNamespaceByWorkspaceId(anyString())).thenReturn(mockNamespace());
    when(namespaceRepository.findAll()).thenReturn(Lists.newArrayList(mockNamespace()));
    List<String> workspaceIds = new ArrayList<>();
    workspaceIds.add("playground");
    when(namespaceService.findAllWorkspaceIds()).thenReturn(workspaceIds);
    NamespaceRole namespace_admin = new NamespaceRole();
    namespace_admin.setName("namespace_admin");
    namespace_admin.setPrivileges(7);
    namespace_admin.setRole(32);
    NamespaceRole model_viewer = new NamespaceRole();
    model_viewer.setName("model_viewer");
    model_viewer.setPrivileges(1);
    model_viewer.setRole(1);
    NamespaceRole model_creator = new NamespaceRole();
    model_creator.setName("model_creator");
    model_creator.setPrivileges(3);
    model_creator.setRole(2);
    NamespaceRole model_promoter = new NamespaceRole();
    model_promoter.setName("model_promoter");
    model_promoter.setPrivileges(3);
    model_promoter.setRole(4);
    NamespaceRole model_publisher = new NamespaceRole();
    model_publisher.setName("model_publisher");
    model_publisher.setPrivileges(3);
    model_publisher.setRole(4);
    NamespaceRole model_reviewer = new NamespaceRole();
    model_reviewer.setName("model_reviewer");
    model_reviewer.setPrivileges(3);
    model_reviewer.setRole(8);
    Set<IRole> roles = new HashSet<>();
    roles.add(namespace_admin);
    roles.add(model_viewer);
    roles.add(model_creator);
    roles.add(model_promoter);
    roles.add(model_publisher);
    roles.add(model_reviewer);
    when(userNamespaceRoleService.getRoles(anyString(), anyString())).thenReturn(roles);
    when(userNamespaceRoleService.getRoles(any(User.class), any(Namespace.class))).thenReturn(roles);
    when(userNamespaceRoleService.getRolesByWorkspaceIdAndUser(anyString(), anyString())).thenAnswer(inv -> {
        if (inv.getArguments()[1].equals("namespace_admin")) {
            return Sets.newHashSet(namespace_admin);
        }
        if (inv.getArguments()[1].equals("viewer")) {
            return Sets.newHashSet(model_viewer);
        }
        if (inv.getArguments()[1].equals("creator")) {
            return Sets.newHashSet(model_creator);
        }
        if (inv.getArguments()[1].equals("promoter")) {
            return Sets.newHashSet(model_promoter);
        }
        if (inv.getArguments()[1].equals("publisher")) {
            return Sets.newHashSet(model_publisher);
        }
        if (inv.getArguments()[1].equals("reviewer")) {
            return Sets.newHashSet(model_reviewer);
        }
        return Sets.newHashSet(namespace_admin, model_viewer, model_creator, model_promoter, model_publisher, model_reviewer);
    });
    when(userNamespaceRoleService.getRolesByWorkspaceIdAndUser(anyString(), any(User.class))).thenReturn(roles);
    Set<Privilege> privileges = new HashSet<>(Arrays.asList(Privilege.DEFAULT_PRIVILEGES));
    when(privilegeService.getPrivileges(anyLong())).thenReturn(privileges);
    when(roleService.findAnyByName("model_viewer")).thenReturn(Optional.of(new NamespaceRole(1, "model_viewer", 1)));
    when(roleService.findAnyByName("model_creator")).thenReturn(Optional.of(new NamespaceRole(2, "model_creator", 3)));
    when(roleService.findAnyByName("model_promoter")).thenReturn(Optional.of(new NamespaceRole(4, "model_promoter", 3)));
    when(roleService.findAnyByName("model_reviewer")).thenReturn(Optional.of(new NamespaceRole(8, "model_reviewer", 3)));
    when(roleService.findAnyByName("model_publisher")).thenReturn(Optional.of(new NamespaceRole(16, "model_publisher", 3)));
    when(roleService.findAnyByName("namespace_admin")).thenReturn(Optional.of(new NamespaceRole(32, "namespace_admin", 7)));
    when(roleService.findAnyByName("sysadmin")).thenReturn(Optional.of(RepositoryRole.SYS_ADMIN));
    User alex = new UserBuilder().withName("alex").withAuthenticationProviderID("GITHUB").build();
    User erle = new UserBuilder().withName("erle").withAuthenticationProviderID("GITHUB").build();
    User admin = new UserBuilder().withName("admin").withAuthenticationProviderID("GITHUB").build();
    User creator = new UserBuilder().withName("creator").withAuthenticationProviderID("GITHUB").build();
    User promoter = new UserBuilder().withName("promoter").withAuthenticationProviderID("GITHUB").build();
    User reviewer = new UserBuilder().withName("reviewer").withAuthenticationProviderID("GITHUB").build();
    User publisher = new UserBuilder().withName("publisher").withAuthenticationProviderID("GITHUB").build();
    when(userRepository.findByUsername("alex")).thenReturn(alex);
    when(userRepository.findByUsername("erle")).thenReturn(erle);
    when(userRepository.findByUsername("admin")).thenReturn(admin);
    when(userRepository.findByUsername("creator")).thenReturn(creator);
    when(userRepository.findByUsername("promoter")).thenReturn(promoter);
    when(userRepository.findByUsername("reviewer")).thenReturn(reviewer);
    when(userRepository.findByUsername("publisher")).thenReturn(publisher);
    when(userRepository.findAll()).thenReturn(Lists.newArrayList(alex, erle, admin, creator, promoter, reviewer, publisher));
    when(userNamespaceRoleService.hasRole(anyString(), any(), any())).thenReturn(false);
    when(userNamespaceRoleService.hasRole(eq(alex), any(), eq(model_creator))).thenReturn(true);
    when(userNamespaceRoleService.hasRole(eq(alex), any(), eq(model_promoter))).thenReturn(true);
    when(userNamespaceRoleService.hasRole(eq(alex), any(), eq(model_reviewer))).thenReturn(true);
    when(userNamespaceRoleService.hasRole(eq(erle), any(), eq(model_creator))).thenReturn(true);
    when(userNamespaceRoleService.hasRole(eq(erle), any(), eq(model_promoter))).thenReturn(true);
    when(userNamespaceRoleService.hasRole(eq(erle), any(), eq(model_reviewer))).thenReturn(true);
    when(userNamespaceRoleService.hasRole(eq(erle), any(), eq(namespace_admin))).thenReturn(true);
    when(userNamespaceRoleService.hasRole(eq(admin), any(), eq(model_creator))).thenReturn(true);
    when(userNamespaceRoleService.hasRole(eq(admin), any(), eq(model_promoter))).thenReturn(true);
    when(userNamespaceRoleService.hasRole(eq(admin), any(), eq(model_reviewer))).thenReturn(true);
    when(userNamespaceRoleService.hasRole(eq(admin), any(), eq(namespace_admin))).thenReturn(true);
    when(userNamespaceRoleService.hasRole(eq(creator), any(), eq(model_creator))).thenReturn(true);
    when(userNamespaceRoleService.hasRole(eq(promoter), any(), eq(model_promoter))).thenReturn(true);
    when(userNamespaceRoleService.hasRole(eq(reviewer), any(), eq(model_reviewer))).thenReturn(true);
    when(userNamespaceRoleService.hasRole(eq(publisher), any(), eq(model_publisher))).thenReturn(true);
    ModelRepositoryEventListener supervisor = new ModelRepositoryEventListener();
    IndexingEventListener indexingSupervisor = new IndexingEventListener(indexingService);
    Collection<ApplicationListener<AppEvent>> listeners = new ArrayList<>();
    listeners.add(supervisor);
    listeners.add(indexingSupervisor);
    ApplicationEventPublisher eventPublisher = new MockAppEventPublisher(listeners);
    accountService = new DefaultUserAccountService(userRolesRequestCache, userRepository, userNamespaceRoleService);
    accountService.setApplicationEventPublisher(eventPublisher);
    modelParserFactory = new ModelParserFactory();
    modelParserFactory.init();
    RepositoryConfiguration config = null;
    config = RepositoryConfiguration.read(new ClassPathResource("vorto-repository.json").getPath());
    repositoryFactory = new ModelRepositoryFactory(modelSearchUtil, attachmentValidator, modelParserFactory, null, config, null, namespaceService, userNamespaceRoleService, privilegeService, userRepositoryRoleService, userRepository) {

        @Override
        public IModelRetrievalService getModelRetrievalService() {
            return super.getModelRetrievalService(createUserContext("admin"));
        }

        @Override
        public IModelRepository getRepository(String workspaceId) {
            return super.getRepository(createUserContext("admin", workspaceId));
        }

        @Override
        public IModelRepository getRepository(String workspaceId, Authentication user) {
            if (user == null) {
                return getRepository(workspaceId);
            }
            return super.getRepository(workspaceId, user);
        }
    };
    repositoryFactory.setApplicationEventPublisher(eventPublisher);
    repositoryFactory.start();
    supervisor.setRepositoryFactory(repositoryFactory);
    modelParserFactory.setModelRepositoryFactory(repositoryFactory);
    searchService = new SimpleSearchService(namespaceRepository, repositoryFactory);
    supervisor.setSearchService(searchService);
    modelValidationHelper = new ModelValidationHelper(repositoryFactory, accountService, userRepositoryRoleService, userNamespaceRoleService);
    importer = new VortoModelImporter();
    importer.setUploadStorage(new InMemoryTemporaryStorage());
    importer.setUserAccountService(accountService);
    importer.setModelParserFactory(modelParserFactory);
    importer.setModelRepoFactory(repositoryFactory);
    importer.setModelValidationHelper(modelValidationHelper);
    workflow = new DefaultWorkflowService(repositoryFactory, accountService, notificationService, namespaceService, userNamespaceRoleService, roleService);
    MockitoAnnotations.initMocks(this);
}
Also used : ModelParserFactory(org.eclipse.vorto.repository.core.impl.parser.ModelParserFactory) VortoModelImporter(org.eclipse.vorto.repository.importer.impl.VortoModelImporter) ModelValidationHelper(org.eclipse.vorto.repository.core.impl.utils.ModelValidationHelper) DefaultWorkflowService(org.eclipse.vorto.repository.workflow.impl.DefaultWorkflowService) IndexingEventListener(org.eclipse.vorto.repository.search.IndexingEventListener) IModelRepository(org.eclipse.vorto.repository.core.IModelRepository) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) RepositoryConfiguration(org.modeshape.jcr.RepositoryConfiguration) SimpleSearchService(org.eclipse.vorto.repository.search.impl.SimpleSearchService) DefaultUserAccountService(org.eclipse.vorto.repository.account.impl.DefaultUserAccountService) ClassPathResource(org.springframework.core.io.ClassPathResource) ModelRepositoryEventListener(org.eclipse.vorto.repository.core.impl.ModelRepositoryEventListener) IModelRetrievalService(org.eclipse.vorto.repository.core.IModelRetrievalService) ModelRepositoryFactory(org.eclipse.vorto.repository.core.impl.ModelRepositoryFactory) Authentication(org.springframework.security.core.Authentication) ApplicationListener(org.springframework.context.ApplicationListener) InMemoryTemporaryStorage(org.eclipse.vorto.repository.core.impl.InMemoryTemporaryStorage)

Aggregations

ModelRepositoryEventListener (org.eclipse.vorto.repository.core.impl.ModelRepositoryEventListener)2 ModelValidationHelper (org.eclipse.vorto.repository.core.impl.utils.ModelValidationHelper)2 IndexingEventListener (org.eclipse.vorto.repository.search.IndexingEventListener)2 SimpleSearchService (org.eclipse.vorto.repository.search.impl.SimpleSearchService)2 DefaultWorkflowService (org.eclipse.vorto.repository.workflow.impl.DefaultWorkflowService)2 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)2 ApplicationListener (org.springframework.context.ApplicationListener)2 DefaultUserAccountService (org.eclipse.vorto.repository.account.impl.DefaultUserAccountService)1 IModelRepository (org.eclipse.vorto.repository.core.IModelRepository)1 IModelRetrievalService (org.eclipse.vorto.repository.core.IModelRetrievalService)1 InMemoryTemporaryStorage (org.eclipse.vorto.repository.core.impl.InMemoryTemporaryStorage)1 ModelRepositoryFactory (org.eclipse.vorto.repository.core.impl.ModelRepositoryFactory)1 ModelParserFactory (org.eclipse.vorto.repository.core.impl.parser.ModelParserFactory)1 VortoModelImporter (org.eclipse.vorto.repository.importer.impl.VortoModelImporter)1 MockAppEventPublisher (org.eclipse.vorto.repository.utils.MockAppEventPublisher)1 RepositoryConfiguration (org.modeshape.jcr.RepositoryConfiguration)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 Authentication (org.springframework.security.core.Authentication)1