@Test public void getParam_of_existing_parameter_of_unspecified_type_with_empty_string() { underTest.setParam("a_string", "value in fake request actually does not matter"); Request.Param<Object> param = underTest.getParam("a_string", (rqt, key) -> ""); assertThat(param.isPresent()).isTrue(); expectSupplierCanNotBeNullNPE(() -> param.or(null)); assertThat(param.or(() -> "foo")).isEqualTo(""); assertThat(param.getValue()).isEqualTo(""); }
@Override public void handle(Request request, Response response) throws Exception { userSession.checkLoggedIn(); try (DbSession dbSession = dbClient.openSession(false)) { organizationFlags.checkEnabled(dbSession); String key = request.mandatoryParam(PARAM_KEY); UpdateOrganizationRequest updateRequest = new UpdateOrganizationRequest( request.getParam(PARAM_NAME, (rqt, paramKey) -> wsSupport.getAndCheckName(rqt)), request.getParam(PARAM_DESCRIPTION, (rqt, paramKey) -> emptyAsNull(wsSupport.getAndCheckDescription(rqt))), request.getParam(PARAM_URL, (rqt, paramKey) -> emptyAsNull(wsSupport.getAndCheckUrl(rqt))), request.getParam(PARAM_AVATAR_URL, (rqt, paramKey) -> emptyAsNull(wsSupport.getAndCheckAvatar(rqt)))); OrganizationDto dto = getDto(dbSession, key); userSession.checkPermission(ADMINISTER, dto); dto.setName(updateRequest.getName().or(dto::getName)) .setDescription(updateRequest.getDescription().or(dto::getDescription)) .setUrl(updateRequest.getUrl().or(dto::getUrl)) .setAvatarUrl(updateRequest.getAvatar().or(dto::getAvatarUrl)); dbClient.organizationDao().update(dbSession, dto); dbSession.commit(); writeResponse(request, response, dto); } }
@Override public void handle(Request request, Response response) throws Exception { userSession.checkLoggedIn(); try (DbSession dbSession = dbClient.openSession(false)) { organizationFlags.checkEnabled(dbSession); String key = request.mandatoryParam(PARAM_KEY); UpdateOrganizationRequest updateRequest = new UpdateOrganizationRequest( request.getParam(PARAM_NAME, (rqt, paramKey) -> wsSupport.getAndCheckName(rqt)), request.getParam(PARAM_DESCRIPTION, (rqt, paramKey) -> emptyAsNull(wsSupport.getAndCheckDescription(rqt))), request.getParam(PARAM_URL, (rqt, paramKey) -> emptyAsNull(wsSupport.getAndCheckUrl(rqt))), request.getParam(PARAM_AVATAR_URL, (rqt, paramKey) -> emptyAsNull(wsSupport.getAndCheckAvatar(rqt)))); OrganizationDto dto = getDto(dbSession, key); userSession.checkPermission(ADMINISTER, dto); dto.setName(updateRequest.getName().or(dto::getName)) .setDescription(updateRequest.getDescription().or(dto::getDescription)) .setUrl(updateRequest.getUrl().or(dto::getUrl)) .setAvatarUrl(updateRequest.getAvatar().or(dto::getAvatarUrl)); dbClient.organizationDao().update(dbSession, dto); dbSession.commit(); writeResponse(request, response, dto); } }
@Test public void getParam_of_existing_parameter_of_unspecified_type_with_null_value() { underTest.setParam("a_string", "value in fake request actually does not matter"); Request.Param<Object> param = underTest.getParam("a_string", (rqt, key) -> null); assertThat(param.isPresent()).isTrue(); expectSupplierCanNotBeNullNPE(() -> param.or(null)); assertThat(param.or(() -> "foo")).isNull(); assertThat(param.getValue()).isNull(); }
@Test public void getParam_of_missing_parameter_of_unspecified_type() { Request.Param<Object> param = underTest.getParam("a_string", (rqt, key) -> { throw new IllegalStateException("retrieveAndValidate BiConsumer should not be called"); }); assertThat(param.isPresent()).isFalse(); expectSupplierCanNotBeNullNPE(() -> param.or(null)); assertThat(param.or(() -> "foo")).isEqualTo("foo"); expectGetValueFailureWithISE(param::getValue); }
@Test public void getParam_of_existing_parameter_of_unspecified_type_with_object() { underTest.setParam("a_string", "value in fake request actually does not matter"); Object value = new Object(); Request.Param<Object> param = underTest.getParam("a_string", (rqt, key) -> value); assertThat(param.isPresent()).isTrue(); expectSupplierCanNotBeNullNPE(() -> param.or(null)); assertThat(param.or(() -> "foo")).isSameAs(value); assertThat(param.getValue()).isSameAs(value); }