|
| 1 | +package org.owasp.esapi.reference.accesscontrol.policyloader; |
| 2 | + |
| 3 | +import static org.mockito.ArgumentMatchers.eq; |
| 4 | + |
| 5 | +import java.math.BigDecimal; |
| 6 | +import java.math.BigInteger; |
| 7 | +import java.util.Date; |
| 8 | +import java.util.Random; |
| 9 | + |
| 10 | +import org.apache.commons.configuration.XMLConfiguration; |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Test; |
| 13 | +import org.mockito.Mockito; |
| 14 | + |
| 15 | +public class ACRParameterLoaderHelperTest { |
| 16 | + private static String DEFAULT_KEY_FORMAT = "AccessControlRules.AccessControlRule(%s).Parameters.Parameter(%s)[@value]"; |
| 17 | + |
| 18 | + XMLConfiguration config = Mockito.spy(XMLConfiguration.class); |
| 19 | + |
| 20 | + private String randomTestKey; |
| 21 | + private int randomRuleIndex; |
| 22 | + private int randomParameterIndex; |
| 23 | + |
| 24 | + @Before |
| 25 | + public void buildUniqueKey () { |
| 26 | + // Assembling a unique key each test verifies that the delegate calls are getting the expected values from the test calls. |
| 27 | + randomRuleIndex = Math.abs(new Random().nextInt() % 100); |
| 28 | + randomParameterIndex = Math.abs(new Random().nextInt() % 100); |
| 29 | + randomTestKey = String.format(DEFAULT_KEY_FORMAT, randomRuleIndex, randomParameterIndex); |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testStringParam_lowercaseType() throws Exception { |
| 35 | + Mockito.doReturn("unused").when(config).getString(eq(randomTestKey)); |
| 36 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "string".toLowerCase()); |
| 37 | + |
| 38 | + // I don't really care what the response is here; |
| 39 | + // I care that the delegate class was called as expected with the generated string key. |
| 40 | + Mockito.verify(config, Mockito.times(1)).getString(randomTestKey); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testStringArrayParam_lowercaseType() throws Exception { |
| 45 | + Mockito.doReturn(new String[0]).when(config).getStringArray(eq(randomTestKey)); |
| 46 | + // Mockito.when(config.getStringArray(eq(randomTestKey))).thenReturn(); |
| 47 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "stringarray".toLowerCase()); |
| 48 | + Mockito.verify(config, Mockito.times(1)).getStringArray(randomTestKey); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testBooleanParam_lowercaseType() throws Exception { |
| 53 | + Mockito.doReturn(Boolean.TRUE).when(config).getBoolean(eq(randomTestKey)); |
| 54 | + // Mockito.when(config.getBoolean(eq(randomTestKey))).thenReturn(Boolean.TRUE); |
| 55 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "boolean".toLowerCase()); |
| 56 | + Mockito.verify(config, Mockito.times(1)).getBoolean(randomTestKey); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testByteParam_lowercaseType() throws Exception { |
| 61 | + Mockito.doReturn( (byte)0 ).when(config).getByte( eq(randomTestKey) ); |
| 62 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "byte".toLowerCase()); |
| 63 | + Mockito.verify(config, Mockito.times(1)).getByte(randomTestKey); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testIntParam_lowercaseType() throws Exception { |
| 68 | + Mockito.doReturn(0).when(config).getInt(eq(randomTestKey)); |
| 69 | + // Mockito.when(config.getInt(eq(randomTestKey))).thenReturn(0); |
| 70 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "int".toLowerCase()); |
| 71 | + Mockito.verify(config, Mockito.times(1)).getInt(randomTestKey); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testLongParam_lowercaseType() throws Exception { |
| 76 | + Mockito.doReturn(0L).when(config).getLong(eq(randomTestKey)); |
| 77 | + // Mockito.when(config.getLong(eq(randomTestKey))).thenReturn(0L); |
| 78 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "long".toLowerCase()); |
| 79 | + Mockito.verify(config, Mockito.times(1)).getLong(randomTestKey); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testFloatParam_lowercaseType() throws Exception { |
| 84 | + Mockito.doReturn((float) 0).when(config).getFloat(eq(randomTestKey)); |
| 85 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "float".toLowerCase()); |
| 86 | + Mockito.verify(config, Mockito.times(1)).getFloat(randomTestKey); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testDoubleParam_lowercaseType() throws Exception { |
| 91 | + Mockito.doReturn(0d).when(config).getDouble(eq(randomTestKey)); |
| 92 | + // Mockito.when(config.getDouble(eq(randomTestKey))).thenReturn(0d); |
| 93 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "double".toLowerCase()); |
| 94 | + Mockito.verify(config, Mockito.times(1)).getDouble(randomTestKey); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testBigDecimalParam_lowercaseType() throws Exception { |
| 100 | + Mockito.doReturn(new BigDecimal(0)).when(config).getBigDecimal(eq(randomTestKey)); |
| 101 | + // Mockito.when(config.getBigDecimal(eq(randomTestKey))).thenReturn(); |
| 102 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "bigdecimal".toLowerCase()); |
| 103 | + Mockito.verify(config, Mockito.times(1)).getBigDecimal(randomTestKey); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testBigIntegerParam_lowercaseType() throws Exception { |
| 108 | + Mockito.doReturn(new BigInteger("0")).when(config).getBigInteger(eq(randomTestKey)); |
| 109 | + // Mockito.when(config.getBigInteger(eq(randomTestKey))).thenReturn(); |
| 110 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "biginteger".toLowerCase()); |
| 111 | + Mockito.verify(config, Mockito.times(1)).getBigInteger(randomTestKey); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testDateParam_lowercaseType() throws Exception { |
| 116 | + String adate = java.text.DateFormat.getDateInstance().format(new Date()); |
| 117 | + Mockito.doReturn(adate).when(config).getString(eq(randomTestKey)); |
| 118 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "date".toLowerCase()); |
| 119 | + Mockito.verify(config, Mockito.times(1)).getString(randomTestKey); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testTimeParam_lowercaseType() throws Exception { |
| 124 | + String atime = new java.text.SimpleDateFormat("h:mm a").format(new Date()); |
| 125 | + Mockito.doReturn(atime).when(config).getString(eq(randomTestKey)); |
| 126 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "time".toLowerCase()); |
| 127 | + Mockito.verify(config, Mockito.times(1)).getString(randomTestKey); |
| 128 | + } |
| 129 | + |
| 130 | + // ------------ |
| 131 | + |
| 132 | + @Test |
| 133 | + public void testStringParam_uppercaseType() throws Exception { |
| 134 | + Mockito.doReturn("unused").when(config).getString(eq(randomTestKey)); |
| 135 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "string".toUpperCase()); |
| 136 | + |
| 137 | + // I don't really care what the response is here; |
| 138 | + // I care that the delegate class was called as expected with the generated string key. |
| 139 | + Mockito.verify(config, Mockito.times(1)).getString(randomTestKey); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void testStringArrayParam_uppercaseType() throws Exception { |
| 144 | + Mockito.doReturn(new String[0]).when(config).getStringArray(eq(randomTestKey)); |
| 145 | + // Mockito.when(config.getStringArray(eq(randomTestKey))).thenReturn(); |
| 146 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "stringarray".toUpperCase()); |
| 147 | + Mockito.verify(config, Mockito.times(1)).getStringArray(randomTestKey); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void testBooleanParam_uppercaseType() throws Exception { |
| 152 | + Mockito.doReturn(Boolean.TRUE).when(config).getBoolean(eq(randomTestKey)); |
| 153 | + // Mockito.when(config.getBoolean(eq(randomTestKey))).thenReturn(Boolean.TRUE); |
| 154 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "boolean".toUpperCase()); |
| 155 | + Mockito.verify(config, Mockito.times(1)).getBoolean(randomTestKey); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void testByteParam_uppercaseType() throws Exception { |
| 160 | + Mockito.doReturn( (byte)0 ).when(config).getByte( eq(randomTestKey) ); |
| 161 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "byte".toUpperCase()); |
| 162 | + Mockito.verify(config, Mockito.times(1)).getByte(randomTestKey); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + public void testIntParam_uppercaseType() throws Exception { |
| 167 | + Mockito.doReturn(0).when(config).getInt(eq(randomTestKey)); |
| 168 | + // Mockito.when(config.getInt(eq(randomTestKey))).thenReturn(0); |
| 169 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "int".toUpperCase()); |
| 170 | + Mockito.verify(config, Mockito.times(1)).getInt(randomTestKey); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void testLongParam_uppercaseType() throws Exception { |
| 175 | + Mockito.doReturn(0L).when(config).getLong(eq(randomTestKey)); |
| 176 | + // Mockito.when(config.getLong(eq(randomTestKey))).thenReturn(0L); |
| 177 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "long".toUpperCase()); |
| 178 | + Mockito.verify(config, Mockito.times(1)).getLong(randomTestKey); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + public void testFloatParam_uppercaseType() throws Exception { |
| 183 | + Mockito.doReturn((float) 0).when(config).getFloat(eq(randomTestKey)); |
| 184 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "float".toUpperCase()); |
| 185 | + Mockito.verify(config, Mockito.times(1)).getFloat(randomTestKey); |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + public void testDoubleParam_uppercaseType() throws Exception { |
| 190 | + Mockito.doReturn(0d).when(config).getDouble(eq(randomTestKey)); |
| 191 | + // Mockito.when(config.getDouble(eq(randomTestKey))).thenReturn(0d); |
| 192 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "double".toUpperCase()); |
| 193 | + Mockito.verify(config, Mockito.times(1)).getDouble(randomTestKey); |
| 194 | + } |
| 195 | + |
| 196 | + |
| 197 | + @Test |
| 198 | + public void testBigDecimalParam_uppercaseType() throws Exception { |
| 199 | + Mockito.doReturn(new BigDecimal(0)).when(config).getBigDecimal(eq(randomTestKey)); |
| 200 | + // Mockito.when(config.getBigDecimal(eq(randomTestKey))).thenReturn(); |
| 201 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "bigdecimal".toUpperCase()); |
| 202 | + Mockito.verify(config, Mockito.times(1)).getBigDecimal(randomTestKey); |
| 203 | + } |
| 204 | + |
| 205 | + @Test |
| 206 | + public void testBigIntegerParam_uppercaseType() throws Exception { |
| 207 | + Mockito.doReturn(new BigInteger("0")).when(config).getBigInteger(eq(randomTestKey)); |
| 208 | + // Mockito.when(config.getBigInteger(eq(randomTestKey))).thenReturn(); |
| 209 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "biginteger".toUpperCase()); |
| 210 | + Mockito.verify(config, Mockito.times(1)).getBigInteger(randomTestKey); |
| 211 | + } |
| 212 | + |
| 213 | + @Test |
| 214 | + public void testDateParam_uppercaseType() throws Exception { |
| 215 | + String adate = java.text.DateFormat.getDateInstance().format(new Date()); |
| 216 | + Mockito.doReturn(adate).when(config).getString(eq(randomTestKey)); |
| 217 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "date".toUpperCase()); |
| 218 | + Mockito.verify(config, Mockito.times(1)).getString(randomTestKey); |
| 219 | + } |
| 220 | + |
| 221 | + @Test |
| 222 | + public void testTimeParam_uppercaseType() throws Exception { |
| 223 | + String atime = new java.text.SimpleDateFormat("h:mm a").format(new Date()); |
| 224 | + Mockito.doReturn(atime).when(config).getString(eq(randomTestKey)); |
| 225 | + ACRParameterLoaderHelper.getParameterValue(config, randomRuleIndex, randomParameterIndex, "time".toUpperCase()); |
| 226 | + Mockito.verify(config, Mockito.times(1)).getString(randomTestKey); |
| 227 | + } |
| 228 | + |
| 229 | +} |
0 commit comments