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