Add several JUnits for test coverage
[aaf/authz.git] / auth / auth-core / src / test / java / org / onap / aaf / auth / validation / test / JU_Validator.java
index fb59a54..11aaa42 100644 (file)
@@ -7,9 +7,9 @@
  * * Licensed under the Apache License, Version 2.0 (the "License");
  * * you may not use this file except in compliance with the License.
  * * You may obtain a copy of the License at
- * * 
+ * *
  *  *      http://www.apache.org/licenses/LICENSE-2.0
- * * 
+ * *
  *  * Unless required by applicable law or agreed to in writing, software
  * * distributed under the License is distributed on an "AS IS" BASIS,
  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -36,32 +36,36 @@ import org.junit.Test;
 import org.junit.Test;
 import org.onap.aaf.auth.env.AuthzTrans;
 import org.onap.aaf.auth.env.AuthzTransOnlyFilter;
+import org.onap.aaf.auth.layer.Result;
 import org.onap.aaf.auth.validation.Validator;
 import org.onap.aaf.cadi.principal.TaggedPrincipal;
 
+import junit.framework.Assert;
+
 public class JU_Validator {
-       
+
        Validator validator;
-       
+       String base = "\\x25\\x28\\x29\\x2C-\\x2E\\x30-\\x39\\x3D\\x40-\\x5A\\x5F\\x61-\\x7A";
+
        @Before
        public void setUp() {
                validator = new Validator();
        }
-       
+
        @Test
        public void testNullOrBlank() {
                validator.nullOrBlank(null, "str");
                validator.nullOrBlank("test", "");
                validator.nullOrBlank("test", null);
        }
-       
+
        @Test
        public void testIsNull() {
                Object o = new Object();
                validator.isNull(null, null);
                validator.isNull(null, o);
        }
-       
+
        @Test
        public void testDescription() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
                Class c = validator.getClass();
@@ -74,7 +78,122 @@ public class JU_Validator {
                descriptionMethod.invoke(validator,null, null);
                descriptionMethod.invoke(validator,null, "[\\\\x25\\\\x28\\\\x29\\\\x2C-\\\\x2E\\\\x30-\\\\x39\\\\x3D\\\\x40-\\\\x5A\\\\x5F\\\\x61-\\\\x7A\\\\x20]+");
 
-               
+
+       }
+
+       @Test
+       public void testPermType() {
+               Assert.assertNotNull(validator.permType("[\\\\w.-]+"));
+               Assert.assertNotNull(validator.permType(null));
+               Assert.assertNotNull(validator.permType(""));
+               Assert.assertNotNull(validator.permType("aewfew"));
+       }
+
+       @Test
+       public void testPermType1() {
+               Assert.assertNotNull(validator.permType("[\\\\w.-]+",null));
+               Assert.assertNotNull(validator.permType(null,null));
+               Assert.assertNotNull(validator.permType("","test"));
+               Assert.assertNotNull(validator.permType("aewfew","test"));
+       }
+
+       @Test
+       public void testPermInstance() {
+
+               String middle = "]+[\\\\*]*|\\\\*|(([:/]\\\\*)|([:/][!]{0,1}[";
+               Assert.assertNotNull(validator.permInstance("[" + base + middle + base + "]+[\\\\*]*[:/]*))+"));
+               Assert.assertNotNull(validator.permInstance(null));
+               Assert.assertNotNull(validator.permInstance(""));
+               Assert.assertNotNull(validator.permInstance("test"));
+       }
+
+       @Test
+       public void testErr() {
+               Assert.assertFalse(validator.err());
+               validator.isNull("test", null);
+               Assert.assertTrue(validator.err());
+       }
+
+       @Test
+       public void testErrs() {
+               validator.isNull("test", null);
+               Assert.assertNotNull(validator.errs());
+       }
+
+       @Test
+       public void testPermAction() {
+               Assert.assertNotNull(validator.permAction("[" + base + "]+" + "|\\\\*"));
+               Assert.assertNotNull(validator.permAction("test"));
+       }
+
+       @Test
+       public void testRole() {
+               Assert.assertNotNull(validator.role("[\\\\w.-]+"));
+               Assert.assertNotNull(validator.role(null));
+               Assert.assertNotNull(validator.role(""));
+               Assert.assertNotNull(validator.role("aewfew"));
+       }
+
+       @Test
+       public void testNs() {
+               Assert.assertNotNull(validator.ns("[\\\\w.-]+"));
+               Assert.assertNotNull(validator.ns(""));
+               Assert.assertNotNull(validator.ns(".access"));
+       }
+
+       @Test
+       public void testKey() {
+               Assert.assertNotNull(validator.key("[\\\\w.-]+"));
+               Assert.assertNotNull(validator.key(""));
+               Assert.assertNotNull(validator.key(".access"));
+       }
+
+       @Test
+       public void testValue() {
+               Assert.assertNotNull(validator.value(base));
+               Assert.assertNotNull(validator.value(""));
+               Assert.assertNotNull(validator.value(".access"));
+       }
+
+       @Test
+       public void testNotOK() {
+               Result<?> test = mock(Result.class);
+               validator.isNull("test", null);
+               when(test.notOK()).thenReturn(true);
+               Assert.assertNotNull(validator.notOK(null));
+               Assert.assertNotNull(validator.notOK(test));
+       }
+
+       @Test
+       public void testIntRange() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+               Class c = validator.getClass();
+               Class[] cArg = new Class[4];
+               cArg[0] = String.class;
+               cArg[1] = int.class;
+               cArg[2] = int.class;
+               cArg[3] = int.class;            //Steps to test a protected method
+               Method intRangeMethod = c.getDeclaredMethod("intRange", cArg);
+               intRangeMethod.setAccessible(true);
+               intRangeMethod.invoke(validator,"Test",5,1,10);
+               intRangeMethod.invoke(validator,"Test",1,5,10);
+               intRangeMethod.invoke(validator,"Test",11,5,10);
+               intRangeMethod.invoke(validator,"Test",5,6,4);
+       }
+
+       @Test
+       public void testFloatRange() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+               Class c = validator.getClass();
+               Class[] cArg = new Class[4];
+               cArg[0] = String.class;
+               cArg[1] = float.class;
+               cArg[2] = float.class;
+               cArg[3] = float.class;          //Steps to test a protected method
+               Method floatRangeMethod = c.getDeclaredMethod("floatRange", cArg);
+               floatRangeMethod.setAccessible(true);
+               floatRangeMethod.invoke(validator,"Test",5f,1f,10f);
+               floatRangeMethod.invoke(validator,"Test",1f,5f,10f);
+               floatRangeMethod.invoke(validator,"Test",11f,5f,10f);
+               floatRangeMethod.invoke(validator,"Test",5f,6f,4f);
        }
 
        @Test
@@ -86,14 +205,14 @@ public class JU_Validator {
                assertFalse(Validator.INST_CHARS.matcher("Howd?yDoody").matches());
                assertTrue(Validator.INST_CHARS.matcher("_HowdyDoody").matches());
 
-               //              
+               //
                assertTrue(Validator.ACTION_CHARS.matcher("*").matches());
                assertTrue(Validator.INST_CHARS.matcher("*").matches());
                assertFalse(Validator.ACTION_CHARS.matcher(":*").matches());
                assertTrue(Validator.INST_CHARS.matcher(":*").matches());
                assertFalse(Validator.ACTION_CHARS.matcher(":*:*").matches());
                assertTrue(Validator.INST_CHARS.matcher(":*:*").matches());
-               
+
                assertFalse(Validator.ACTION_CHARS.matcher(":hello").matches());
                assertTrue(Validator.INST_CHARS.matcher(":hello").matches());
                assertFalse(Validator.INST_CHARS.matcher("hello:").matches());
@@ -120,7 +239,7 @@ public class JU_Validator {
                assertFalse(Validator.INST_CHARS.matcher(":h:*:*h").matches());
                assertTrue(Validator.INST_CHARS.matcher(":com.test.*:ns:*").matches());
 
-               
+
                assertFalse(Validator.ACTION_CHARS.matcher("1234+235gd").matches());
                assertTrue(Validator.ACTION_CHARS.matcher("1234-235gd").matches());
                assertTrue(Validator.ACTION_CHARS.matcher("1234-23_5gd").matches());
@@ -134,7 +253,7 @@ public class JU_Validator {
                assertFalse(Validator.ACTION_CHARS.matcher("").matches());
                assertFalse(Validator.ACTION_CHARS.matcher(" ").matches());
 
-               // Allow % and =   (Needed for Escaping & Base64 usages) jg 
+               // Allow % and =   (Needed for Escaping & Base64 usages) jg
                assertTrue(Validator.ACTION_CHARS.matcher("1234%235g=d").matches());
                assertFalse(Validator.ACTION_CHARS.matcher(":1234%235g=d").matches());
                assertTrue(Validator.INST_CHARS.matcher("1234%235g=d").matches());
@@ -145,7 +264,7 @@ public class JU_Validator {
                assertTrue(Validator.INST_CHARS.matcher(":1234%235g=d:==%20:*").matches());
                assertTrue(Validator.INST_CHARS.matcher(":*:==%20:*").matches());
 
-               // Allow / instead of :  (more natural instance expression) jg 
+               // Allow / instead of :  (more natural instance expression) jg
                assertFalse(Validator.INST_CHARS.matcher("1234/a").matches());
                assertTrue(Validator.INST_CHARS.matcher("/1234/a").matches());
                assertTrue(Validator.INST_CHARS.matcher("/1234/*/a/").matches());
@@ -165,7 +284,7 @@ public class JU_Validator {
                assertFalse(Validator.INST_CHARS.matcher("123#4-23@5g:d").matches());
                assertFalse(Validator.INST_CHARS.matcher("").matches());
 
-               
+
                for( char c=0x20;c<0x7F;++c) {
                        boolean b;
                        switch(c) {
@@ -181,18 +300,18 @@ public class JU_Validator {
                                        b=true;
                        }
                }
-               
+
                assertFalse(Validator.ID_CHARS.matcher("abc").matches());
                assertFalse(Validator.ID_CHARS.matcher("").matches());
                assertTrue(Validator.ID_CHARS.matcher("abc@att.com").matches());
                assertTrue(Validator.ID_CHARS.matcher("ab-me@att.com").matches());
                assertTrue(Validator.ID_CHARS.matcher("ab-me_.x@att._-com").matches());
-               
+
                assertFalse(Validator.NAME_CHARS.matcher("ab-me_.x@att._-com").matches());
                assertTrue(Validator.NAME_CHARS.matcher("ab-me").matches());
                assertTrue(Validator.NAME_CHARS.matcher("ab-me_.xatt._-com").matches());
 
-               
+
                // 7/22/2016
                assertTrue(Validator.INST_CHARS.matcher(
                                "/!com.att.*/role/write").matches());