Change API Version to 2.1.15
[aaf/authz.git] / auth / auth-certman / src / test / java / org / onap / aaf / auth / cm / validation / JU_CertmanValidator.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 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 package org.onap.aaf.auth.cm.validation;
22
23 import org.junit.Test;
24 import org.onap.aaf.auth.dao.cass.ArtiDAO;
25
26 import static com.google.common.collect.Lists.newArrayList;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertFalse;
29
30 public class JU_CertmanValidator {
31
32     private static final String COLLECTION_NAME = "collection_name";
33     private static final int MIN_SIZE = 3;
34     private CertmanValidator certmanValidator = new CertmanValidator();
35
36
37     @Test
38     public void nullBlankMin_shouldReportErrorWhenListIsNull() {
39
40         certmanValidator.nullBlankMin(COLLECTION_NAME, null, MIN_SIZE);
41         assertEquals(COLLECTION_NAME + " is null.\n", certmanValidator.errs());
42     }
43
44     @Test
45     public void nullBlankMin_shouldReportErrorWhenListHasNotEnoughElements() {
46
47         certmanValidator.nullBlankMin(COLLECTION_NAME, newArrayList("one", "two"), MIN_SIZE);
48         assertEquals(COLLECTION_NAME + " must have at least " + MIN_SIZE + " entries.\n", certmanValidator.errs());
49     }
50
51     @Test
52     public void nullBlankMin_shouldReportErrorWhenListContainsNullOrEmptyElements() {
53
54         certmanValidator.nullBlankMin(COLLECTION_NAME, newArrayList("one", "", "three"), MIN_SIZE);
55         assertEquals("List Item is blank.\n", certmanValidator.errs());
56     }
57
58     @Test
59     public void nullBlankMin_shouldPassValidation() {
60
61         certmanValidator.nullBlankMin(COLLECTION_NAME, newArrayList("one", "two", "three"), MIN_SIZE);
62         assertFalse(certmanValidator.err());
63     }
64
65     @Test
66     public void artisRequired_shouldReportErrorWhenListIsNull() {
67
68         certmanValidator.artisRequired(null, MIN_SIZE);
69         assertEquals("Artifact List is null.\n", certmanValidator.errs());
70     }
71
72     @Test
73     public void artisRequired_shouldReportErrorWhenListHasNotEnoughElements() {
74
75         certmanValidator.artisRequired(newArrayList(newArtifactData(), newArtifactData()), MIN_SIZE);
76         assertEquals("Artifacts must have at least " + MIN_SIZE + " entries.\n", certmanValidator.errs());
77     }
78
79     @Test
80     public void artisRequired_shouldReportErrorWhenArtifactDoesNotHaveAllRequiredFields() {
81
82         certmanValidator.artisRequired(newArrayList(newArtifactData("id", "", "ca", "dir", "user")), 1);
83         assertEquals("machine is blank.\n"  + "NS must be dot separated AlphaNumeric\n", certmanValidator.errs());
84     }
85
86     @Test
87     public void keys_shouldReportErrorWhenArtifactIsNull() {
88
89         certmanValidator.keys(null);
90         assertEquals("Artifact is null.\n", certmanValidator.errs());
91     }
92
93     @Test
94     public void keys_shouldReportErrorWhenArtifactDoesNotHaveAllRequiredFields() {
95
96         certmanValidator.keys(newArtifactData("", "", "ca", "dir", "user"));
97         assertEquals("mechid is blank.\n" + "machine is blank.\n", certmanValidator.errs());
98     }
99
100     private ArtiDAO.Data newArtifactData() {
101         return new ArtiDAO.Data();
102     }
103
104     private ArtiDAO.Data newArtifactData(String mechId, String machine, String ca, String dir, String user) {
105         ArtiDAO.Data artifact = new ArtiDAO.Data();
106         artifact.mechid = mechId;
107         artifact.machine = machine;
108         artifact.ca = ca;
109         artifact.dir = dir;
110         artifact.os_user = user;
111         return artifact;
112
113     }
114 }