Skip to content

Commit

Permalink
Cover oxTrust API by tests #786 - tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmogn committed Feb 19, 2018
1 parent 378dbd0 commit d1abe3d
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 11 deletions.
Expand Up @@ -32,24 +32,40 @@ public AbstractClient(Class<T> entityClass, String baseURI, String path) {
webTarget = client.target(baseURI).path(path);
}

/**
* Read entity by ID.
*
* @param id ID of the entity (inum bu default)
* @return entity instance
* @throws ClientErrorException
*/
public T read(String id) throws ClientErrorException {
WebTarget resource = webTarget.path(java.text.MessageFormat.format("read/{0}", new Object[]{id}));
return resource.request(javax.ws.rs.core.MediaType.APPLICATION_JSON)
.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON)
.get(entityClass);
}

public void create(T requestEntity) throws ClientErrorException {
/**
* Create (save) entity.
*
* @param requestEntity
* @return ID of created entity (inum bu default)
* @throws ClientErrorException
*/
public String create(T requestEntity) throws ClientErrorException {
// Entity<T> entity = Entity.entity(requestEntity, MediaType.APPLICATION_JSON);
// System.out.println("entity: " + entity);
// Response response = client.target("http://localhost:8080/ws/user/create")
// .request()
// .post(entity);

Response response = webTarget.path("create").request().post(Entity.entity(requestEntity, MediaType.APPLICATION_JSON));

String id = response.readEntity(String.class);
System.out.println("response: " + response.getStatusInfo().getReasonPhrase());
response.close();

return id;
}

public boolean update(T requestEntity, String id) throws ClientErrorException {
Expand Down
Expand Up @@ -11,20 +11,21 @@
import org.apache.logging.log4j.Logger;
import org.gluu.oxtrust.api.client.AbstractClient;
import org.gluu.oxtrust.api.saml.SAMLTrustRelationshipShort;
import org.gluu.oxtrust.model.GluuSAMLTrustRelationship;

/**
* REST webservice CRUD for TrustRelationships.
*
* @author Dmitry Ognyannikov
*/
public class TrustRelationshipClient extends AbstractClient<String> {
public class TrustRelationshipClient extends AbstractClient<GluuSAMLTrustRelationship> {

private final Logger logger = LogManager.getLogger(getClass());
private static final Logger logger = LogManager.getLogger(TrustRelationshipClient.class);

private static final String PATH = "/api/saml/tr";

public TrustRelationshipClient(String baseURI) {
super(String.class, baseURI, PATH);
super(GluuSAMLTrustRelationship.class, baseURI, PATH);
}

public List<SAMLTrustRelationshipShort> list() {
Expand Down
@@ -0,0 +1,17 @@
/*
* oxTrust is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
*
* Copyright (c) 2018, Gluu
*/
package org.gluu.oxtrust.api.test;

/**
* A message about test fail.
*
* @author Dmitry Ognyannikov
*/
public class APITestException extends Exception {
public APITestException(String message) {
super(message);
}
}
Expand Up @@ -5,6 +5,8 @@
*/
package org.gluu.oxtrust.api.test;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.gluu.oxtrust.api.client.Client;
import org.gluu.oxtrust.api.test.saml.SamlTestScenary;

Expand All @@ -15,7 +17,9 @@
*/
public class ClientTestScenary {

private Client client;
private static final Logger logger = LogManager.getLogger(ClientTestScenary.class);

private final Client client;

public ClientTestScenary(Client client) {
this.client = client;
Expand All @@ -24,7 +28,7 @@ public ClientTestScenary(Client client) {
/**
* Run tests.
*/
public void run() {
public void run() throws APITestException {
SamlTestScenary saml = new SamlTestScenary(client);
saml.run();
}
Expand Down
Expand Up @@ -42,7 +42,7 @@ public void init() throws IOException {
/**
* Run tests.
*/
public void run() {
public void run() throws APITestException {
Client client = new Client(baseURI, login, password);

ClientTestScenary clientScenary = new ClientTestScenary(client);
Expand Down
Expand Up @@ -5,8 +5,16 @@
*/
package org.gluu.oxtrust.api.test.saml;

import java.util.List;
import java.util.Random;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.gluu.oxtrust.api.client.Client;
import org.gluu.oxtrust.api.client.saml.TrustRelationshipClient;
import org.gluu.oxtrust.api.saml.SAMLTrustRelationshipShort;
import org.gluu.oxtrust.api.test.APITestException;
import org.gluu.oxtrust.model.GluuMetadataSourceType;
import org.gluu.oxtrust.model.GluuSAMLTrustRelationship;

/**
* SAML-relater test requests.
Expand All @@ -15,18 +23,55 @@
*/
public class SamlTestScenary {

private Client client;
private static final Logger logger = LogManager.getLogger(SamlTestScenary.class);

private final Client client;

public SamlTestScenary(Client client) {
this.client = client;
}

private Random random = new Random();

/**
* Run tests.
*/
public void run() {
public void run() throws APITestException {
TrustRelationshipClient samlClient = client.getTrustRelationshipClient();

GluuSAMLTrustRelationship trGenerated = generateRandomeSingleTrustRelationship();
// test create()
String inum = samlClient.create(trGenerated);
// test read()
GluuSAMLTrustRelationship trReaded = samlClient.read(inum);
//TODO: compare
trReaded.setDescription("description changed");
// test read()
samlClient.update(trReaded, inum);
// test list()
List<SAMLTrustRelationshipShort> trustRelationships = samlClient.list();
if (!checkListForTrustRelationship(trustRelationships, inum))
throw new APITestException("TrustRelationship really not saved");
//TODO
}

private GluuSAMLTrustRelationship generateRandomeSingleTrustRelationship() {
int randTestNumber = Math.abs(random.nextInt());

GluuSAMLTrustRelationship tr = new GluuSAMLTrustRelationship();
tr.setDisplayName("test_TrustRelationship_#" + randTestNumber);
tr.setDescription("test TrustRelationship #" + randTestNumber);
tr.setSpMetaDataSourceType(GluuMetadataSourceType.FILE);
tr.setUrl("https://ce.gluu.info");
tr.setSpMetaDataFN("38CBAF15F4E4708200029736F2AB0006BF5CFB85-sp-metadata.xml");
return tr;
}

private boolean checkListForTrustRelationship(List<SAMLTrustRelationshipShort> trustRelationships, String inum) {
for (SAMLTrustRelationshipShort tr : trustRelationships) {
if (inum.equals(tr.getInum()))
return true;
}
return false;
}
}
Expand Up @@ -219,7 +219,7 @@ public String list(@Context HttpServletResponse response) {
try { response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "INTERNAL SERVER ERROR"); } catch (Exception ex) {}
return null;
}
}
}

@GET
@Path("/list_all_federations")
Expand Down

0 comments on commit d1abe3d

Please sign in to comment.