Skip to content
This repository has been archived by the owner on Jun 9, 2021. It is now read-only.

Commit

Permalink
Validating button forms JSON #173
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Dec 27, 2016
1 parent 1384dd2 commit e52ff1d
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 92 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Changelog of Pull Request Notifier for Bitbucket.

## Unreleased
### GitHub [#173](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/pull/173) Add the ability to specify forms for buttons
Validating button forms JSON

[f2279ddefd9aee8](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/f2279ddefd9aee8) Tomas Bjerre *2016-12-27 08:01:47*

Refactoring button forms

* Triggering without showing dialog, if no form supplied.
Expand All @@ -25,7 +29,7 @@ Changelog of Pull Request Notifier for Bitbucket.
### GitHub [#174](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/174) java.lang.NullPointerException: null
More information if header name/value missing

[82765ba693e04cd](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/82765ba693e04cd) Tomas Bjerre *2016-12-26 21:18:18*
[1384dd2418f3049](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/1384dd2418f3049) Tomas Bjerre *2016-12-26 21:33:56*

### GitHub [#57](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/57) Trigger button: confirmation dialog
Adding optional confirmation text to buttons
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ cd *
git reset --hard $TO_HASH
git status
git remote add from $FROM_REPO
git fetch from
git fetch --all
git merge $FROM_HASH
git --no-pager log --max-count=10 --graph --abbrev-commit
Expand Down
25 changes: 0 additions & 25 deletions src/main/java/se/bjurr/prnfb/presentation/ButtonServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
import static javax.ws.rs.core.Response.status;
import static javax.ws.rs.core.Response.Status.OK;
import static javax.ws.rs.core.Response.Status.UNAUTHORIZED;
import static se.bjurr.prnfb.presentation.dto.ButtonFormType.radio;
import static se.bjurr.prnfb.transformer.ButtonTransformer.toButtonDto;
import static se.bjurr.prnfb.transformer.ButtonTransformer.toButtonDtoList;
import static se.bjurr.prnfb.transformer.ButtonTransformer.toPrnfbButton;
import static se.bjurr.prnfb.transformer.ButtonTransformer.toTriggerResultDto;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
Expand All @@ -29,7 +27,6 @@
import javax.ws.rs.core.Response;

import com.atlassian.annotations.security.XsrfProtectionExcluded;
import com.google.common.annotations.VisibleForTesting;

import se.bjurr.prnfb.http.NotificationResponse;
import se.bjurr.prnfb.presentation.dto.ButtonDTO;
Expand Down Expand Up @@ -167,28 +164,6 @@ public Response get(@PathParam("uuid") UUID uuid) {
return ok(dto, APPLICATION_JSON).build();
}

@VisibleForTesting
List<ButtonFormElementDTO> getButtonFormDTOList(List<ButtonFormElementDTO> buttonFormDtoList)
throws Error {
if (buttonFormDtoList == null || buttonFormDtoList.isEmpty()) {
return new ArrayList<>();
}
for (ButtonFormElementDTO buttonFormDto : buttonFormDtoList) {
if (isNullOrEmpty(buttonFormDto.getLabel())) {
throw new Error("The label must be set.");
}
if (isNullOrEmpty(buttonFormDto.getName())) {
throw new Error("The name must be set.");
}
if (buttonFormDto.getType() == radio
&& (buttonFormDto.getButtonFormElementOptionList() == null
|| buttonFormDto.getButtonFormElementOptionList().isEmpty())) {
throw new Error("When adding radio buttons, options must also be defined.");
}
}
return buttonFormDtoList;
}

private void populateButtonFormDtoList(
Integer repositoryId, Long pullRequestId, List<ButtonDTO> dtos) {
for (ButtonDTO dto : dtos) {
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/se/bjurr/prnfb/transformer/ButtonTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import static com.google.common.base.Strings.isNullOrEmpty;
import static com.google.common.collect.Lists.newArrayList;
import static se.bjurr.prnfb.presentation.dto.ButtonDTO.BUTTON_FORM_LIST_DTO_TYPE;
import static se.bjurr.prnfb.presentation.dto.ButtonFormType.checkbox;
import static se.bjurr.prnfb.presentation.dto.ButtonFormType.radio;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.google.common.annotations.VisibleForTesting;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

Expand Down Expand Up @@ -99,6 +102,7 @@ public static PrnfbButton toPrnfbButton(ButtonDTO buttonDto) {
|| buttonFormDto.isEmpty() && !isNullOrEmpty(buttonDto.getButtonFormListString())) {
buttonFormDto = gson.fromJson(buttonDto.getButtonFormListString(), BUTTON_FORM_LIST_DTO_TYPE);
}
validateButtonFormDTOList(buttonFormDto);
List<PrnfbButtonFormElement> buttonFormElement = toPrnfbButtonElementList(buttonFormDto);
return new PrnfbButton( //
buttonDto.getUUID(), //
Expand All @@ -111,6 +115,26 @@ public static PrnfbButton toPrnfbButton(ButtonDTO buttonDto) {
buttonFormElement); //
}

@VisibleForTesting
static void validateButtonFormDTOList(List<ButtonFormElementDTO> buttonFormDtoList) throws Error {
if (buttonFormDtoList == null || buttonFormDtoList.isEmpty()) {
return;
}
for (ButtonFormElementDTO buttonFormDto : buttonFormDtoList) {
if (isNullOrEmpty(buttonFormDto.getLabel())) {
throw new Error("The label must be set.");
}
if (isNullOrEmpty(buttonFormDto.getName())) {
throw new Error("The name must be set.");
}
if ((buttonFormDto.getType() == radio || buttonFormDto.getType() == checkbox)
&& (buttonFormDto.getButtonFormElementOptionList() == null
|| buttonFormDto.getButtonFormElementOptionList().isEmpty())) {
throw new Error("When adding radio buttons, options must also be defined.");
}
}
}

private static List<PrnfbButtonFormElement> toPrnfbButtonElementList(
List<ButtonFormElementDTO> buttonFormDtoList) {
List<PrnfbButtonFormElement> to = new ArrayList<>();
Expand Down
65 changes: 0 additions & 65 deletions src/test/java/se/bjurr/prnfb/presentation/ButtonServletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

import static com.google.common.collect.Lists.newArrayList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.Matchers.anyListOf;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static se.bjurr.prnfb.presentation.dto.ButtonFormType.radio;
import static se.bjurr.prnfb.presentation.dto.ButtonFormType.textarea;
import static se.bjurr.prnfb.settings.USER_LEVEL.EVERYONE;
import static se.bjurr.prnfb.test.Podam.populatedInstanceOf;
import static se.bjurr.prnfb.transformer.ButtonTransformer.toPrnfbButton;
Expand All @@ -30,7 +27,6 @@
import org.mockito.stubbing.Answer;

import se.bjurr.prnfb.presentation.dto.ButtonDTO;
import se.bjurr.prnfb.presentation.dto.ButtonFormElementDTO;
import se.bjurr.prnfb.presentation.dto.ON_OR_OFF;
import se.bjurr.prnfb.service.ButtonsService;
import se.bjurr.prnfb.service.PrnfbRenderer.ENCODE_FOR;
Expand Down Expand Up @@ -292,65 +288,4 @@ public void testThatButtonCanBeUpdated() throws Exception {
verify(this.settingsService) //
.addOrUpdateButton(prnfbButton);
}

@Test
public void testThatButtonFormJsonStringCanBeTransormedToDTO() {

ButtonFormElementDTO button = new ButtonFormElementDTO();
button.setDefaultValue("defaultValue");
button.setDescription("descr");
button.setLabel("label");
button.setName("name");
button.setRequired(true);
button.setType(textarea);
List<ButtonFormElementDTO> buttonFormDtoList = new ArrayList<>();
buttonFormDtoList.add(button);
List<ButtonFormElementDTO> actual = sut.getButtonFormDTOList(buttonFormDtoList);

assertThat(actual) //
.isNotNull() //
.hasSize(1);
}

@Test
public void testThatButtonFormJsonStringCanFindProblemsInJsonNoName() {
ButtonFormElementDTO button = new ButtonFormElementDTO();
button.setDefaultValue("defaultValue");
button.setDescription("descr");
button.setLabel("label");
button.setName("");
button.setRequired(true);
button.setType(textarea);
List<ButtonFormElementDTO> buttonFormDtoList = new ArrayList<>();
buttonFormDtoList.add(button);
try {
sut.getButtonFormDTOList(buttonFormDtoList);
} catch (Error e) {
assertThat(e.getMessage()) //
.isEqualTo("The name must be set.");
return;
}
fail("No error from: " + buttonFormDtoList);
}

@Test
public void testThatButtonFormJsonStringCanFindProblemsInJsonNoRadioNoOptions() {
ButtonFormElementDTO button = new ButtonFormElementDTO();
button.setDefaultValue("defaultValue");
button.setDescription("descr");
button.setLabel("label");
button.setName("name");
button.setRequired(true);
button.setType(radio);
List<ButtonFormElementDTO> buttonFormDtoList = new ArrayList<>();
buttonFormDtoList.add(button);
try {
sut.getButtonFormDTOList(buttonFormDtoList);
} catch (Error e) {
assertThat(e.getMessage()) //
.isEqualTo("When adding radio buttons, options must also be defined.");
return;
}
fail("No error from: " + buttonFormDtoList);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package se.bjurr.prnfb.transformer;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static se.bjurr.prnfb.presentation.dto.ButtonFormType.radio;
import static se.bjurr.prnfb.presentation.dto.ButtonFormType.textarea;
import static se.bjurr.prnfb.test.Podam.populatedInstanceOf;
import static se.bjurr.prnfb.transformer.ButtonTransformer.validateButtonFormDTOList;
import static se.bjurr.prnfb.transformer.SettingsTransformer.toDto;
import static se.bjurr.prnfb.transformer.SettingsTransformer.toPrnfbSettingsData;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import se.bjurr.prnfb.presentation.dto.ButtonFormElementDTO;
import se.bjurr.prnfb.presentation.dto.SettingsDataDTO;
import se.bjurr.prnfb.settings.ValidationException;

Expand All @@ -23,4 +31,68 @@ public void testTransformation() throws ValidationException {
assertThat(retransformedDto.hashCode()) //
.isEqualTo(originalDto.hashCode());
}

@Test
public void testThatButtonFormJsonStringCanFindProblemsInJsonNoName() {
ButtonFormElementDTO button = new ButtonFormElementDTO();
button.setDefaultValue("defaultValue");
button.setDescription("descr");
button.setLabel("label");
button.setName("");
button.setRequired(true);
button.setType(textarea);
List<ButtonFormElementDTO> buttonFormDtoList = new ArrayList<>();
buttonFormDtoList.add(button);
try {
validateButtonFormDTOList(buttonFormDtoList);
} catch (Error e) {
assertThat(e.getMessage()) //
.isEqualTo("The name must be set.");
return;
}
fail("No error from: " + buttonFormDtoList);
}

@Test
public void testThatButtonFormJsonStringCanFindProblemsInJsonNoRadioNoOptions() {
ButtonFormElementDTO button = new ButtonFormElementDTO();
button.setDefaultValue("defaultValue");
button.setDescription("descr");
button.setLabel("label");
button.setName("name");
button.setRequired(true);
button.setType(radio);
List<ButtonFormElementDTO> buttonFormDtoList = new ArrayList<>();
buttonFormDtoList.add(button);
try {
validateButtonFormDTOList(buttonFormDtoList);
} catch (Error e) {
assertThat(e.getMessage()) //
.isEqualTo("When adding radio buttons, options must also be defined.");
return;
}
fail("No error from: " + buttonFormDtoList);
}

@Test
public void testThatButtonFormJsonStringCanFindProblemsInJsonNoRadioEmptyOptions() {
ButtonFormElementDTO button = new ButtonFormElementDTO();
button.setDefaultValue("defaultValue");
button.setDescription("descr");
button.setLabel("label");
button.setName("name");
button.setRequired(true);
button.setType(radio);
button.setButtonFormElementOptionList(new ArrayList<>());
List<ButtonFormElementDTO> buttonFormDtoList = new ArrayList<>();
buttonFormDtoList.add(button);
try {
validateButtonFormDTOList(buttonFormDtoList);
} catch (Error e) {
assertThat(e.getMessage()) //
.isEqualTo("When adding radio buttons, options must also be defined.");
return;
}
fail("No error from: " + buttonFormDtoList);
}
}

0 comments on commit e52ff1d

Please sign in to comment.