Mod aaa-12 提高单元测试覆盖率

RCA:
SOL:
修改人:dongxiancun
检视人:dongxiancun
This commit is contained in:
dongxiancun 2019-07-26 11:40:38 +08:00
parent 0c30a1f257
commit 93b1f170ff
4 changed files with 51 additions and 86 deletions

View File

@ -251,15 +251,6 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*UserHandler.*</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -14,21 +14,7 @@
package org.opendaylight.aaa.shiro.idm;
import java.util.Collection;
import java.util.Objects;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.opendaylight.aaa.api.ClaimCache;
import org.opendaylight.aaa.AAAShiroProvider;
import org.opendaylight.aaa.api.IDMStoreException;
import org.opendaylight.aaa.api.IIDMStore;
import org.opendaylight.aaa.api.model.IDMError;
@ -37,7 +23,14 @@ import org.opendaylight.aaa.api.model.Users;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ws.rs.*;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
@ -58,7 +51,7 @@ import java.util.Objects;
public class UserHandler {
private static final Logger LOG = LoggerFactory.getLogger(UserHandler.class);
private static final String PW_PATTERN = "/^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)])+$).{8,}$/";
private static final String PW_PATTERN = "^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)])+$).{8,}$";
/**
* If a user is created through the <code>/auth/v1/users</code> rest
* endpoint without a password, the default password is assigned to the
@ -129,10 +122,9 @@ public class UserHandler {
* Extracts the user represented by <code>id</code>. The password and salt
* fields are redacted for security reasons.
*
* @param id
* the unique id of representing the user account
* @param id the unique id of representing the user account
* @return A response with the user information, or internal error if one
* occurs
* occurs
*/
@GET
@Path("/{id}")
@ -170,10 +162,8 @@ public class UserHandler {
* If a password is not provided, please ensure you change the default
* password ASAP for security reasons!
*
* @param info
* passed from Jersey
* @param user
* the user defined in the JSON payload
* @param info passed from Jersey
* @param user the user defined in the JSON payload
* @return A response stating success or failure of user creation
*/
@POST
@ -220,7 +210,7 @@ public class UserHandler {
String userDescription = user.getDescription();
if (userDescription == null) {
user.setDescription(DEFAULT_DESCRIPTION);
} else if (userDescription.length() > IdmLightApplication.MAX_FIELD_LEN) {
} else if (userDescription.length() > IdmLightApplication.MAX_FIELD_LEN) {
// The "description" field has a maximum length.
return providedFieldTooLong("description", IdmLightApplication.MAX_FIELD_LEN);
}
@ -242,7 +232,7 @@ public class UserHandler {
return providedFieldTooLong("password", IdmLightApplication.MAX_FIELD_LEN);
}
Response response = checkPasswordError(userPassword);
Response response = checkPasswordError(userPassword);
if (response.getStatus() != 200) {
return response;
}
@ -278,15 +268,13 @@ public class UserHandler {
private Response providePasswordError(String s) {
return new IDMError(407, s).response();
}
/**
* REST endpoint to update a user account.
*
* @param info
* passed from Jersey
* @param user
* the user defined in the JSON payload
* @param id
* the unique id for the user that will be updated
* @param info passed from Jersey
* @param user the user defined in the JSON payload
* @param id the unique id for the user that will be updated
* @return A response stating success or failure of the user update
*/
@PUT
@ -338,10 +326,8 @@ public class UserHandler {
/**
* REST endpoint to delete a user account.
*
* @param info
* passed from Jersey
* @param id
* the unique id of the user which is being deleted
* @param info passed from Jersey
* @param id the unique id of the user which is being deleted
* @return A response stating success or failure of user deletion
*/
@DELETE
@ -368,10 +354,8 @@ public class UserHandler {
/**
* Creates a <code>Response</code> related to an internal server error.
*
* @param verbal
* such as "creating", "deleting", "updating"
* @param ex
* The exception, which is logged locally
* @param verbal such as "creating", "deleting", "updating"
* @param ex The exception, which is logged locally
* @return A response containing internal error with specific reasoning
*/
private Response internalError(final String verbal, final Exception ex) {
@ -384,8 +368,7 @@ public class UserHandler {
* Creates a <code>Response</code> related to the user not providing a
* required field.
*
* @param fieldName
* the name of the field which is missing
* @param fieldName the name of the field which is missing
* @return A response explaining that the request is missing a field
*/
private Response missingRequiredField(final String fieldName) {
@ -400,10 +383,8 @@ public class UserHandler {
* Creates a <code>Response</code> related to the user providing a field
* that is too long.
*
* @param fieldName
* the name of the field that is too long
* @param maxFieldLength
* the maximum length of <code>fieldName</code>
* @param fieldName the name of the field that is too long
* @param maxFieldLength the maximum length of <code>fieldName</code>
* @return A response containing the bad field and the maximum field length
*/
private Response providedFieldTooLong(final String fieldName, final int maxFieldLength) {
@ -414,10 +395,8 @@ public class UserHandler {
* Creates the client-facing message related to the user providing a field
* that is too long.
*
* @param fieldName
* the name of the field that is too long
* @param maxFieldLength
* the maximum length of <code>fieldName</code>
* @param fieldName the name of the field that is too long
* @param maxFieldLength the maximum length of <code>fieldName</code>
* @return a response containing the too long field and its length
*/
private static String getProvidedFieldTooLongMessage(final String fieldName, final int maxFieldLength) {
@ -430,8 +409,7 @@ public class UserHandler {
* Prepares a user account for output by redacting the appropriate fields.
* This method side-effects the <code>user</code> parameter.
*
* @param user
* the user account which will have fields redacted
* @param user the user account which will have fields redacted
*/
private static void redactUserPasswordInfo(final User user) {
user.setPassword(REDACTED_PASSWORD);
@ -441,8 +419,7 @@ public class UserHandler {
/**
* Validate the input field length.
*
* @param inputField
* the field to check
* @param inputField the field to check
* @return true if input field bigger than the MAX_FIELD_LEN
*/
private boolean checkInputFieldLength(final String inputField) {

View File

@ -14,24 +14,24 @@
package org.opendaylight.aaa.shiro.idm.rest.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import org.junit.Ignore;
import org.junit.Test;
import org.opendaylight.aaa.api.model.IDMError;
import org.opendaylight.aaa.api.model.User;
import org.opendaylight.aaa.api.model.Users;
@Ignore
import javax.ws.rs.NotFoundException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class UserHandlerTest extends HandlerTest {
@Test
@ -61,12 +61,14 @@ public class UserHandlerTest extends HandlerTest {
// check create user
Map<String, String> usrData = new HashMap<>();
usrData.put("name", "usr1");
usrData.put("description", "test user");
usrData.put("enabled", "true");
// usrData.put("description", "test user");
// usrData.put("enabled", "true");
usrData.put("email", "user1@usr.org");
usrData.put("password", "ChangeZbadPa$$w0rd");
usrData.put("domainid", "0");
Response clientResponse = target("/v1/users").request().post(entity(usrData));
Response clientResponse = target("/v1/users")
.request()
.post(entity(usrData));
assertEquals(201, clientResponse.getStatus());
// check create user missing name data
@ -101,8 +103,8 @@ public class UserHandlerTest extends HandlerTest {
// Bug 8382: if a user id is specified, 400 is returned
usrData = new HashMap<>();
usrData.put("name", "usr1");
usrData.put("description", "test user");
usrData.put("enabled", "true");
// usrData.put("description", "test user");
// usrData.put("enabled", "true");
usrData.put("email", "user1@usr.org");
usrData.put("password", "ChangeZbadPa$$w0rd");
usrData.put("userid", "userid");
@ -113,10 +115,10 @@ public class UserHandlerTest extends HandlerTest {
}
/**
* Revision history
*
* <p>
* -------------------------------------------------------------------------
* Date Author Note
*
* <p>
* -------------------------------------------------------------------------
* 2019/7/3 Dong Xiancun creat
*/

View File

@ -139,11 +139,6 @@
</configuration>
</execution>
</executions>
<configuration>
<excludes>
<exclude>**/*UserHandler.*</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>