|
| 1 | +package com.nordstrom.common.params; |
| 2 | + |
| 3 | +import static com.nordstrom.common.params.Params.param; |
| 4 | +import static org.testng.Assert.assertEquals; |
| 5 | +import static org.testng.Assert.assertFalse; |
| 6 | +import static org.testng.Assert.assertTrue; |
| 7 | + |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Optional; |
| 10 | + |
| 11 | +import org.testng.annotations.Test; |
| 12 | + |
| 13 | +public class ParamTest implements Params { |
| 14 | + |
| 15 | + @Test |
| 16 | + public void testDefault() { |
| 17 | + assertFalse(Params.super.getParameters().isPresent()); |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + public void testParam() { |
| 22 | + Param param = param("boolean", true); |
| 23 | + assertEquals(param.getKey(), "boolean"); |
| 24 | + verifyBoolean(param.getVal()); |
| 25 | + |
| 26 | + param = param("int", 1); |
| 27 | + assertEquals(param.getKey(), "int"); |
| 28 | + verifyInt(param.getVal()); |
| 29 | + |
| 30 | + param = param("String", "one"); |
| 31 | + assertEquals(param.getKey(), "String"); |
| 32 | + verifyString(param.getVal()); |
| 33 | + |
| 34 | + param = param("Map", Params.mapOf(param("key", "value"))); |
| 35 | + assertEquals(param.getKey(), "Map"); |
| 36 | + verifyMap(param.getVal()); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testParams() { |
| 41 | + Optional<Map<String, Object>> optParameters = getParameters(); |
| 42 | + assertTrue(optParameters.isPresent()); |
| 43 | + Map<String, Object> parameters = optParameters.get(); |
| 44 | + assertFalse(parameters.isEmpty()); |
| 45 | + |
| 46 | + assertTrue(parameters.containsKey("boolean")); |
| 47 | + verifyBoolean(parameters.get("boolean")); |
| 48 | + |
| 49 | + assertTrue(parameters.containsKey("int")); |
| 50 | + verifyInt(parameters.get("int")); |
| 51 | + |
| 52 | + assertTrue(parameters.containsKey("String")); |
| 53 | + verifyString(parameters.get("String")); |
| 54 | + |
| 55 | + assertTrue(parameters.containsKey("Map")); |
| 56 | + verifyMap(parameters.get("Map")); |
| 57 | + } |
| 58 | + |
| 59 | + private void verifyBoolean(Object value) { |
| 60 | + assertTrue(value instanceof Boolean); |
| 61 | + assertTrue((Boolean) value); |
| 62 | + } |
| 63 | + |
| 64 | + private void verifyInt(Object value) { |
| 65 | + assertTrue(value instanceof Integer); |
| 66 | + assertEquals(value, Integer.valueOf(1)); |
| 67 | + } |
| 68 | + |
| 69 | + private void verifyString(Object value) { |
| 70 | + assertTrue(value instanceof String); |
| 71 | + assertEquals(value, "one"); |
| 72 | + } |
| 73 | + |
| 74 | + public void verifyMap(Object value) { |
| 75 | + assertTrue(value instanceof Optional); |
| 76 | + Optional<?> optObj = (Optional<?>) value; |
| 77 | + assertTrue(optObj.isPresent()); |
| 78 | + Object obj = optObj.get(); |
| 79 | + assertTrue(obj instanceof Map); |
| 80 | + Map<?, ?> map = (Map<?, ?>) obj; |
| 81 | + assertTrue(map.containsKey("key")); |
| 82 | + assertEquals(map.get("key"), "value"); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public Optional<Map<String, Object>> getParameters() { |
| 87 | + return Params.mapOf(param("boolean", true), param("int", 1), param("String", "one"), |
| 88 | + param("Map", Params.mapOf(param("key", "value")))); |
| 89 | + } |
| 90 | +} |
0 commit comments