4836e4ed1e7e09372718a88ab6128f20bd3517dc
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / aaf / test / JU_AAFPermission.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
4  * * ===========================================================================
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * * ===========================================================================
7  * * Licensed under the Apache License, Version 2.0 (the "License");
8  * * you may not use this file except in compliance with the License.
9  * * You may obtain a copy of the License at
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
13  *  * Unless required by applicable law or agreed to in writing, software
14  * * distributed under the License is distributed on an "AS IS" BASIS,
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * * See the License for the specific language governing permissions and
17  * * limitations under the License.
18  * * ============LICENSE_END====================================================
19  * *
20  * *
21  ******************************************************************************/
22
23 package org.onap.aaf.cadi.aaf.test;
24
25 import static org.junit.Assert.*;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import static org.hamcrest.CoreMatchers.*;
31 import org.junit.*;
32 import org.onap.aaf.cadi.Permission;
33 import org.onap.aaf.cadi.aaf.AAFPermission;
34
35 public class JU_AAFPermission {
36
37         private final static String type = "type";
38         private final static String instance = "instance";
39         private final static String action = "action";
40         private final static String key = type + '|' + instance + '|' + action;
41         private final static String role = "role";
42
43         private static List<String> roles;
44
45         @Before
46         public void setup() {
47                 roles = new ArrayList<>();
48                 roles.add(role);
49         }
50
51         @Test
52         public void constructor1Test() {
53                 AAFPermission perm = new AAFPermission(type, instance, action);
54                 assertThat(perm.getName(), is(type));
55                 assertThat(perm.getInstance(), is(instance));
56                 assertThat(perm.getAction(), is(action));
57                 assertThat(perm.getKey(), is(key));
58                 assertThat(perm.permType(), is("AAF"));
59                 assertThat(perm.roles().size(), is(0));
60                 assertThat(perm.toString(), is("AAFPermission:\n\tType: " + type +
61                                                                                 "\n\tInstance: " + instance +
62                                                                                 "\n\tAction: " + action +
63                                                                                 "\n\tKey: " + key));
64         }
65
66         @Test
67         public void constructor2Test() {
68                 AAFPermission perm;
69
70                 perm = new AAFPermission(type, instance, action, null);
71                 assertThat(perm.getName(), is(type));
72                 assertThat(perm.getInstance(), is(instance));
73                 assertThat(perm.getAction(), is(action));
74                 assertThat(perm.getKey(), is(key));
75                 assertThat(perm.permType(), is("AAF"));
76                 assertThat(perm.roles().size(), is(0));
77                 assertThat(perm.toString(), is("AAFPermission:\n\tType: " + type +
78                                                                                 "\n\tInstance: " + instance +
79                                                                                 "\n\tAction: " + action +
80                                                                                 "\n\tKey: " + key));
81
82                 perm = new AAFPermission(type, instance, action, roles);
83                 assertThat(perm.getName(), is(type));
84                 assertThat(perm.getInstance(), is(instance));
85                 assertThat(perm.getAction(), is(action));
86                 assertThat(perm.getKey(), is(key));
87                 assertThat(perm.permType(), is("AAF"));
88                 assertThat(perm.roles().size(), is(1));
89                 assertThat(perm.roles().get(0), is(role));
90                 assertThat(perm.toString(), is("AAFPermission:\n\tType: " + type +
91                                                                                 "\n\tInstance: " + instance +
92                                                                                 "\n\tAction: " + action +
93                                                                                 "\n\tKey: " + key));
94         }
95
96         @Test
97         public void matchTest() {
98                 final AAFPermission controlPermission = new AAFPermission(type, instance, action);
99                 PermissionStub perm;
100                 AAFPermission aafperm;
101
102                 aafperm = new AAFPermission(type, instance, action);
103                 assertThat(controlPermission.match(aafperm), is(true));
104
105                 perm = new PermissionStub(key);
106                 assertThat(controlPermission.match(perm), is(true));
107
108                 // Coverage tests
109                 perm = new PermissionStub("not a valid key");
110                 assertThat(controlPermission.match(perm), is(false));
111                 perm = new PermissionStub("type");
112                 assertThat(controlPermission.match(perm), is(false));
113                 perm = new PermissionStub("type|instance|badAction");
114                 assertThat(controlPermission.match(perm), is(false));
115         }
116
117         @Test
118         public void coverageTest() {
119                 AAFPermissionStub aafps = new AAFPermissionStub();
120                 assertThat(aafps.getName(), is(nullValue()));
121                 assertThat(aafps.getInstance(), is(nullValue()));
122                 assertThat(aafps.getAction(), is(nullValue()));
123                 assertThat(aafps.getKey(), is(nullValue()));
124                 assertThat(aafps.permType(), is("AAF"));
125                 assertThat(aafps.roles().size(), is(0));
126         }
127
128         private class PermissionStub implements Permission {
129                 private String key;
130
131                 public PermissionStub(String key) { this.key = key; }
132                 @Override public String permType() { return null; }
133                 @Override public String getKey() { return key; }
134                 @Override public boolean match(Permission p) { return false; }
135         }
136
137         private class AAFPermissionStub extends AAFPermission {
138
139         }
140 }