Java tutorial
/* * Copyright 2017 Red Hat, Inc. and/or its affiliates * and other contributors as indicated by the @author tags. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.keycloak.quickstart; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.container.test.api.OperateOnDeployment; import org.jboss.arquillian.container.test.api.RunAsClient; import org.jboss.arquillian.drone.api.annotation.Drone; import org.jboss.arquillian.graphene.Graphene; import org.jboss.arquillian.graphene.page.Page; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.Filters; import org.jboss.shrinkwrap.api.GenericArchive; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.StringAsset; import org.jboss.shrinkwrap.api.importer.ExplodedImporter; import org.jboss.shrinkwrap.api.spec.WebArchive; import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.keycloak.test.TestsHelper; import org.keycloak.test.builders.ClientBuilder; import org.keycloak.test.page.IndexPage; import org.keycloak.test.page.LoginPage; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.TimeoutException; import org.openqa.selenium.support.ui.ExpectedConditions; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.TimeUnit; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.keycloak.test.TestsHelper.createClient; import static org.keycloak.test.TestsHelper.deleteRealm; import static org.keycloak.test.TestsHelper.importTestRealm; import static org.keycloak.test.builders.ClientBuilder.AccessType.BEARER_ONLY; import static org.keycloak.test.builders.ClientBuilder.AccessType.PUBLIC; import static org.keycloak.test.page.IndexPage.UNAUTHORIZED; /** * @author <a href="mailto:ssilvert@redhat.com">Stan Silvert</a> */ @RunWith(Arquillian.class) @RunAsClient public class ArquillianAngular2Test { private static final String WEBAPP_SRC = "src/main/webapp"; private static final String APP_NAME = "app-angular2"; private static final String APP_SERVICE = "service-jaxrs"; private static final String ROOT_URL = "http://127.0.0.1:8080/app-angular2"; @Page private IndexPage indexPage; @Page private LoginPage loginPage; static { try { importTestRealm("admin", "admin", "/quickstart-realm.json"); } catch (IOException e) { e.printStackTrace(); } } @Deployment(name = APP_SERVICE, order = 1, testable = false) public static Archive<?> createTestArchive1() throws IOException { return ShrinkWrap.createFromZipFile(WebArchive.class, new File("../service-jee-jaxrs/target/service.war")) .addAsWebInfResource( new StringAsset(createClient(ClientBuilder.create(APP_SERVICE).accessType(BEARER_ONLY))), "keycloak.json"); } @Deployment(name = APP_NAME, order = 2, testable = false) public static Archive<?> createTestArchive2() throws IOException { WebArchive war = ShrinkWrap.create(WebArchive.class, "app-angular2.war").addAsWebResource( new StringAsset(createClient(ClientBuilder.create(APP_NAME).rootUrl(ROOT_URL).accessType(PUBLIC))), "keycloak.json"); war.merge(ShrinkWrap.create(GenericArchive.class).as(ExplodedImporter.class).importDirectory(WEBAPP_SRC) .as(GenericArchive.class), "/", Filters.includeAll()); return war; } @Drone private WebDriver webDriver; @ArquillianResource @OperateOnDeployment(APP_NAME) private URL contextRoot; @AfterClass public static void cleanUp() throws IOException { deleteRealm("admin", "admin", TestsHelper.testRealm); } @Before public void setup() { webDriver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS); webDriver.navigate().to(contextRoot); waitNg2Init(); } @Test public void testSecuredResource() throws InterruptedException { try { indexPage.clickSecured(); assertTrue(Graphene.waitGui().withTimeout(60, TimeUnit.SECONDS).until( ExpectedConditions.textToBePresentInElementLocated(By.className("error"), UNAUTHORIZED))); } catch (Exception e) { debugTest(e); fail("Should display an error message"); } } @Test public void testAdminResource() { try { indexPage.clickAdmin(); assertTrue(Graphene.waitGui().withTimeout(60, TimeUnit.SECONDS) .until(ExpectedConditions.textToBePresentInElementLocated(By.id("message"), UNAUTHORIZED))); } catch (Exception e) { debugTest(e); fail("Should display an error message"); } } @Test public void testPublicResource() { try { indexPage.clickPublic(); assertTrue(Graphene.waitGui().withTimeout(60, TimeUnit.SECONDS).until( ExpectedConditions.textToBePresentInElementLocated(By.id("message"), "Message: public"))); } catch (Exception e) { debugTest(e); fail("Should display an error message"); } } @Test public void testAdminWithAuthAndRole() throws MalformedURLException, InterruptedException { try { indexPage.clickLogin(); loginPage.login("test-admin", "password"); waitNg2Init(); indexPage.clickAdmin(); assertTrue(Graphene.waitGui().withTimeout(60, TimeUnit.SECONDS).until( ExpectedConditions.textToBePresentInElementLocated(By.className("message"), "Message: admin"))); indexPage.clickLogout(); } catch (Exception e) { debugTest(e); fail("Should display logged in user"); } } @Test public void testUserWithAuthAndRole() throws MalformedURLException, InterruptedException { try { indexPage.clickLogin(); loginPage.login("alice", "password"); waitNg2Init(); indexPage.clickSecured(); assertTrue(Graphene.waitGui().withTimeout(60, TimeUnit.SECONDS).until( ExpectedConditions.textToBePresentInElementLocated(By.id("message"), "Message: secured"))); indexPage.clickLogout(); } catch (Exception e) { debugTest(e); fail("Should display logged in user"); } } private void waitNg2Init() { try { Graphene.waitModel().withTimeout(60, TimeUnit.SECONDS) .until(ExpectedConditions.presenceOfElementLocated(By.id("accountBtn"))); } catch (TimeoutException e) { debugTest(e); fail(); } } private void debugTest(Exception e) { System.out.println(webDriver.getPageSource()); e.printStackTrace(); } }