| /** |
| * Copyright (c) 2016 Codetrails GmbH. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| */ |
| package org.eclipse.epp.internal.logging.aeri.ide; |
| |
| import static org.hamcrest.Matchers.is; |
| import static org.junit.Assert.assertThat; |
| import static org.mockito.Matchers.any; |
| import static org.mockito.Mockito.mock; |
| import static org.mockito.Mockito.when; |
| |
| import java.util.Arrays; |
| import java.util.Collection; |
| import java.util.List; |
| |
| import org.eclipse.e4.core.contexts.IEclipseContext; |
| import org.eclipse.e4.core.services.events.IEventBroker; |
| import org.eclipse.epp.internal.logging.aeri.ide.notifications.MylynNotificationsSupport; |
| import org.eclipse.epp.internal.logging.aeri.ide.processors.ReportProcessorSafeWrapper; |
| import org.eclipse.epp.internal.logging.aeri.ide.utils.UploadReportsScheduler; |
| import org.eclipse.epp.logging.aeri.core.IModelFactory; |
| import org.eclipse.epp.logging.aeri.core.IProblemState; |
| import org.eclipse.epp.logging.aeri.core.IReportProcessor; |
| import org.eclipse.epp.logging.aeri.core.ISystemSettings; |
| import org.junit.Before; |
| import org.junit.Test; |
| import org.junit.runner.RunWith; |
| import org.junit.runners.Parameterized; |
| import org.junit.runners.Parameterized.Parameter; |
| import org.junit.runners.Parameterized.Parameters; |
| import org.mockito.Mockito; |
| |
| import com.google.common.collect.Lists; |
| |
| @RunWith(Parameterized.class) |
| public class IDEWorkflowTest { |
| |
| private IModelFactory mf = IModelFactory.eINSTANCE; |
| private IIdeFactory idf = IIdeFactory.eINSTANCE; |
| |
| private IDEWorkflow sut; |
| private List<IServerDescriptor> endpoints; |
| private ILogEventsQueue queue; |
| private ISystemSettings settings; |
| private IEventBroker broker; |
| private MylynNotificationsSupport notificationSupport; |
| private UploadReportsScheduler uploadService; |
| private IEclipseContext context; |
| private List<IProcessorDescriptor> descriptors; |
| private IServerDescriptor serverDescriptor; |
| private IProcessorDescriptor processorDescriptor; |
| private ILogEventGroup group; |
| private ILogEvent event; |
| |
| @Parameters |
| public static Collection<Object[]> data() { |
| return Arrays.asList(new Object[][] { |
| // @formatter:off |
| //enable a processor only if the processor is automatic, can contribute and either wants to contribute or is requested |
| { isAutomatic(true), canContribute(true), wantsToContribute(true), isRequested(true), expected(true) }, |
| { isAutomatic(true), canContribute(true), wantsToContribute(true), isRequested(false), expected(true) }, |
| { isAutomatic(true), canContribute(true), wantsToContribute(false), isRequested(true), expected(true) }, |
| //do nothing in all other cases |
| { isAutomatic(true), canContribute(true), wantsToContribute(false), isRequested(false), expected(false) }, |
| { isAutomatic(true), canContribute(false), wantsToContribute(true), isRequested(true), expected(false) }, |
| { isAutomatic(true), canContribute(false), wantsToContribute(true), isRequested(false), expected(false) }, |
| { isAutomatic(true), canContribute(false), wantsToContribute(false), isRequested(true), expected(false) }, |
| { isAutomatic(true), canContribute(false), wantsToContribute(false), isRequested(false), expected(false) }, |
| { isAutomatic(false), canContribute(true), wantsToContribute(true), isRequested(true), expected(false) }, |
| { isAutomatic(false), canContribute(true), wantsToContribute(true), isRequested(false), expected(false) }, |
| { isAutomatic(false), canContribute(true), wantsToContribute(false), isRequested(true), expected(false) }, |
| { isAutomatic(false), canContribute(true), wantsToContribute(false), isRequested(false), expected(false) }, |
| { isAutomatic(false), canContribute(false), wantsToContribute(true), isRequested(true), expected(false) }, |
| { isAutomatic(false), canContribute(false), wantsToContribute(true), isRequested(false), expected(false) }, |
| { isAutomatic(false), canContribute(false), wantsToContribute(false), isRequested(true), expected(false) }, |
| { isAutomatic(false), canContribute(false), wantsToContribute(false), isRequested(false), expected(false) }, |
| // @formatter:on |
| }); |
| } |
| |
| @Parameter(0) |
| public boolean isAutomatic; |
| |
| @Parameter(1) |
| public boolean canContribute; |
| |
| @Parameter(2) |
| public boolean wantsToContribute; |
| |
| @Parameter(3) |
| public boolean isRequested; |
| |
| @Parameter(4) |
| public boolean expected; |
| |
| @Before |
| public void setUp() { |
| serverDescriptor = mock(IServerDescriptor.class); |
| Mockito.when(serverDescriptor.isConfigured()).thenReturn(true); |
| endpoints = Lists.newArrayList(serverDescriptor); |
| queue = idf.createLogEventsQueue(); |
| settings = mock(ISystemSettings.class); |
| Mockito.when(settings.isConfigured()).thenReturn(true); |
| broker = mock(IEventBroker.class); |
| notificationSupport = mock(MylynNotificationsSupport.class); |
| uploadService = mock(UploadReportsScheduler.class); |
| context = mock(IEclipseContext.class); |
| processorDescriptor = mock(IProcessorDescriptor.class); |
| when(processorDescriptor.getName()).thenReturn("Test Descriptor"); |
| when(processorDescriptor.getDirective()).thenReturn("testDirective"); |
| descriptors = Lists.newArrayList(processorDescriptor); |
| group = idf.createLogEventGroup(); |
| event = idf.createLogEvent(); |
| event.setServer(serverDescriptor); |
| IProblemState interest = mf.createProblemState(); |
| event.setInterest(interest); |
| event.setOptions(mf.createSendOptions()); |
| group.getEvents().add(event); |
| sut = new IDEWorkflow(endpoints, queue, settings, broker, notificationSupport, uploadService, context, descriptors); |
| } |
| |
| @Test |
| public void testProcessorEnabled() { |
| |
| when(processorDescriptor.isAutomatic()).thenReturn(isAutomatic); |
| |
| IReportProcessor processor = mock(IReportProcessor.class); |
| when(processor.canContribute(any(), any())).thenReturn(canContribute); |
| when(processor.wantsToContribute(any(), any())).thenReturn(wantsToContribute); |
| if (isRequested) { |
| event.getInterest().getNeedinfo().add(processorDescriptor.getDirective()); |
| } else { |
| event.getInterest().getNeedinfo().clear(); |
| } |
| ReportProcessorSafeWrapper safeProcessor = new ReportProcessorSafeWrapper(processor); |
| when(processorDescriptor.getProcessor()).thenReturn(safeProcessor); |
| |
| sut.onNewEventLogged(group); |
| |
| assertThat(event.getOptions().getEnabledProcessors().contains(safeProcessor), is(expected)); |
| } |
| |
| private static boolean isAutomatic(boolean b) { |
| return b; |
| } |
| |
| private static boolean canContribute(boolean b) { |
| return b; |
| } |
| |
| private static boolean wantsToContribute(boolean b) { |
| return b; |
| } |
| |
| private static boolean isRequested(boolean b) { |
| return b; |
| } |
| |
| private static boolean expected(boolean b) { |
| return b; |
| } |
| |
| } |