Skip to content

Commit

Permalink
#360 - Create stress/load test which should cover all APIs with mocke…
Browse files Browse the repository at this point in the history
…d OP
  • Loading branch information
duttarnab committed Aug 12, 2019
1 parent 52647ef commit c438b1b
Show file tree
Hide file tree
Showing 13 changed files with 341 additions and 14 deletions.
9 changes: 8 additions & 1 deletion oxd-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -442,6 +443,12 @@
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.19.0</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ protected Result check() {
}
});
environment.jersey().register(RolesAllowedDynamicFeature.class);
environment.jersey().register(new RestResource());
environment.jersey().register(ServerLauncher.getInjector().getInstance(RestResource.class));
}
}
5 changes: 3 additions & 2 deletions oxd-server/src/main/java/org/gluu/oxd/server/Processor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package org.gluu.oxd.server;

import com.google.inject.Inject;
import org.gluu.oxd.server.op.OpClientFactory;
import org.jboss.resteasy.client.ClientResponseFailure;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -36,10 +37,10 @@ public Processor(ValidationService validationService) {
this.validationService = validationService;
}

public IOpResponse process(Command command) {
public IOpResponse process(Command command, OpClientFactory opClientFactory) {
if (command != null) {
try {
final IOperation<IParams> operation = (IOperation<IParams>) OperationFactory.create(command, ServerLauncher.getInjector());
final IOperation<IParams> operation = (IOperation<IParams>) OperationFactory.create(command, ServerLauncher.getInjector(), opClientFactory);
if (operation != null) {
IParams iParams = Convertor.asParams(operation.getParameterClass(), command);
validationService.validate(iParams);
Expand Down
15 changes: 13 additions & 2 deletions oxd-server/src/main/java/org/gluu/oxd/server/RestResource.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.gluu.oxd.server;

import com.google.inject.Inject;
import org.apache.commons.lang.StringUtils;
import org.gluu.oxd.common.Command;
import org.gluu.oxd.common.CommandType;
import org.gluu.oxd.common.Jackson2;
import org.gluu.oxd.common.params.*;
import org.gluu.oxd.common.response.IOpResponse;
import org.gluu.oxd.common.response.POJOResponse;
import org.gluu.oxd.server.op.OpClientFactory;
import org.gluu.oxd.server.service.ConfigurationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -21,7 +23,12 @@ public class RestResource {

private static final Logger LOG = LoggerFactory.getLogger(RestResource.class);

public RestResource() {

private static OpClientFactory opClientFactory;

@Inject
public RestResource(OpClientFactory opClientFactory) {
this.opClientFactory = opClientFactory;
}

@GET
Expand Down Expand Up @@ -231,7 +238,7 @@ private static <T extends IParams> Object getObjectForJsonConversion(CommandType
((HasAccessTokenParams) params).setToken(validateAccessToken(authorization));
}
Command command = new Command(commandType, params);
final IOpResponse response = ServerLauncher.getInjector().getInstance(Processor.class).process(command);
final IOpResponse response = ServerLauncher.getInjector().getInstance(Processor.class).process(command, opClientFactory);
Object forJsonConversion = response;
if (response instanceof POJOResponse) {
forJsonConversion = ((POJOResponse) response).getNode();
Expand All @@ -255,4 +262,8 @@ private static String validateAccessToken(String authorization) {
LOG.debug("No access token provided in Authorization header. Forbidden.");
throw new WebApplicationException(Response.status(Response.Status.FORBIDDEN).build());
}

public void setOpClientFactory(OpClientFactory opClientFactory) {
this.opClientFactory = opClientFactory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
import com.google.inject.name.Names;
import org.gluu.oxd.server.OxdServerConfiguration;
import org.gluu.oxd.server.op.OpClientFactory;
import org.gluu.oxd.server.op.OpClientFactoryImpl;
import org.gluu.oxd.server.op.RegisterSiteOperation;
import org.gluu.oxd.server.persistence.H2PersistenceProvider;
import org.gluu.oxd.server.persistence.PersistenceService;
import org.gluu.oxd.server.persistence.PersistenceServiceImpl;
Expand Down Expand Up @@ -34,5 +38,6 @@ protected void configure() {
bind(DiscoveryService.class).in(Singleton.class);
bind(ValidationService.class).in(Singleton.class);
bind(StateService.class).in(Singleton.class);
bind(OpClientFactory.class).to(OpClientFactoryImpl.class).in(Singleton.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,24 @@ public class GetClientTokenOperation extends BaseOperation<GetClientTokenParams>

private static final Logger LOG = LoggerFactory.getLogger(GetClientTokenOperation.class);

private OpClientFactory opClientFactory;
/**
* Base constructor
*
* @param command command
*/
protected GetClientTokenOperation(Command command, final Injector injector) {
protected GetClientTokenOperation(Command command, final Injector injector, OpClientFactory opClientFactory) {
super(command, injector, GetClientTokenParams.class);
this.opClientFactory = opClientFactory;
}

@Override
public IOpResponse execute(GetClientTokenParams params) {
try {
final AuthenticationMethod authenticationMethod = AuthenticationMethod.fromString(params.getAuthenticationMethod());
final String tokenEndpoint = getDiscoveryService().getConnectDiscoveryResponse(params.getOpHost(), params.getOpDiscoveryPath()).getTokenEndpoint();
final TokenClient tokenClient = new TokenClient(tokenEndpoint);
//final TokenClient tokenClient = new TokenClient(tokenEndpoint);
final TokenClient tokenClient = opClientFactory.createTokenClient(tokenEndpoint);
tokenClient.setExecutor(getHttpService().getClientExecutor());

final TokenResponse tokenResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.gluu.oxd.server.op;

import org.gluu.oxauth.client.*;

public interface OpClientFactory {
public TokenClient createTokenClient(String url);

public UserInfoClient createUserInfoClient(String url);

public RegisterClient createRegisterClient(String url);

public OpenIdConfigurationClient createOpenIdConfigurationClient(String url);

public AuthorizeClient createAuthorizeClient(String url);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.gluu.oxd.server.op;

import org.gluu.oxauth.client.*;

public class OpClientFactoryImpl implements OpClientFactory {

public OpClientFactoryImpl() {
}

public TokenClient createTokenClient(String url) {
return new TokenClient(url);
}

public UserInfoClient createUserInfoClient(String url) {
return new UserInfoClient(url);
}

public RegisterClient createRegisterClient(String url) {
return new RegisterClient(url);
}

public OpenIdConfigurationClient createOpenIdConfigurationClient(String url) {
return new OpenIdConfigurationClient(url);
}

public AuthorizeClient createAuthorizeClient(String url) {
return new AuthorizeClient(url);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package org.gluu.oxd.server.op;

import com.google.inject.Injector;
import org.gluu.oxd.server.ServerLauncher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.gluu.oxd.common.Command;
Expand All @@ -21,7 +22,7 @@ public class OperationFactory {
private OperationFactory() {
}

public static IOperation<? extends IParams> create(Command command, final Injector injector) {
public static IOperation<? extends IParams> create(Command command, final Injector injector, OpClientFactory opClientFactory) {
if (command != null && command.getCommandType() != null) {
switch (command.getCommandType()) {
case AUTHORIZATION_CODE_FLOW:
Expand All @@ -43,7 +44,7 @@ public static IOperation<? extends IParams> create(Command command, final Inject
case GET_ACCESS_TOKEN_BY_REFRESH_TOKEN:
return new GetAccessTokenByRefreshTokenOperation(command, injector);
case REGISTER_SITE:
return new RegisterSiteOperation(command, injector);
return new RegisterSiteOperation(command, injector, opClientFactory);
case GET_AUTHORIZATION_CODE:
return new GetAuthorizationCodeOperation(command, injector);
case GET_LOGOUT_URI:
Expand All @@ -59,7 +60,7 @@ public static IOperation<? extends IParams> create(Command command, final Inject
case RP_GET_CLAIMS_GATHERING_URL:
return new RpGetGetClaimsGatheringUrlOperation(command, injector);
case GET_CLIENT_TOKEN:
return new GetClientTokenOperation(command, injector);
return new GetClientTokenOperation(command, injector, opClientFactory);
case INTROSPECT_ACCESS_TOKEN:
return new IntrospectAccessTokenOperation(command, injector);
case INTROSPECT_RPT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.gluu.oxauth.model.register.ApplicationType;
import org.gluu.oxauth.model.uma.UmaMetadata;
import org.gluu.oxd.common.Command;
import org.gluu.oxd.common.CommandType;
import org.gluu.oxd.common.ErrorResponseCode;
import org.gluu.oxd.common.params.RegisterSiteParams;
import org.gluu.oxd.common.response.IOpResponse;
Expand All @@ -39,20 +40,22 @@
/**
* @author Yuriy Zabrovarnyy
*/

public class RegisterSiteOperation extends BaseOperation<RegisterSiteParams> {

private static final Logger LOG = LoggerFactory.getLogger(RegisterSiteOperation.class);

private Rp rp;

private OpClientFactory opClientFactory;

/**
* Base constructor
*
* @param command command
*/
protected RegisterSiteOperation(Command command, final Injector injector) {
protected RegisterSiteOperation(Command command, final Injector injector, OpClientFactory opClientFactory) {
super(command, injector, RegisterSiteParams.class);
this.opClientFactory = opClientFactory;
}

public RegisterSiteResponse execute_(RegisterSiteParams params) {
Expand Down Expand Up @@ -245,7 +248,8 @@ private RegisterResponse registerClient(RegisterSiteParams params) {
throw new HttpException(ErrorResponseCode.NO_REGISTRATION_ENDPOINT);
}

final RegisterClient registerClient = new RegisterClient(registrationEndpoint);
//final RegisterClient registerClient = new RegisterClient(registrationEndpoint);
final RegisterClient registerClient = opClientFactory.createRegisterClient(registrationEndpoint);
registerClient.setRequest(createRegisterClientRequest(params));
registerClient.setExecutor(getHttpService().getClientExecutor());
final RegisterResponse response = registerClient.exec();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package org.gluu.oxd.server.mock.op;

import com.google.common.base.Preconditions;
import io.dropwizard.testing.ConfigOverride;
import io.dropwizard.testing.DropwizardTestSupport;
import io.dropwizard.testing.ResourceHelpers;
import org.apache.commons.lang.StringUtils;
import org.gluu.oxd.common.response.RegisterSiteResponse;
import org.gluu.oxd.server.*;
import org.gluu.oxd.server.persistence.PersistenceService;
import org.gluu.oxd.server.service.RpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Parameters;

/**
* Main class to set up and tear down suite.
*
* @author Yuriy Zabrovarnyy
* @version 0.9, 21/08/2013
*/

public class MockSetUpTest {

private static final Logger LOG = LoggerFactory.getLogger(SetUpTest.class);

public static DropwizardTestSupport<OxdServerConfiguration> SUPPORT = null;


@Parameters({"host", "opHost", "redirectUrls"})
@BeforeSuite
public static void beforeSuite(String host, String opHost, String redirectUrls) {
try {
LOG.debug("Running beforeSuite ...");
ServerLauncher.setSetUpSuite(true);

SUPPORT = new DropwizardTestSupport<OxdServerConfiguration>(OxdServerApplication.class,
ResourceHelpers.resourceFilePath("oxd-server-jenkins.yml"),
ConfigOverride.config("server.applicationConnectors[0].port", "0") // Optional, if not using a separate testing-specific configuration file, use a randomly selected port
);
SUPPORT.before();
LOG.debug("HTTP server started.");

setOpClientFactory();
LOG.debug("HTTP server started.");

removeExistingRps();
LOG.debug("Existing RPs are removed.");

RegisterSiteResponse setupClient = SetupClientTest.setupClient(MockTester.newClient(host), opHost, redirectUrls);
MockTester.setSetupClient(setupClient, host, opHost);
LOG.debug("SETUP_CLIENT is set in Tester.");

Preconditions.checkNotNull(MockTester.getAuthorization());
LOG.debug("Tester's authorization is set.");

setupSwaggerSuite(MockTester.getTargetHost(host), opHost, redirectUrls);
LOG.debug("Finished beforeSuite!");
} catch (Exception e) {
LOG.error("Failed to start suite.", e);
throw new AssertionError("Failed to start suite.");
}
}

private static void setOpClientFactory() {
ServerLauncher.getInjector().getInstance(RestResource.class).setOpClientFactory(new OpClientFactoryStubImpl());
}
private static void setupSwaggerSuite(String host, String opHost, String redirectUrls) {
try {
if (StringUtils.countMatches(host, ":") < 2 && "http://localhost".equalsIgnoreCase(host) || "http://127.0.0.1".equalsIgnoreCase(host) ) {
host = host + ":" + SetUpTest.SUPPORT.getLocalPort();
}
io.swagger.client.api.SetUpTest.beforeSuite(host, opHost, redirectUrls); // manual swagger tests setup
io.swagger.client.api.SetUpTest.setTokenProtectionEnabled(SUPPORT.getConfiguration().getProtectCommandsWithAccessToken());
} catch (Throwable e) {
LOG.error("Failed to setup swagger suite.");
}
}

private static void removeExistingRps() {
try {
ServerLauncher.getInjector().getInstance(PersistenceService.class).create();
ServerLauncher.getInjector().getInstance(RpService.class).removeAllRps();
ServerLauncher.getInjector().getInstance(RpService.class).load();
LOG.debug("Finished removeExistingRps successfullly.");
} catch (Exception e) {
LOG.error("Failed to removed existing RPs.", e);
}
}

@AfterSuite
public static void afterSuite() {
try {
LOG.debug("Running afterSuite ...");
SUPPORT.after();
ServerLauncher.shutdown(false);
LOG.debug("HTTP server is successfully stopped.");
} catch (Exception e) {
LOG.error("Failed to stop HTTP server.", e);
}
}

}

0 comments on commit c438b1b

Please sign in to comment.