KEYCLOAK-3729 Ability to run tests within Keycloak server

This commit is contained in:
Stian Thorgersen 2017-01-26 15:37:52 +01:00
parent 3a366dd0a6
commit 5fd3eb2990
22 changed files with 561 additions and 49 deletions

View file

@ -77,6 +77,10 @@ public class JsonSerialization {
return readValue(bytes, type, false);
}
public static <T> T readValue(String string, TypeReference<T> type) throws IOException {
return mapper.readValue(string, type);
}
public static <T> T readValue(InputStream bytes, TypeReference<T> type) throws IOException {
return mapper.readValue(bytes, type);
}

View file

@ -56,6 +56,10 @@
<groupId>io.undertow</groupId>
<artifactId>undertow-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.core</groupId>
<artifactId>wildfly-controller</artifactId>
</dependency>
</dependencies>

View file

@ -17,13 +17,10 @@
package org.keycloak.testsuite.rest;
import org.infinispan.Cache;
import org.jboss.resteasy.annotations.cache.NoCache;
import org.jboss.resteasy.spi.BadRequestException;
import org.keycloak.common.util.MultivaluedHashMap;
import org.keycloak.common.util.Time;
import org.keycloak.component.ComponentModel;
import org.keycloak.connections.infinispan.InfinispanConnectionProvider;
import org.keycloak.events.Event;
import org.keycloak.events.EventQuery;
import org.keycloak.events.EventStoreProvider;
@ -62,6 +59,11 @@ import org.keycloak.testsuite.forms.PassThroughClientAuthenticator;
import org.keycloak.testsuite.rest.representation.AuthenticatorState;
import org.keycloak.testsuite.rest.resource.TestCacheResource;
import org.keycloak.testsuite.rest.resource.TestingExportImportResource;
import org.keycloak.testsuite.runonserver.ModuleUtil;
import org.keycloak.testsuite.runonserver.FetchOnServer;
import org.keycloak.testsuite.runonserver.RunOnServer;
import org.keycloak.testsuite.runonserver.SerializationUtil;
import org.keycloak.util.JsonSerialization;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
@ -82,8 +84,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.keycloak.exportimport.ExportImportConfig.PROVIDER;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
@ -636,21 +636,6 @@ public class TestingResourceProvider implements RealmResourceProvider {
return reps;
}
@GET
@Path("/component")
@Produces(MediaType.APPLICATION_JSON)
public MultivaluedHashMap<String, String> getComponentConfig(@QueryParam("componentId") String componentId) {
RealmModel realm = session.getContext().getRealm();
return realm.getComponent(componentId).getConfig();
}
@GET
@Path("/smtp-config")
@Produces(MediaType.APPLICATION_JSON)
public Map<String, String> getSmtpConfig() {
return session.getContext().getRealm().getSmtpConfig();
}
@GET
@Path("/identity-config")
@Produces(MediaType.APPLICATION_JSON)
@ -665,6 +650,29 @@ public class TestingResourceProvider implements RealmResourceProvider {
System.setProperty("java.security.krb5.conf", krb5ConfFile);
}
@POST
@Path("/run-on-server")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String runOnServer(String runOnServer) throws Exception {
try {
ClassLoader cl = ModuleUtil.isModules() ? ModuleUtil.getClassLoader() : getClass().getClassLoader();
Object r = SerializationUtil.decode(runOnServer, cl);
if (r instanceof FetchOnServer) {
Object result = ((FetchOnServer) r).run(session);
return result != null ? JsonSerialization.writeValueAsString(result) : null;
} else if (r instanceof RunOnServer) {
((RunOnServer) r).run(session);
return null;
} else {
throw new IllegalArgumentException();
}
} catch (Throwable t) {
return SerializationUtil.encodeException(t);
}
}
private RealmModel getRealmByName(String realmName) {
RealmProvider realmProvider = session.getProvider(RealmProvider.class);
return realmProvider.getRealmByName(realmName);

View file

@ -0,0 +1,32 @@
/*
* Copyright 2016 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.testsuite.runonserver;
import org.keycloak.models.KeycloakSession;
import java.io.Serializable;
import java.util.function.Function;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public interface FetchOnServer extends Serializable {
Object run(KeycloakSession session);
}

View file

@ -0,0 +1,12 @@
package org.keycloak.testsuite.runonserver;
/**
* Created by st on 26.01.17.
*/
public interface FetchOnServerWrapper<T> {
FetchOnServer getRunOnServer();
Class<T> getResultClass();
}

View file

@ -0,0 +1,35 @@
package org.keycloak.testsuite.runonserver;
import org.jboss.modules.Module;
import org.jboss.modules.ModuleIdentifier;
/**
* Created by st on 26.01.17.
*/
public class ModuleUtil {
private static boolean modules;
static {
try {
Module.getContextModuleLoader().loadModule(ModuleIdentifier.fromString("org.wildfly.common"));
modules = true;
} catch (Throwable t) {
modules = false;
}
}
public static boolean isModules() {
return modules;
}
public static ClassLoader getClassLoader() {
try {
Module m = Module.getContextModuleLoader().loadModule(ModuleIdentifier.fromString("deployment.run-on-server-classes.war"));
return m.getClassLoader();
} catch (Exception e) {
throw new RuntimeException("Failed to load 'deployment.run-on-server-classes.war', did you include RunOnServerDeployment?", e);
}
}
}

View file

@ -0,0 +1,44 @@
package org.keycloak.testsuite.runonserver;
import org.keycloak.models.utils.ModelToRepresentation;
import org.keycloak.representations.idm.ComponentRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
/**
* Created by st on 26.01.17.
*/
public class RunHelpers {
public static FetchOnServerWrapper<RealmRepresentation> internalRealm() {
return new FetchOnServerWrapper() {
@Override
public FetchOnServer getRunOnServer() {
return (FetchOnServer) session -> ModelToRepresentation.toRepresentation(session.getContext().getRealm(), true);
}
@Override
public Class<RealmRepresentation> getResultClass() {
return RealmRepresentation.class;
}
};
}
public static FetchOnServerWrapper<ComponentRepresentation> internalComponent(String componentId) {
return new FetchOnServerWrapper() {
@Override
public FetchOnServer getRunOnServer() {
return (FetchOnServer) session -> ModelToRepresentation.toRepresentation(session, session.getContext().getRealm().getComponent(componentId), true);
}
@Override
public Class<ComponentRepresentation> getResultClass() {
return ComponentRepresentation.class;
}
};
}
}

View file

@ -0,0 +1,31 @@
/*
* Copyright 2016 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.testsuite.runonserver;
import org.keycloak.models.KeycloakSession;
import java.io.Serializable;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public interface RunOnServer extends Serializable {
void run(KeycloakSession session);
}

View file

@ -0,0 +1,12 @@
package org.keycloak.testsuite.runonserver;
/**
* Created by st on 26.01.17.
*/
public class RunOnServerException extends RuntimeException {
public RunOnServerException(Throwable throwable) {
super(throwable);
}
}

View file

@ -0,0 +1,70 @@
package org.keycloak.testsuite.runonserver;
import org.keycloak.common.util.Base64;
import java.io.*;
/**
* Created by st on 26.01.17.
*/
public class SerializationUtil {
public static String encode(Object function) {
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(function);
oos.close();
return Base64.encodeBytes(os.toByteArray());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Object decode(String encoded, ClassLoader classLoader) {
try {
byte[] bytes = Base64.decode(encoded);
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(is) {
@Override
protected Class<?> resolveClass(ObjectStreamClass c) throws IOException, ClassNotFoundException {
try {
return Class.forName(c.getName(), false, classLoader);
} catch (ClassNotFoundException e) {
throw e;
}
}
};
return ois.readObject();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String encodeException(Throwable t) {
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(t);
oos.close();
return Base64.encodeBytes(os.toByteArray());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Throwable decodeException(String result) {
try {
byte[] bytes = Base64.decode(result);
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(is);
return (Throwable) ois.readObject();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View file

@ -38,5 +38,6 @@
<module name="javax.persistence.api"/>
<module name="org.hibernate"/>
<module name="org.javassist"/>
<module name="org.jboss.modules"/>
</dependencies>
</module>

View file

@ -71,6 +71,11 @@ public class DeploymentArchiveProcessor implements ApplicationArchiveProcessor {
@Override
public void process(Archive<?> archive, TestClass testClass) {
// Ignore run on server classes
if (archive.getName().equals("run-on-server-classes.war")) {
return;
}
log.info("Processing archive " + archive.getName());
// if (isAdapterTest(testClass)) {
modifyAdapterConfigs(archive, testClass);

View file

@ -23,11 +23,14 @@ import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import org.keycloak.testsuite.client.resources.TestApplicationResource;
import org.keycloak.testsuite.client.resources.TestExampleCompanyResource;
import org.keycloak.testsuite.client.resources.TestingResource;
import org.keycloak.testsuite.runonserver.*;
import org.keycloak.util.JsonSerialization;
/**
* @author <a href="mailto:mstrukel@redhat.com">Marko Strukelj</a>
*/
public class KeycloakTestingClient {
private final ResteasyWebTarget target;
private final ResteasyClient client;
@ -52,6 +55,67 @@ public class KeycloakTestingClient {
public TestExampleCompanyResource testExampleCompany() { return target.proxy(TestExampleCompanyResource.class); }
public Server server() {
return new Server("master");
}
public Server server(String realm) {
return new Server(realm);
}
public class Server {
private String realm;
public Server(String realm) {
this.realm = realm;
}
public <T> T fetch(FetchOnServerWrapper<T> wrapper) throws RunOnServerException {
return fetch(wrapper.getRunOnServer(), wrapper.getResultClass());
}
public <T> T fetch(FetchOnServer function, Class<T> clazz) throws RunOnServerException {
try {
String s = fetch(function);
return JsonSerialization.readValue(s, clazz);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public String fetch(FetchOnServer function) throws RunOnServerException {
String encoded = SerializationUtil.encode(function);
String result = testing(realm != null ? realm : "master").runOnServer(encoded);
if (result != null && !result.isEmpty() && !result.trim().startsWith("{")) {
Throwable t = SerializationUtil.decodeException(result);
if (t instanceof AssertionError) {
throw (AssertionError) t;
} else {
throw new RunOnServerException(t);
}
} else {
return result;
}
}
public void run(RunOnServer function) throws RunOnServerException {
String encoded = SerializationUtil.encode(function);
String result = testing(realm != null ? realm : "master").runOnServer(encoded);
if (result != null && !result.isEmpty() && !result.trim().startsWith("{")) {
Throwable t = SerializationUtil.decodeException(result);
if (t instanceof AssertionError) {
throw (AssertionError) t;
} else {
throw new RunOnServerException(t);
}
}
}
}
public void close() {
client.close();
}

View file

@ -242,23 +242,20 @@ public interface TestingResource {
@Produces(MediaType.APPLICATION_JSON)
Map<String, TestProvider.DetailsRepresentation> getTestComponentDetails();
@GET
@Path("/smtp-config")
@Produces(MediaType.APPLICATION_JSON)
Map<String, String> getSmtpConfig();
@GET
@Path("/identity-config")
@Produces(MediaType.APPLICATION_JSON)
Map<String, String> getIdentityProviderConfig(@QueryParam("alias") String alias);
@GET
@Path("/component")
@Produces(MediaType.APPLICATION_JSON)
MultivaluedHashMap<String, String> getComponentConfig(@QueryParam("componentId") String componentId);
@PUT
@Path("/set-krb5-conf-file")
@Consumes(MediaType.APPLICATION_JSON)
void setKrb5ConfFile(@QueryParam("krb5-conf-file") String krb5ConfFile);
@POST
@Path("/run-on-server")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
String runOnServer(String runOnServer);
}

View file

@ -129,8 +129,6 @@ public class IdentityProviderTest extends AbstractAdminTest {
assertFalse(representation.isStoreToken());
assertFalse(representation.isTrustEmail());
testingClient.testing("admin-client-test").getSmtpConfig();
assertEquals("some secret value", testingClient.testing("admin-client-test").getIdentityProviderConfig("new-identity-provider").get("clientSecret"));
IdentityProviderRepresentation rep = realm.identityProviders().findAll().stream().filter(i -> i.getAlias().equals("new-identity-provider")).findFirst().get();

View file

@ -19,12 +19,13 @@ package org.keycloak.testsuite.admin.realm;
import org.apache.commons.io.IOUtils;
import org.hamcrest.Matchers;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Rule;
import org.junit.Test;
import org.keycloak.OAuth2Constants;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.resource.ServerInfoResource;
import org.keycloak.common.util.StreamUtil;
import org.keycloak.common.util.Time;
import org.keycloak.events.admin.OperationType;
import org.keycloak.events.admin.ResourceType;
@ -34,7 +35,6 @@ import org.keycloak.representations.adapters.action.PushNotBeforeAction;
import org.keycloak.representations.idm.AdminEventRepresentation;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.ComponentRepresentation;
import org.keycloak.representations.idm.CredentialRepresentation;
import org.keycloak.representations.idm.EventRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.RoleRepresentation;
@ -45,6 +45,9 @@ import org.keycloak.testsuite.admin.AbstractAdminTest;
import org.keycloak.testsuite.admin.ApiUtil;
import org.keycloak.testsuite.arquillian.AuthServerTestEnricher;
import org.keycloak.testsuite.auth.page.AuthRealm;
import org.keycloak.testsuite.client.KeycloakTestingClient;
import org.keycloak.testsuite.runonserver.RunOnServerDeployment;
import org.keycloak.testsuite.runonserver.RunHelpers;
import org.keycloak.testsuite.util.AdminEventPaths;
import org.keycloak.testsuite.util.CredentialBuilder;
import org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse;
@ -52,7 +55,6 @@ import org.keycloak.testsuite.util.RealmBuilder;
import org.keycloak.testsuite.util.UserBuilder;
import org.keycloak.util.JsonSerialization;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Response;
import java.io.IOException;
@ -66,7 +68,6 @@ import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@ -77,6 +78,11 @@ import static org.junit.Assert.fail;
*/
public class RealmTest extends AbstractAdminTest {
@Deployment
public static WebArchive deploy() {
return RunOnServerDeployment.create();
}
@Rule
public AssertEvents events = new AssertEvents(this);
@ -140,7 +146,10 @@ public class RealmTest extends AbstractAdminTest {
RealmRepresentation returned = adminClient.realm("realm-with-smtp").toRepresentation();
assertEquals(ComponentRepresentation.SECRET_VALUE, returned.getSmtpServer().get("password"));
assertEquals("secret", testingClient.testing("realm-with-smtp").getSmtpConfig().get("password"));
KeycloakTestingClient.Server serverClient = testingClient.server("realm-with-smtp");
RealmRepresentation internalRep = serverClient.fetch(RunHelpers.internalRealm());
assertEquals("secret", internalRep.getSmtpServer().get("password"));
adminClient.realm("realm-with-smtp").update(rep);
@ -148,7 +157,8 @@ public class RealmTest extends AbstractAdminTest {
assertFalse(event.getRepresentation().contains("some secret value!!"));
assertTrue(event.getRepresentation().contains(ComponentRepresentation.SECRET_VALUE));
assertEquals("secret", testingClient.testing("realm-with-smtp").getSmtpConfig().get("password"));
internalRep = serverClient.fetch(RunHelpers.internalRealm());
assertEquals("secret", internalRep.getSmtpServer().get("password"));
RealmRepresentation realm = adminClient.realms().findAll().stream().filter(r -> r.getRealm().equals("realm-with-smtp")).findFirst().get();
assertEquals(ComponentRepresentation.SECRET_VALUE, realm.getSmtpServer().get("password"));

View file

@ -17,16 +17,15 @@
package org.keycloak.testsuite.keys;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.graphene.page.Page;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Rule;
import org.junit.Test;
import org.keycloak.common.util.Base64Url;
import org.keycloak.common.util.MultivaluedHashMap;
import org.keycloak.common.util.PemUtils;
import org.keycloak.jose.jws.AlgorithmType;
import org.keycloak.jose.jws.crypto.HMACProvider;
import org.keycloak.keys.GeneratedHmacKeyProviderFactory;
import org.keycloak.keys.GeneratedRsaKeyProviderFactory;
import org.keycloak.keys.KeyProvider;
import org.keycloak.representations.idm.ComponentRepresentation;
import org.keycloak.representations.idm.ErrorRepresentation;
@ -37,9 +36,10 @@ import org.keycloak.testsuite.AssertEvents;
import org.keycloak.testsuite.admin.ApiUtil;
import org.keycloak.testsuite.pages.AppPage;
import org.keycloak.testsuite.pages.LoginPage;
import org.keycloak.testsuite.runonserver.RunHelpers;
import org.keycloak.testsuite.runonserver.RunOnServerDeployment;
import javax.ws.rs.core.Response;
import java.security.interfaces.RSAPublicKey;
import java.util.List;
import static org.junit.Assert.*;
@ -50,6 +50,11 @@ import static org.keycloak.testsuite.admin.AbstractAdminTest.loadJson;
*/
public class GeneratedHmacKeyProviderTest extends AbstractKeycloakTest {
@Deployment
public static WebArchive deploy() {
return RunOnServerDeployment.create();
}
@Rule
public AssertEvents events = new AssertEvents(this);
@ -94,7 +99,8 @@ public class GeneratedHmacKeyProviderTest extends AbstractKeycloakTest {
assertEquals(AlgorithmType.HMAC.name(), key.getType());
assertEquals(priority, key.getProviderPriority());
assertEquals(32, Base64Url.decode(testingClient.testing("test").getComponentConfig(id).getFirst("secret")).length);
ComponentRepresentation component = testingClient.server("test").fetch(RunHelpers.internalComponent(id));
assertEquals(32, Base64Url.decode(component.getConfig().getFirst("secret")).length);
}
@Test
@ -127,7 +133,8 @@ public class GeneratedHmacKeyProviderTest extends AbstractKeycloakTest {
assertEquals(AlgorithmType.HMAC.name(), key.getType());
assertEquals(priority, key.getProviderPriority());
assertEquals(512, Base64Url.decode(testingClient.testing("test").getComponentConfig(id).getFirst("secret")).length);
ComponentRepresentation component = testingClient.server("test").fetch(RunHelpers.internalComponent(id));
assertEquals(512, Base64Url.decode(component.getConfig().getFirst("secret")).length);
}
@Test
@ -141,13 +148,15 @@ public class GeneratedHmacKeyProviderTest extends AbstractKeycloakTest {
Response response = adminClient.realm("test").components().add(rep);
String id = ApiUtil.getCreatedId(response);
assertEquals(32, Base64Url.decode(testingClient.testing("test").getComponentConfig(id).getFirst("secret")).length);
ComponentRepresentation component = testingClient.server("test").fetch(RunHelpers.internalComponent(id));
assertEquals(32, Base64Url.decode(component.getConfig().getFirst("secret")).length);
ComponentRepresentation createdRep = adminClient.realm("test").components().component(id).toRepresentation();
createdRep.getConfig().putSingle("secretSize", "512");
adminClient.realm("test").components().component(id).update(createdRep);
assertEquals(512, Base64Url.decode(testingClient.testing("test").getComponentConfig(id).getFirst("secret")).length);
component = testingClient.server("test").fetch(RunHelpers.internalComponent(id));
assertEquals(512, Base64Url.decode(component.getConfig().getFirst("secret")).length);
}
@Test

View file

@ -21,6 +21,9 @@ import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.ws.rs.NotFoundException;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Before;
import org.junit.Test;
import org.keycloak.admin.client.resource.ClientResource;
@ -50,6 +53,8 @@ import org.keycloak.storage.UserStorageProvider;
import org.keycloak.testsuite.AbstractKeycloakTest;
import org.keycloak.testsuite.Assert;
import org.keycloak.testsuite.arquillian.migration.Migration;
import org.keycloak.testsuite.runonserver.RunHelpers;
import org.keycloak.testsuite.runonserver.RunOnServerDeployment;
import org.keycloak.testsuite.util.OAuthClient;
import static org.keycloak.testsuite.Assert.assertEquals;
@ -71,7 +76,12 @@ public class MigrationTest extends AbstractKeycloakTest {
private RealmResource migrationRealm2;
private RealmResource migrationRealm3;
private RealmResource masterRealm;
@Deployment
public static WebArchive deploy() {
return RunOnServerDeployment.create();
}
@Override
public void addTestRealms(List<RealmRepresentation> testRealms) {
log.info("Adding no test realms for migration test. Test realm should be migrated from previous vesrion.");
@ -187,7 +197,9 @@ public class MigrationTest extends AbstractKeycloakTest {
components = masterRealm.components().query(MASTER, KeyProvider.class.getName(), "rsa");
assertEquals(1, components.size());
assertEquals(expectedMasterRealmKey, testingClient.testing(MASTER).getComponentConfig(components.get(0).getId()).getFirst("privateKey"));
ComponentRepresentation component = testingClient.server(MASTER).fetch(RunHelpers.internalComponent(components.get(0).getId()));
assertEquals(expectedMasterRealmKey, component.getConfig().getFirst("privateKey"));
components = masterRealm.components().query(MASTER, KeyProvider.class.getName(), "hmac-generated");
assertEquals(1, components.size());
@ -197,7 +209,9 @@ public class MigrationTest extends AbstractKeycloakTest {
components = migrationRealm.components().query(MIGRATION, KeyProvider.class.getName(), "rsa");
assertEquals(1, components.size());
assertEquals(expectedMigrationRealmKey, testingClient.testing(MIGRATION).getComponentConfig(components.get(0).getId()).getFirst("privateKey"));
component = testingClient.server(MIGRATION).fetch(RunHelpers.internalComponent(components.get(0).getId()));
assertEquals(expectedMigrationRealmKey, component.getConfig().getFirst("privateKey"));
components = migrationRealm.components().query(MIGRATION, KeyProvider.class.getName(), "hmac-generated");
assertEquals(1, components.size());

View file

@ -0,0 +1,27 @@
package org.keycloak.testsuite.runonserver;
import org.keycloak.models.utils.ModelToRepresentation;
import org.keycloak.representations.idm.ComponentRepresentation;
/**
* Created by st on 26.01.17.
*/
public class InternalComponentRepresentation implements FetchOnServerWrapper<ComponentRepresentation> {
private final String componentId;
public InternalComponentRepresentation(String componentId) {
this.componentId = componentId;
}
@Override
public FetchOnServer getRunOnServer() {
return (FetchOnServer) session -> ModelToRepresentation.toRepresentation(session.getContext().getRealm(), true);
}
@Override
public Class<ComponentRepresentation> getResultClass() {
return ComponentRepresentation.class;
}
}

View file

@ -0,0 +1,23 @@
package org.keycloak.testsuite.runonserver;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.keycloak.testsuite.AbstractKeycloakTest;
import org.keycloak.testsuite.runonserver.RunOnServerException;
/**
* Created by st on 26.01.17.
*/
public class RunOnServerDeployment {
@Deployment
public static WebArchive create(Class<?> ... classes) {
return ShrinkWrap.create(WebArchive.class, "run-on-server-classes.war")
.addAsManifestResource("run-on-server-jboss-deployment-structure.xml","jboss-deployment-structure.xml")
.addClasses(classes)
.addClass(AbstractKeycloakTest.class)
.addClass(RunOnServerException.class);
}
}

View file

@ -0,0 +1,97 @@
/*
* Copyright 2016 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.testsuite.runonserver;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.ComparisonFailure;
import org.junit.Test;
import org.keycloak.models.ModelException;
import org.keycloak.models.RealmModel;
import org.keycloak.models.utils.ModelToRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.testsuite.AbstractKeycloakTest;
import java.io.IOException;
import java.util.List;
import static org.junit.Assert.*;
/**
* This checks running code on the server for tests works and is not a test of the actual server
*
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public class RunOnServerTest extends AbstractKeycloakTest {
@Deployment
public static WebArchive deploy() {
return RunOnServerDeployment.create(RunOnServerTest.class);
}
@Test
public void runOnServerRep() throws IOException {
final String realmName = "master";
RealmRepresentation realmRep = testingClient.server().fetch(session -> {
RealmModel master = session.realms().getRealm(realmName);
return ModelToRepresentation.toRepresentation(master, true);
}, RealmRepresentation.class);
assertEquals(realmName, realmRep.getRealm());
}
@Test
public void runOnServerHelpers() throws IOException {
RealmRepresentation realmRep = testingClient.server().fetch(RunHelpers.internalRealm());
assertEquals("master", realmRep.getRealm());
}
@Test
public void runOnServerNoResponse() throws IOException {
testingClient.server().run(session -> System.out.println("Hello world!"));
}
@Test
public void runOnServerAssertOnServer() throws IOException {
try {
testingClient.server().run(session -> assertEquals("foo", "bar"));
fail("Expected exception");
} catch (ComparisonFailure e) {
assertEquals("expected:<[foo]> but was:<[bar]>", e.getMessage());
}
}
@Test
public void runOnServerExceptionOnServer() throws IOException {
try {
testingClient.server().run(session -> {
throw new ModelException("Something went wrong");
});
fail("Expected exception");
} catch (RunOnServerException e) {
assertTrue(e.getCause() instanceof ModelException);
assertEquals("Something went wrong", e.getCause().getMessage());
}
}
@Override
public void addTestRealms(List<RealmRepresentation> testRealms) {
}
}

View file

@ -0,0 +1,15 @@
<?xml version="1.0"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
<deployment>
<dependencies>
<module name="org.keycloak.testsuite.integration-arquillian-testsuite-providers"/>
<module name="org.keycloak.keycloak-common"/>
<module name="org.keycloak.keycloak-core"/>
<module name="org.keycloak.keycloak-server-spi"/>
<module name="org.keycloak.keycloak-server-spi-private"/>
<module name="org.keycloak.keycloak-services"/>
<module name="org.keycloak.keycloak-model-infinispan"/>
<module name="org.keycloak.keycloak-model-jpa"/>
</dependencies>
</deployment>
</jboss-deployment-structure>