// Test that it works correctly with end entity profiles using the counter
int pid = 0;
try {
EndEntityProfile profile = new EndEntityProfile();
profile.addField(DnComponents.ORGANIZATION);
profile.addField(DnComponents.COUNTRY);
profile.addField(DnComponents.COMMONNAME);
profile.setValue(EndEntityProfile.AVAILCAS,0,""+caid);
profile.setUse(EndEntityProfile.ALLOWEDREQUESTS, 0, false);
endEntityProfileSession.addEndEntityProfile(admin, "TESTREQUESTCOUNTER", profile);
pid = endEntityProfileSession.getEndEntityProfileId(admin, "TESTREQUESTCOUNTER");
} catch (EndEntityProfileExistsException pee) {
assertTrue("Can not create end entity profile", false);
}
// Now add extended information with allowed requests 2
ei = new ExtendedInformation();
allowedrequests = 2;
ei.setCustomData(ExtendedInformation.CUSTOM_REQUESTCOUNTER, String.valueOf(allowedrequests));
user = new UserDataVO(username, "C=SE,O=AnaTom,CN="+username, caid, null, null, SecConst.USER_INVALID, pid, SecConst.CERTPROFILE_FIXED_ENDUSER, SecConst.TOKEN_SOFT_PEM, 0, ei);
thrown = false;
try {
userAdminSession.changeUser(admin, user, false);
} catch (UserDoesntFullfillEndEntityProfile e) {
thrown = true;
}
assertTrue(thrown);
// decrease the value
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(0, counter);
// decrease the value again, default value when the counter is not used is 0
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(0, counter);
// Now allow the counter
EndEntityProfile ep = endEntityProfileSession.getEndEntityProfile(admin, pid);
ep.setUse(EndEntityProfile.ALLOWEDREQUESTS, 0, true);
ep.setValue(EndEntityProfile.ALLOWEDREQUESTS,0,"2");
endEntityProfileSession.changeEndEntityProfile(admin, "TESTREQUESTCOUNTER", ep);
// This time changeUser will be ok
userAdminSession.changeUser(admin, user, false);
// decrease the value
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(1, counter);
// decrease the value again
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(0, counter);
// decrease the value again
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(-1, counter);
// decrease the value again
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(-1, counter);
// Now disallow the counter, it will be deleted from the user
ep = endEntityProfileSession.getEndEntityProfile(admin, pid);
ep.setUse(EndEntityProfile.ALLOWEDREQUESTS, 0, false);
endEntityProfileSession.changeEndEntityProfile(admin, "TESTREQUESTCOUNTER", ep);
ei = user.getExtendedinformation();
ei.setCustomData(ExtendedInformation.CUSTOM_REQUESTCOUNTER, null);
user.setExtendedinformation(ei);
userAdminSession.changeUser(admin, user, false);
// decrease the value
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(0, counter);
// allow the counter
ep = endEntityProfileSession.getEndEntityProfile(admin, pid);
ep.setUse(EndEntityProfile.ALLOWEDREQUESTS, 0, true);
ep.setValue(EndEntityProfile.ALLOWEDREQUESTS,0,"2");
endEntityProfileSession.changeEndEntityProfile(admin, "TESTREQUESTCOUNTER", ep);
ei = user.getExtendedinformation();
ei.setCustomData(ExtendedInformation.CUSTOM_REQUESTCOUNTER, "0");
user.setExtendedinformation(ei);
user.setStatus(UserDataConstants.STATUS_NEW);
userAdminSession.changeUser(admin, user, false);
// decrease the value
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(1, counter);
// decrease the value again
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(0, counter);
// decrease the value again
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(-1, counter);
// test setuserstatus it will re-set the counter
userAdminSession.setUserStatus(admin, user.getUsername(), UserDataConstants.STATUS_GENERATED);
ep = endEntityProfileSession.getEndEntityProfile(admin, pid);
ep.setUse(EndEntityProfile.ALLOWEDREQUESTS, 0, true);
ep.setValue(EndEntityProfile.ALLOWEDREQUESTS,0,"3");
endEntityProfileSession.changeEndEntityProfile(admin, "TESTREQUESTCOUNTER", ep);
userAdminSession.setUserStatus(admin, user.getUsername(), UserDataConstants.STATUS_NEW);
// decrease the value
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(2, counter);
// decrease the value again
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(1, counter);
// test setuserstatus again it will not re-set the counter if it is already new
userAdminSession.setUserStatus(admin, user.getUsername(), UserDataConstants.STATUS_NEW);
assertEquals(1, counter);
// decrease the value again
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(0, counter); // sets status to generated
// decrease the value again
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(-1, counter);
// test setuserstatus again it will re-set the counter since status is generated
ep = endEntityProfileSession.getEndEntityProfile(admin, pid);
ep.setUse(EndEntityProfile.ALLOWEDREQUESTS, 0, true);
ep.setValue(EndEntityProfile.ALLOWEDREQUESTS,0,"3");
endEntityProfileSession.changeEndEntityProfile(admin, "TESTREQUESTCOUNTER", ep);
userAdminSession.setUserStatus(admin, user.getUsername(), UserDataConstants.STATUS_NEW);
// decrease the value
counter = userAdminSession.decRequestCounter(admin, username);
assertEquals(2, counter);