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