Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jgomer2001 committed Aug 3, 2020
1 parent 61437fe commit 510868b
Showing 1 changed file with 159 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package gluu.scim2.client.multipleresource;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import gluu.scim2.client.UserBaseTest;

import org.gluu.oxtrust.model.scim2.user.UserResource;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;

import javax.ws.rs.core.Response;
import java.time.*;
import java.util.*;

import static javax.ws.rs.core.Response.Status.*;

import static org.testng.Assert.*;

/**
* Test devoted to /scim/UpdatedUsers endpoint
*/
public class UpdatedUsersTest extends UserBaseTest {

private static final int MAX_USERS = 10;

private int N;
private List<String> inums;
private ObjectMapper mapper;

@BeforeTest
public void init() {
//Choose N in [1, MAX_USERS]
N = 1 + randInt(MAX_USERS);

inums = new ArrayList<>();
mapper = new ObjectMapper();
}

@Test
public void creatingUsers() throws Exception {

String isoDate = null;

//pick a rand number i in [1, N-1]
int i = 1 + randInt (N - 1);

//Create N random users
for (int j = 0; j < N; j++) {
if (j == i) {
isoDate = ZonedDateTime.now(ZoneOffset.UTC).toString();
}
UserResource user = getDummyPatient();
Response response = client.createUser(user, "id", null);

assertEquals(response.getStatus(), CREATED.getStatusCode());
inums.add(response.readEntity(usrClass).getId());
}

logger.info("Querying created users after '{}'", isoDate);
Response response = client.usersChangedAfter(isoDate, 0, N);
assertEquals(response.getStatus(), OK.getStatusCode());

String json = response.readEntity(String.class);
//Convert response into an opaque map
Map<String, Object> map = mapper.readValue(json, new TypeReference<Map<String, Object>>(){});

//There should be N - i results
assertEquals(map.get("total"), N - i);
Set<String> foundInums = getFoundInums(map);

for (int j = 0; j < inums.size(); j++) {
assertEquals(j >= i, foundInums.contains(inums.get(j)));
}

}

@Test(dependsOnMethods = "creatingUsers")
public void updatingUsers() throws Exception {

//pick 2 rand inums
String A = inums.get(randInt(N));
String B = inums.get(randInt(N));

UserResource u = new UserResource();
u.setActive(true);
String isoDate = ZonedDateTime.now(ZoneOffset.UTC).toString();

//Update active attribute for the chosen users
Response response = client.updateUser(u, A, null, null);
assertEquals(response.getStatus(), OK.getStatusCode());

response = client.updateUser(u, B, null, null);
assertEquals(response.getStatus(), OK.getStatusCode());

logger.info("Querying updated users after '{}'", isoDate);
response = client.usersChangedAfter(isoDate, 0, N);
assertEquals(response.getStatus(), OK.getStatusCode());

String json = response.readEntity(String.class);
//Convert response into an opaque map
Map<String, Object> map = mapper.readValue(json, new TypeReference<Map<String, Object>>(){});
Set<String> foundInums = getFoundInums(map);

assertTrue(foundInums.contains(A));
assertTrue(foundInums.contains(B));

//Ensure there are no false positives
foundInums.remove(A);
foundInums.remove(B);
assertTrue(foundInums.isEmpty());

}

@Test(dependsOnMethods = "updatingUsers", alwaysRun = true)
public void deleteUsers() {

//Delete all users (assert is not used so the list can be thoroughly exhausted)
for (String id : inums) {
Response response = client.deleteUser(id);

if (response.getStatus() == NO_CONTENT.getStatusCode()) {
logger.info("User '{}' removed", id);
} else {
logger.error("Error removing user '{}'", id);
}
}

}

private UserResource getDummyPatient() {

UserResource user = new UserResource();
user.setUserName("test-" + Math.random());
user.setDisplayName(user.getUserName());
return user;
}

private Set<String> getFoundInums(Map<String, Object> map) throws Exception {

Set<String> foundInums = new HashSet<>();
List<Map<String, Object>> results = mapper.convertValue(map.get("results"),
new TypeReference<List<Map<String, Object>>>(){});

for (Map<String, Object> user : results) {
Object inum = List.class.cast(user.get("inum")).get(0);
foundInums.add(inum.toString());
}

return foundInums;

}

//Returns a random integer in [0, n - 1]
private int randInt(int n) {
return Double.valueOf(Math.random() * n).intValue();
}

}

0 comments on commit 510868b

Please sign in to comment.