Improve code coverage for aaf cadi modules
[aaf/cadi.git] / core / src / test / java / org / onap / aaf / cadi / UserTest.java
diff --git a/core/src/test/java/org/onap/aaf/cadi/UserTest.java b/core/src/test/java/org/onap/aaf/cadi/UserTest.java
new file mode 100644 (file)
index 0000000..3e8254b
--- /dev/null
@@ -0,0 +1,107 @@
+/*******************************************************************************\r
+ * ============LICENSE_START====================================================\r
+ * * org.onap.aaf\r
+ * * ===========================================================================\r
+ * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
+ * * ===========================================================================\r
+ * * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * * you may not use this file except in compliance with the License.\r
+ * * You may obtain a copy of the License at\r
+ * * \r
+ *  *      http://www.apache.org/licenses/LICENSE-2.0\r
+ * * \r
+ *  * Unless required by applicable law or agreed to in writing, software\r
+ * * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * * See the License for the specific language governing permissions and\r
+ * * limitations under the License.\r
+ * * ============LICENSE_END====================================================\r
+ * *\r
+ * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
+ * *\r
+ ******************************************************************************/\r
+package org.onap.aaf.cadi;\r
+\r
+import static org.hamcrest.CoreMatchers.is;\r
+import static org.junit.Assert.*;\r
+import static org.mockito.Mockito.when;\r
+\r
+import java.security.Principal;\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.mockito.Mock;\r
+import org.mockito.MockitoAnnotations;\r
+import org.onap.aaf.cadi.lur.LocalPermission;\r
+\r
+public class UserTest {\r
+\r
+       @Mock\r
+       private Principal principal;\r
+       \r
+       @Mock\r
+       private LocalPermission permission;\r
+       \r
+       @Before\r
+       public void setup() {\r
+               MockitoAnnotations.initMocks(this);\r
+               \r
+               when(principal.getName()).thenReturn("Principal");\r
+               \r
+               when(permission.getKey()).thenReturn("NewKey");\r
+               when(permission.match(permission)).thenReturn(true);\r
+       }\r
+       \r
+       @Test\r
+       public void testCountCheck() {\r
+               User<Permission> user = new User<Permission>(principal);\r
+               user.resetCount();\r
+               assertThat(user.count, is(0));\r
+               user.incCount();\r
+               assertThat(user.count, is(1));\r
+       }\r
+\r
+       @Test\r
+       public void testPerm() throws InterruptedException {\r
+               User<Permission> user = new User<Permission>(principal);\r
+               assertThat(user.permExpires(), is(Long.MAX_VALUE));\r
+               user.renewPerm();\r
+               Thread.sleep(1);\r
+               assertThat(user.permExpired(), is(true));\r
+               user = new User<Permission>(principal,100);\r
+               assertTrue(user.noPerms());\r
+               user.add(permission);\r
+               assertFalse(user.noPerms());\r
+               user.setNoPerms();\r
+               assertThat(user.permExpired(), is(false));\r
+               assertFalse(user.permsUnloaded());\r
+               user.perms = null;\r
+               assertTrue(user.permsUnloaded());\r
+               assertTrue(user.noPerms());\r
+       }\r
+       \r
+       @Test\r
+       public void testAddValuesToNewMap() {\r
+               User<Permission> user = new User<Permission>(principal);\r
+               Map<String, Permission> newMap = new HashMap<String,Permission>();\r
+               \r
+               assertFalse(user.contains(permission));\r
+               \r
+               user.add(newMap, permission);\r
+               user.setMap(newMap);\r
+               \r
+               assertTrue(user.contains(permission));\r
+               \r
+               List<Permission> sink = new ArrayList<Permission>();\r
+               user.copyPermsTo(sink);\r
+               \r
+               assertThat(sink.size(), is(1));\r
+               assertTrue(sink.contains(permission));\r
+               \r
+               assertThat(user.toString(), is("Principal|:NewKey"));\r
+       }\r
+}\r