Check Perm Instance ending in colon
[aaf/authz.git] / auth / auth-service / src / test / java / org / onap / aaf / auth / service / validation / test / JU_ServiceValidator.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 package org.onap.aaf.auth.service.validation.test;
23
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26
27 import java.util.HashSet;
28 import java.util.Set;
29
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.aaf.auth.dao.cass.PermDAO;
33 import org.onap.aaf.auth.dao.cass.RoleDAO;
34 import org.onap.aaf.auth.layer.Result;
35 import org.onap.aaf.auth.service.validation.ServiceValidator;
36 import org.onap.aaf.auth.validation.Validator;
37
38 public class JU_ServiceValidator {
39
40         ServiceValidator validator;
41
42         @Before
43         public void setUp() {
44                 validator = new ServiceValidator();
45         }
46
47         @Test
48         public void permNotOk() {
49
50                 Result<PermDAO.Data> rpd = Result.err(1, "ERR_Security");
51
52                 validator.perm(rpd);
53                 assertTrue(validator.errs().equals("ERR_Security\n"));
54
55         }
56         
57         @Test
58         public void permInstance() {
59                 assertFalse(validator.permInstance("hello").err());
60                 assertFalse(validator.permInstance("hello32").err());
61                 assertFalse(validator.permInstance("hello-32").err());
62                 assertFalse(validator.permInstance(":asdf:*:sdf*:sdk").err());
63                 assertFalse(validator.permInstance(":asdf:*:sdf*:sdk*").err());
64                 // Perms may not end in ":"
65                 assertTrue(validator.permInstance(":").err());
66                 assertTrue(validator.permInstance(":hello:").err());
67         }
68
69         @Test
70         public void permOkNull() {
71
72                 Result rpd = Result.ok();
73
74                 validator.perm(rpd);
75                 assertTrue(validator.errs().equals("Perm Data is null.\n"));
76
77         }
78
79         @Test
80         public void roleOkNull() {
81
82                 Result rrd = Result.ok();
83
84                 validator.role(rrd);
85                 assertTrue(validator.errs().equals("Role Data is null.\n"));
86         }
87
88         @Test
89         public void roleOk() {
90                 RoleDAO.Data to = new RoleDAO.Data();
91                 to.ns = "namespace";
92                 to.name = "name";
93                 to.description = "description";
94                 Set<String> permissions = new HashSet<String>();
95                 permissions.add("perm1");
96                 to.perms = permissions;
97
98                 Result<RoleDAO.Data> rrd = Result.ok(to);
99
100                 validator.role(rrd);
101                 assertTrue(
102                                 validator.errs().equals("Perm [perm1] in Role [namespace.name] is not correctly separated with '|'\n"));
103         }
104
105         @Test
106         public void roleNotOk() {
107
108                 Result rrd = Result.err(1, "ERR_Security");
109
110                 validator.role(rrd);
111                 assertTrue(validator.errs().equals("ERR_Security\n"));
112         }
113
114 }