Configuration and Auto-Certificates
[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         private final static String ns = "ns";
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 = ns + '|' + 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(ns, type, instance, action);
54                 assertThat(perm.getNS(), is(ns));
55                 assertThat(perm.getType(), is(type));
56                 assertThat(perm.getInstance(), is(instance));
57                 assertThat(perm.getAction(), is(action));
58                 assertThat(perm.getKey(), is(key));
59                 assertThat(perm.permType(), is("AAF"));
60                 assertThat(perm.roles().size(), is(0));
61                 assertThat(perm.toString(), is("AAFPermission:" +
62                                                                                 "\n\tNS: " + ns +
63                                                                                 "\n\tType: " + type +
64                                                                                 "\n\tInstance: " + instance +
65                                                                                 "\n\tAction: " + action +
66                                                                                 "\n\tKey: " + key));
67         }
68
69         @Test
70         public void constructor2Test() {
71                 AAFPermission perm;
72
73                 perm = new AAFPermission(ns, type, instance, action, null);
74                 assertThat(perm.getNS(), is(ns));
75                 assertThat(perm.getType(), is(type));
76                 assertThat(perm.getInstance(), is(instance));
77                 assertThat(perm.getAction(), is(action));
78                 assertThat(perm.getKey(), is(key));
79                 assertThat(perm.permType(), is("AAF"));
80                 assertThat(perm.roles().size(), is(0));
81                 assertThat(perm.toString(), is("AAFPermission:" +
82                                                                                 "\n\tNS: " + ns +
83                                                                                 "\n\tType: " + type +
84                                                                                 "\n\tInstance: " + instance +
85                                                                                 "\n\tAction: " + action +
86                                                                                 "\n\tKey: " + key));
87
88                 perm = new AAFPermission(ns, type, instance, action, roles);
89                 assertThat(perm.getNS(), is(ns));
90                 assertThat(perm.getType(), is(type));
91                 assertThat(perm.getInstance(), is(instance));
92                 assertThat(perm.getAction(), is(action));
93                 assertThat(perm.getKey(), is(key));
94                 assertThat(perm.permType(), is("AAF"));
95                 assertThat(perm.roles().size(), is(1));
96                 assertThat(perm.roles().get(0), is(role));
97                 assertThat(perm.toString(), is("AAFPermission:" +
98                                 "\n\tNS: " + ns +
99                                 "\n\tType: " + type +
100                                 "\n\tInstance: " + instance +
101                                 "\n\tAction: " + action +
102                                 "\n\tKey: " + key));
103         }
104
105         @Test
106         public void matchTest() {
107                 final AAFPermission controlPermission = new AAFPermission(ns,type, instance, action);
108                 PermissionStub perm;
109                 AAFPermission aafperm;
110
111                 aafperm = new AAFPermission(ns, type, instance, action);
112                 assertThat(controlPermission.match(aafperm), is(true));
113
114                 perm = new PermissionStub(key);
115                 assertThat(controlPermission.match(perm), is(true));
116
117                 // Coverage tests
118                 perm = new PermissionStub("not a valid key");
119                 assertThat(controlPermission.match(perm), is(false));
120                 perm = new PermissionStub("type");
121                 assertThat(controlPermission.match(perm), is(false));
122                 perm = new PermissionStub("type|instance|badAction");
123                 assertThat(controlPermission.match(perm), is(false));
124         }
125
126         @Test
127         public void coverageTest() {
128                 AAFPermissionStub aafps = new AAFPermissionStub();
129                 assertThat(aafps.getNS(), is(nullValue()));
130                 assertThat(aafps.getType(), is(nullValue()));
131                 assertThat(aafps.getInstance(), is(nullValue()));
132                 assertThat(aafps.getAction(), is(nullValue()));
133                 assertThat(aafps.getKey(), is(nullValue()));
134                 assertThat(aafps.permType(), is("AAF"));
135                 assertThat(aafps.roles().size(), is(0));
136         }
137
138         private class PermissionStub implements Permission {
139                 private String key;
140
141                 public PermissionStub(String key) { this.key = key; }
142                 @Override public String permType() { return null; }
143                 @Override public String getKey() { return key; }
144                 @Override public boolean match(Permission p) { return false; }
145         }
146
147         private class AAFPermissionStub extends AAFPermission {
148
149         }
150 }