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> </execution>
</executions> </executions>
</plugin> </plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*UserHandler.*</exclude>
</excludes>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

View File

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

View File

@ -14,24 +14,24 @@
package org.opendaylight.aaa.shiro.idm.rest.test; 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.Ignore;
import org.junit.Test; import org.junit.Test;
import org.opendaylight.aaa.api.model.IDMError; import org.opendaylight.aaa.api.model.IDMError;
import org.opendaylight.aaa.api.model.User; import org.opendaylight.aaa.api.model.User;
import org.opendaylight.aaa.api.model.Users; 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 { public class UserHandlerTest extends HandlerTest {
@Test @Test
@ -61,12 +61,14 @@ public class UserHandlerTest extends HandlerTest {
// check create user // check create user
Map<String, String> usrData = new HashMap<>(); Map<String, String> usrData = new HashMap<>();
usrData.put("name", "usr1"); usrData.put("name", "usr1");
usrData.put("description", "test user"); // usrData.put("description", "test user");
usrData.put("enabled", "true"); // usrData.put("enabled", "true");
usrData.put("email", "user1@usr.org"); usrData.put("email", "user1@usr.org");
usrData.put("password", "ChangeZbadPa$$w0rd"); usrData.put("password", "ChangeZbadPa$$w0rd");
usrData.put("domainid", "0"); 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()); assertEquals(201, clientResponse.getStatus());
// check create user missing name data // 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 // Bug 8382: if a user id is specified, 400 is returned
usrData = new HashMap<>(); usrData = new HashMap<>();
usrData.put("name", "usr1"); usrData.put("name", "usr1");
usrData.put("description", "test user"); // usrData.put("description", "test user");
usrData.put("enabled", "true"); // usrData.put("enabled", "true");
usrData.put("email", "user1@usr.org"); usrData.put("email", "user1@usr.org");
usrData.put("password", "ChangeZbadPa$$w0rd"); usrData.put("password", "ChangeZbadPa$$w0rd");
usrData.put("userid", "userid"); usrData.put("userid", "userid");
@ -113,10 +115,10 @@ public class UserHandlerTest extends HandlerTest {
} }
/** /**
* Revision history * Revision history
* * <p>
* ------------------------------------------------------------------------- * -------------------------------------------------------------------------
* Date Author Note * Date Author Note
* * <p>
* ------------------------------------------------------------------------- * -------------------------------------------------------------------------
* 2019/7/3 Dong Xiancun creat * 2019/7/3 Dong Xiancun creat
*/ */

View File

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