1 /*******************************************************************************
2 * ============LICENSE_START====================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
21 ******************************************************************************/
23 package org.onap.aaf.cadi.aaf.test;
25 import static org.junit.Assert.*;
27 import java.util.ArrayList;
28 import java.util.List;
30 import static org.hamcrest.CoreMatchers.*;
32 import org.onap.aaf.cadi.Permission;
33 import org.onap.aaf.cadi.aaf.AAFPermission;
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";
43 private static List<String> roles;
47 roles = new ArrayList<>();
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:" +
64 "\n\tInstance: " + instance +
65 "\n\tAction: " + action +
70 public void constructor2Test() {
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:" +
84 "\n\tInstance: " + instance +
85 "\n\tAction: " + action +
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:" +
100 "\n\tInstance: " + instance +
101 "\n\tAction: " + action +
106 public void matchTest() {
107 final AAFPermission controlPermission = new AAFPermission(ns,type, instance, action);
109 AAFPermission aafperm;
111 aafperm = new AAFPermission(ns, type, instance, action);
112 assertThat(controlPermission.match(aafperm), is(true));
114 perm = new PermissionStub(key);
115 assertThat(controlPermission.match(perm), is(true));
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));
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));
138 private class PermissionStub implements Permission {
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; }
147 private class AAFPermissionStub extends AAFPermission {