Mass removal of all Tabs (Style Warnings)
[aaf/authz.git] / auth / auth-core / src / test / java / org / onap / aaf / auth / rserv / test / JU_BetterMatch.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.rserv.test;
23
24 import static junit.framework.Assert.assertEquals;
25 import static junit.framework.Assert.assertFalse;
26 import static junit.framework.Assert.assertTrue;
27
28 import java.util.Set;
29
30 import org.junit.Assert;
31 import org.junit.Test;
32 import org.onap.aaf.auth.rserv.Match;
33 import org.onap.aaf.misc.env.Env;
34 import org.onap.aaf.misc.env.TimeTaken;
35 import org.onap.aaf.misc.env.Trans;
36 import org.onap.aaf.misc.env.impl.EnvFactory;
37
38
39 public class JU_BetterMatch {
40
41     @Test
42     public void test() {
43         Trans trans = EnvFactory.newTrans();
44         // Bad Match
45         Match bm = new Match("/req/1.0.0/:var");
46
47         assertTrue(bm.match("/req/1.0.0/fred"));
48         assertTrue(bm.match("/req/1.0.0/wilma"));
49         assertTrue(bm.match("/req/1.0.0/wilma/"));
50         assertFalse(bm.match("/req/1.0.0/wilma/bambam"));
51         assertFalse(bm.match("/not/valid/234"));
52         assertFalse(bm.match(""));
53         
54         TimeTaken tt = trans.start("A", Env.SUB);
55         TimeTaken tt2;
56         int i = 0;
57         try {
58             bm = new Match(null);
59             tt2 = trans.start(Integer.toString(++i), Env.SUB);
60             assertTrue(bm.match(""));
61             tt2.done();
62             tt2 = trans.start(Integer.toString(++i), Env.SUB);
63             assertTrue(bm.match(null));
64             tt2.done();
65         } finally {
66             tt.done();
67         }
68         
69     
70         tt = trans.start("B", Env.SUB);
71         i = 0;
72         try {
73             bm = new Match("/req/1.0.0/:urn/:ref");
74             tt2 = trans.start(Integer.toString(++i), Env.SUB);
75             assertTrue(bm.match("/req/1.0.0/urn:fsdb,1.0,req,newreq/0x12345"));
76             tt2.done();
77             tt2 = trans.start(Integer.toString(++i), Env.SUB);
78             assertFalse(bm.match("/req/1.0.0/urn"));
79             tt2.done();
80             tt2 = trans.start(Integer.toString(++i), Env.SUB);
81             assertTrue(bm.match("/req/1.0.0/urn:fsdb,1.0,req,newreq/0x12345/"));
82             tt2.done();
83             tt2 = trans.start(Integer.toString(++i), Env.SUB);
84             assertFalse(bm.match("/req/1.0.0/urn:fsdb,1.0,req,newreq/0x12345/x"));
85             tt2.done();
86             tt2 = trans.start(Integer.toString(++i), Env.SUB);
87             assertFalse(bm.match("/req/1.0.0/urn:fsdb,1.0,req,newreq/0x12345/xyx"));
88         } finally {
89             tt2.done();
90             tt.done();    
91         }
92         
93         tt = trans.start("C", Env.SUB);
94         i = 0;
95         try {
96             String url = "/req/1.0.0/";
97             bm = new Match(url+":urn*");
98             tt2 = trans.start(Integer.toString(++i), Env.SUB);
99             String value = "urn:fsdb,1.0,req,newreq/0x12345";
100             
101             assertTrue(bm.match(url+value));
102             assertEquals("urn:fsdb,1.0,req,newreq/0x12345",bm.param(url+value, ":urn"));
103         } finally {
104             tt2.done();
105             tt.done();    
106         }
107
108         tt = trans.start("D", Env.SUB);
109         i = 0;
110         try {
111             bm = new Match("/req/1.0.0/:urn/:ref*");
112             tt2 = trans.start(Integer.toString(++i), Env.SUB);
113             assertTrue(bm.match("/req/1.0.0/urn:fsdb,1.0,req,newreq/0x12345"));
114             tt2.done();
115             tt2 = trans.start(Integer.toString(++i), Env.SUB);
116             assertFalse(bm.match("/req/1.0.0/urn:fsdb,1.0,req,newreq/"));
117         } finally {
118             tt2.done();
119             tt.done();    
120         }
121
122         tt = trans.start("E", Env.SUB);
123         i = 0;
124         try {
125             bm = new Match("this*");
126             tt2 = trans.start(Integer.toString(++i), Env.SUB);
127             assertTrue(bm.match("this"));
128             tt2.done();
129             tt2 = trans.start(Integer.toString(++i), Env.SUB);
130             assertTrue(bm.match("thisandthat"));
131             tt2.done();
132             tt2 = trans.start(Integer.toString(++i), Env.SUB);
133             assertTrue(bm.match("this/1.0.0/urn:fsdb,1.0,req,newreq/0x12345/"));
134         } finally {
135             tt2.done();
136             tt.done();    
137         }
138
139         tt = trans.start("F", Env.SUB);
140         i = 0;
141         try {
142             bm = new Match("*");
143             tt2 = trans.start(Integer.toString(++i), Env.SUB);
144             assertTrue(bm.match("<pass>/this"));
145         } finally {
146             tt2.done();
147             tt.done();    
148         }
149         
150         StringBuilder sb = new StringBuilder();
151         trans.auditTrail(0, sb);
152         //System.out.println(sb);
153         
154     }
155     
156     @Test
157     public void specialTest() {
158         Match match = new Match("/sample");
159         assertTrue(match.match("/sample"));
160         
161         match = new Match("/lpeer//lpeer/:key/:item*");
162         assertTrue(match.match("/lpeer//lpeer/x/y"));
163         assertFalse(match.match("/lpeer/x/lpeer/x/y"));
164
165     }
166
167     @Test
168     public void testGetParamNames() {
169         Match bm = new Match("/req/1.0.0/:var");
170         Set s = bm.getParamNames();
171         Assert.assertNotNull(s);
172     }
173 }