modify copyright year
[msb/discovery.git] / sdclient / discovery-service / src / main / java / org / onap / msb / sdclient / wrapper / util / RegExpTestUtil.java
1 /**
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.msb.sdclient.wrapper.util;
17
18 import java.util.regex.Pattern;
19
20 import org.apache.commons.lang3.StringUtils;
21
22 public class RegExpTestUtil {
23   
24   public static boolean  httpUrlRegExpTest(String url){
25     
26     
27     
28     String httpUrlReg = "^(|http:\\/\\/)(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."  
29             +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."  
30             +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."  
31             +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)"
32             +":(\\d{1,5}).*$";   
33     return Pattern.matches(httpUrlReg, url); 
34     
35 }
36     
37   public static boolean  hostRegExpTest(String host){
38       
39       String hostReg = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."  
40               +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."  
41               +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."  
42               +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)"
43               +":(\\d{1,5})$";   
44       return Pattern.matches(hostReg, host); 
45       
46   }
47   
48   public static boolean  ipRegExpTest(String ip){
49       
50       String hostReg = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."  
51               +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."  
52               +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."  
53               +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";  
54       return Pattern.matches(hostReg, ip); 
55       
56   }
57   
58  public static boolean  portRegExpTest(String port){
59       
60       String hostReg = "^\\d{1,5}$";  
61       if(Pattern.matches(hostReg, port)){
62         int iport=Integer.parseInt(port);
63         if(iport>=1 && iport<=65535) return true;
64       }
65       
66       return false;
67       
68   }
69   
70 public static boolean versionRegExpTest(String version){
71       
72       String versionReg = "^v\\d+(\\.\\d+)?$";  
73       return Pattern.matches(versionReg, version); 
74       
75   }
76
77 public static boolean urlRegExpTest(String url){
78     if(url.equals("/")) return true;
79     
80     String urlReg = "^\\/.*((?!\\/).)$";  
81     return Pattern.matches(urlReg, url); 
82     
83 }
84
85 public static boolean serviceNameRegExpTest(String serviceName){
86
87     String serviceNameReg = "^([0-9a-zA-Z]|-|_)*$";  
88     return Pattern.matches(serviceNameReg, serviceName); 
89     
90 }
91
92 public static boolean apiRouteUrlRegExpTest(String url){
93     
94     String urlReg = "^\\/api\\/.*$";  
95     return Pattern.matches(urlReg, url); 
96     
97 }
98
99 public static boolean labelRegExpTest(String label){
100
101   String labelReg = "^\\S+:\\S+$";  
102   String[] labelArray=StringUtils.split(label,",");
103   for(int i=0;i<labelArray.length;i++){
104     if(!Pattern.matches(labelReg, labelArray[i])){
105       return false;
106     }
107   }
108     return true;
109   
110 }
111
112   
113   public static void main(String[] args) {
114    
115       System.out.println(httpUrlRegExpTest("/wqew"));
116   }
117 }