code cleanup and tests

This commit is contained in:
anhefti 2019-03-04 08:17:09 +01:00
parent d2cfa615a1
commit 9bfe3fb2c8
7 changed files with 92 additions and 9 deletions

View file

@ -19,6 +19,7 @@ public final class API {
public static final String PARAM_LOGO_IMAGE = "logoImageBase64";
public static final String PARAM_INSTITUTION_ID = "institutionId";
public static final String PARAM_MODEL_ID = "modelId";
public static final String PARAM_MODEL_ID_LIST = "modelIds";
public static final String PARAM_ENTITY_TYPE = "entityType";
public static final String PARAM_BULK_ACTION_TYPE = "bulkActionType";

View file

@ -63,7 +63,7 @@ public final class Institution implements GrantEntity, Activatable {
this.name = mapper.getString(INSTITUTION.ATTR_NAME);
this.urlSuffix = mapper.getString(INSTITUTION.ATTR_URL_SUFFIX);
this.logoImage = mapper.getString(INSTITUTION.ATTR_LOGO_IMAGE);
this.active = mapper.getBooleanObject(INSTITUTION.ATTR_ACTIVE);
this.active = false;
}
@Override

View file

@ -10,6 +10,7 @@ package ch.ethz.seb.sebserver.webservice.weblayer.api;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import javax.validation.Valid;
@ -163,7 +164,7 @@ public abstract class EntityController<T extends GrantEntity, M extends GrantEnt
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Collection<EntityKey> getDependencies(
@PathVariable final String modelId,
@RequestParam(API.PARAM_BULK_ACTION_TYPE) final BulkActionType bulkActionType) {
@RequestParam(name = API.PARAM_BULK_ACTION_TYPE, required = true) final BulkActionType bulkActionType) {
this.entityDAO
.byModelId(modelId)
@ -204,11 +205,11 @@ public abstract class EntityController<T extends GrantEntity, M extends GrantEnt
method = RequestMethod.GET,
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Collection<T> getForIds(@RequestParam(name = "ids", required = true) final String ids) {
public List<T> getForIds(@RequestParam(name = API.PARAM_MODEL_ID_LIST, required = true) final String modelIds) {
return Result.tryCatch(() -> {
return Arrays.asList(StringUtils.split(ids, Constants.LIST_SEPARATOR_CHAR))
return Arrays.asList(StringUtils.split(modelIds, Constants.LIST_SEPARATOR_CHAR))
.stream()
.map(modelId -> new EntityKey(modelId, this.entityDAO.entityType()))
.collect(Collectors.toList());
@ -307,7 +308,7 @@ public abstract class EntityController<T extends GrantEntity, M extends GrantEnt
// ******************
@RequestMapping(
path = "/{modelId}",
path = API.MODEL_ID_VAR_PATH_SEGMENT,
method = RequestMethod.DELETE,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public EntityProcessingReport hardDelete(@PathVariable final String modelId) {

View file

@ -0,0 +1,80 @@
/*
* Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package ch.ethz.seb.sebserver.gbl.model.user;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import org.joda.time.DateTimeZone;
import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectWriter;
import ch.ethz.seb.sebserver.gbl.api.JSONMapper;
import ch.ethz.seb.sebserver.gbl.model.Page;
public class UserInfoTest {
@Test
public void pageOfUserAccount() throws Exception {
final Page<UserInfo> page = new Page<>(2, 1, "name", Arrays.asList(
new UserInfo("id1", 1L, "user1", "user1", "user1@inst2.none", true, Locale.ENGLISH, DateTimeZone.UTC,
new HashSet<>(Arrays.asList(UserRole.EXAM_ADMIN.name()))),
new UserInfo("id2", 3L, "user2", "user2", "user2@inst2.none", true, Locale.ENGLISH, DateTimeZone.UTC,
new HashSet<>(Arrays.asList(UserRole.EXAM_ADMIN.name()))),
new UserInfo("id3", 4L, "user3", "user3", "user3@inst2.none", false, Locale.GERMAN, DateTimeZone.UTC,
new HashSet<>(Arrays.asList(UserRole.EXAM_ADMIN.name())))));
final JSONMapper jsonMapper = new JSONMapper();
final ObjectWriter writerWithDefaultPrettyPrinter = jsonMapper.writerWithDefaultPrettyPrinter();
final String json = writerWithDefaultPrettyPrinter.writeValueAsString(page);
assertEquals("{\r\n" +
" \"number_of_pages\" : 2,\r\n" +
" \"page_number\" : 1,\r\n" +
" \"sort\" : \"name\",\r\n" +
" \"content\" : [ {\r\n" +
" \"uuid\" : \"id1\",\r\n" +
" \"institutionId\" : 1,\r\n" +
" \"name\" : \"user1\",\r\n" +
" \"username\" : \"user1\",\r\n" +
" \"email\" : \"user1@inst2.none\",\r\n" +
" \"active\" : true,\r\n" +
" \"language\" : \"en\",\r\n" +
" \"timezone\" : \"UTC\",\r\n" +
" \"userRoles\" : [ \"EXAM_ADMIN\" ]\r\n" +
" }, {\r\n" +
" \"uuid\" : \"id2\",\r\n" +
" \"institutionId\" : 3,\r\n" +
" \"name\" : \"user2\",\r\n" +
" \"username\" : \"user2\",\r\n" +
" \"email\" : \"user2@inst2.none\",\r\n" +
" \"active\" : true,\r\n" +
" \"language\" : \"en\",\r\n" +
" \"timezone\" : \"UTC\",\r\n" +
" \"userRoles\" : [ \"EXAM_ADMIN\" ]\r\n" +
" }, {\r\n" +
" \"uuid\" : \"id3\",\r\n" +
" \"institutionId\" : 4,\r\n" +
" \"name\" : \"user3\",\r\n" +
" \"username\" : \"user3\",\r\n" +
" \"email\" : \"user3@inst2.none\",\r\n" +
" \"active\" : false,\r\n" +
" \"language\" : \"de\",\r\n" +
" \"timezone\" : \"UTC\",\r\n" +
" \"userRoles\" : [ \"EXAM_ADMIN\" ]\r\n" +
" } ],\r\n" +
" \"page_size\" : 3\r\n" +
"}", json);
}
}

View file

@ -269,6 +269,7 @@ public class InstitutionAPITest extends AdministrationAPIIntegrationTester {
+ "dependencies=[], "
+ "errors=[]]",
report.toString());
// get
institution = new RestAPITestHelper()
.withAccessToken(sebAdminAccess)
@ -349,7 +350,7 @@ public class InstitutionAPITest extends AdministrationAPIIntegrationTester {
.withAccessToken(getSebAdminAccess())
.withPath(API.INSTITUTION_ENDPOINT)
.withPath(API.LIST_PATH_SEGMENT)
.withAttribute("ids", "1,2,3")
.withAttribute(API.PARAM_MODEL_ID_LIST, "1,2,3")
.withExpectedStatus(HttpStatus.OK)
.getAsObject(new TypeReference<Collection<Institution>>() {
});

View file

@ -224,7 +224,7 @@ public class LmsSetupAPITest extends AdministrationAPIIntegrationTester {
.withAccessToken(getSebAdminAccess())
.withPath(API.LMS_SETUP_ENDPOINT)
.withPath(API.LIST_PATH_SEGMENT)
.withAttribute("ids", lmsSetup1.id + "," + lmsSetup2.id)
.withAttribute(API.PARAM_MODEL_ID_LIST, lmsSetup1.id + "," + lmsSetup2.id)
.withExpectedStatus(HttpStatus.OK)
.getAsObject(new TypeReference<Collection<LmsSetup>>() {
});

View file

@ -1090,7 +1090,7 @@ public class UserAPITest extends AdministrationAPIIntegrationTester {
this.mockMvc
.perform(
get(this.endpoint + API.USER_ACCOUNT_ENDPOINT
+ "/list?ids=user1,user2,user6,user7")
+ "/list?modelIds=user1,user2,user6,user7")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.header("Authorization", "Bearer " + sebAdminToken))
.andExpect(status().isOk())
@ -1108,7 +1108,7 @@ public class UserAPITest extends AdministrationAPIIntegrationTester {
this.mockMvc
.perform(
get(this.endpoint + API.USER_ACCOUNT_ENDPOINT
+ "/list?ids=user1,user2,user6,user7")
+ "/list?modelIds=user1,user2,user6,user7")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.header("Authorization", "Bearer " + instAdminToken))
.andExpect(status().isOk())