Skip to content

Commit

Permalink
updating iteration of FormData item (networknt#408)
Browse files Browse the repository at this point in the history
* Added missing test

* Improving readbility - updating iteration of FormData item
  • Loading branch information
jefperito authored and stevehu committed Feb 25, 2019
1 parent 031b0f6 commit 67e6203
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
4 changes: 1 addition & 3 deletions body/src/main/java/com/networknt/body/BodyConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
public class BodyConverter {
static Map<String, Object> convert(FormData data) {
Map<String, Object> map = new HashMap<>();
Iterator<String> iterator = data.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
for (String key : data) {
List<Object> list = new ArrayList<>();
for (FormData.FormValue value : data.get(key)) {
list.add(value);
Expand Down
69 changes: 69 additions & 0 deletions body/src/test/java/com/networknt/body/BodyConverterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.networknt.body;

import io.undertow.server.handlers.form.FormData;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;
import java.util.Map;

public class BodyConverterTest {

@Test
public void shouldToGetEmptyMapWhenFormDataIsEmpty() {
FormData formData = new FormData(99);
Map<String, Object> bodyMap = BodyConverter.convert(formData);

Assert.assertEquals(0, bodyMap.size());
}

@Test
public void shouldToGetConvertedFormDataInAMap() {
String aKey = "aKey";
String aValue = "aValue";
String anotherKey = "anotherKey";
String anotherValue = "anotherValue";

FormData formData = new FormData(99);
formData.add(aKey, aValue);
formData.add(anotherKey, anotherValue);

Map<String, Object> bodyMap = BodyConverter.convert(formData);

Assert.assertEquals(2, bodyMap.size());

List<Object> aConvertedListvalue = (List<Object>) bodyMap.get(aKey);
Assert.assertEquals(1, aConvertedListvalue.size());
Assert.assertTrue(aConvertedListvalue.get(0) instanceof FormData.FormValue);
Assert.assertEquals(aValue, ((FormData.FormValue)aConvertedListvalue.get(0)).getValue());

List<Object> anotherListvalues = (List<Object>) bodyMap.get(anotherKey);
Assert.assertEquals(1, anotherListvalues.size());
Assert.assertTrue(anotherListvalues.get(0) instanceof FormData.FormValue);
Assert.assertEquals(anotherValue, ((FormData.FormValue)anotherListvalues.get(0)).getValue());
}

@Test
public void shouldToGetConvertedFormDataInAMapGroupedByKey() {
String aKey = "aKey";
String aValue = "aValue";
String anotherValue = "anotherValue";

FormData formData = new FormData(99);
formData.add(aKey, aValue);
formData.add(aKey, anotherValue);

Map<String, Object> bodyMap = BodyConverter.convert(formData);

Assert.assertEquals(1, bodyMap.size());

List<Object> aConvertedListvalue = (List<Object>) bodyMap.get(aKey);
Assert.assertEquals(2, aConvertedListvalue.size());

Assert.assertTrue(aConvertedListvalue.get(0) instanceof FormData.FormValue);
Assert.assertEquals(aValue, ((FormData.FormValue)aConvertedListvalue.get(0)).getValue());

Assert.assertTrue(aConvertedListvalue.get(1) instanceof FormData.FormValue);
Assert.assertEquals(anotherValue, ((FormData.FormValue)aConvertedListvalue.get(1)).getValue());
}
}

0 comments on commit 67e6203

Please sign in to comment.