Example usage for org.apache.wicket.protocol.http.mock MockServletContext setAttribute

List of usage examples for org.apache.wicket.protocol.http.mock MockServletContext setAttribute

Introduction

In this page you can find the example usage for org.apache.wicket.protocol.http.mock MockServletContext setAttribute.

Prototype

@Override
public void setAttribute(final String name, final Object o) 

Source Link

Document

Set an attribute.

Usage

From source file:org.artifactory.webapp.wicket.page.admin.AbstractWicketTest.java

License:Open Source License

@BeforeClass
public void setUp() throws Exception {
    //Creates a new application context mock.
    applicationContextMock = new ApplicationContextMock();

    artifactoryHomeBoundTest = new ArtifactoryHomeBoundTest();
    artifactoryHomeBoundTest.bindArtifactoryHome();

    dummyArtifactoryContext = new DummyArtifactoryContext(applicationContextMock);
    ArtifactoryContextThreadBinder.bind(dummyArtifactoryContext);

    configService = mock(CentralConfigService.class);
    when(configService.getVersionInfo()).thenReturn(new VersionInfo(
            ConstantValues.artifactoryVersion.getString(), ConstantValues.artifactoryRevision.getString()));
    CentralConfigDescriptorImpl configDescriptor = new CentralConfigDescriptorImpl();
    configDescriptor.setFooter("bla");
    when(configService.getDescriptor()).thenReturn(configDescriptor);
    addMock("centralConfig", configService);

    AuthenticationManager authenticationManager = mock(AuthenticationManager.class);
    //For authentication, use mock like this:
    //Authentication authentication = mock(Authentication.class);
    //when(authentication.isAuthenticated()).thenReturn(true);
    //when(authentication.getPrincipal()).thenReturn("admin");
    //when(authentication.getAuthorities()).thenReturn(SimpleUser.ADMIN_GAS);

    //or real authentication like this:
    //((ArtifactoryWebSession) getTester().getSession()).signIn("admin", "password");
    when(authenticationManager.authenticate(any(Authentication.class)))
            .thenReturn(new SystemAuthenticationToken());
    addMock("authenticationManager", authenticationManager);

    AddonsManager addonsManager = mock(AddonsManager.class);
    when(addonsManager.isInstantiationAuthorized((Class) notNull())).thenReturn(true);
    when(addonsManager.addonByType(WebApplicationAddon.class)).thenReturn(new WicketAddonsImpl());
    when(addonsManager.addonByType(SearchAddon.class)).thenReturn(new WicketAddonsImpl());
    when(addonsManager.addonByType(PropertiesWebAddon.class)).thenReturn(new WicketAddonsImpl());
    when(addonsManager.addonByType(LicensesWebAddon.class)).thenReturn(new WicketAddonsImpl());
    when(addonsManager.addonByType(BlackDuckWebAddon.class)).thenReturn(new WicketAddonsImpl());
    when(addonsManager.addonByType(HaWebAddon.class)).thenReturn(new WicketAddonsImpl());
    when(addonsManager.addonByType(CrowdWebAddon.class)).thenReturn(new WicketAddonsImpl());
    when(addonsManager.addonByType(SamlWebAddon.class)).thenReturn(new WicketAddonsImpl());
    when(addonsManager.addonByType(HttpSsoAddon.class)).thenReturn(new WicketAddonsImpl());
    when(addonsManager.addonByType(WebstartWebAddon.class)).thenReturn(new WicketAddonsImpl());
    when(addonsManager.addonByType(SamlAddon.class)).thenReturn(new WicketAddonsImpl());
    when(addonsManager.isLicenseInstalled()).thenReturn(true);
    when(addonsManager.addonByType(HaCommonAddon.class)).thenReturn(new CoreAddonsImpl());
    addMock("addonsManager", addonsManager);

    AuthorizationService authorizationService = mock(AuthorizationService.class);
    when(authorizationService.isAnonymous()).thenReturn(false);
    when(authorizationService.isAdmin()).thenReturn(true);
    addMock("authorizationService", authorizationService);

    AddonsWebManager addonsWebManager = mock(AddonsWebManager.class);
    addMock("AddonsWebManager", addonsWebManager);

    ArtifactoryServersCommonService serversCommonService = mock(ArtifactoryServersCommonService.class);
    addMock("serversService", serversCommonService);

    SecurityService securityService = mock(SecurityService.class);
    when(securityService.getUserLastLoginInfo("admin")).thenReturn(null);
    addMock("securityService", securityService);

    StorageService storageService = mock(StorageService.class);
    when(storageService.getStorageQuotaInfo(0)).thenReturn(null);
    addMock("storageService", storageService);

    //Creates a new WicketTester
    ArtifactoryApplication application = new ArtifactoryApplication();
    MockServletContext servletContext = new MockServletContext(application, null);
    servletContext.setAttribute(ArtifactoryContext.APPLICATION_CONTEXT_KEY, ContextHelper.get());
    tester = new WicketTester(application, servletContext);

    setupTest();/*  w w w . j  ava 2 s.  c o  m*/
}