Unit test for MSB JAVA SDK
[msb/java-sdk.git] / src / main / java / org / onap / msb / sdk / discovery / util / RegExpTestUtil.java
1 /*******************************************************************************
2  * Copyright 2017 ZTE, Inc. and others.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  * 
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * 
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  ******************************************************************************/
14 package org.onap.msb.sdk.discovery.util;
15
16 import java.util.regex.Pattern;
17
18 public class RegExpTestUtil {
19
20     public static boolean serviceNameRegExpTest(String serviceName) {
21
22         String serviceNameReg = "^([0-9a-zA-Z]|-|_)*$";
23         return Pattern.matches(serviceNameReg, serviceName);
24
25     }
26
27
28     public static boolean hostRegExpTest(String host) {
29
30         String hostReg = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
31                         + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
32                         + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
33                         + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)" + ":(\\d{1,5})$";
34         return Pattern.matches(hostReg, host);
35
36     }
37
38     public static boolean ipRegExpTest(String ip) {
39
40         String hostReg = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
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                         + "(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
44         return Pattern.matches(hostReg, ip);
45
46     }
47
48     public static boolean portRegExpTest(String port) {
49         String hostReg = "^\\d{1,5}$";
50         if (Pattern.matches(hostReg, port)) {
51             int iport = Integer.parseInt(port);
52             if (iport >= 1 && iport <= 65535)
53                 return true;
54         }
55
56         return false;
57
58     }
59
60     public static boolean versionRegExpTest(String version) {
61
62         String versionReg = "^v\\d+(\\.\\d+)?$";
63         return Pattern.matches(versionReg, version);
64
65     }
66
67     public static boolean urlRegExpTest(String url) {
68         if (url.equals("/"))
69             return true;
70         String urlReg = "^\\/.*((?!\\/).)$";
71         return Pattern.matches(urlReg, url);
72
73     }
74
75
76
77     public static void main(String[] args) {
78         System.out.println(urlRegExpTest("/"));
79     }
80 }