View Javadoc

1   package org.apache.helix.alerts;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.helix.HelixException;
23  import org.apache.helix.alerts.AlertParser;
24  import org.testng.annotations.Test;
25  import org.testng.AssertJUnit;
26  import org.testng.Assert;
27  import org.testng.annotations.Test;
28  
29  
30  @Test
31  public class TestAlertValidation {
32  
33  	public final String EXP = AlertParser.EXPRESSION_NAME;
34  	public final String CMP = AlertParser.COMPARATOR_NAME;
35  	public final String CON = AlertParser.CONSTANT_NAME;
36  
37  	@Test
38  	public void testSimple() {
39  		String alertName = EXP + "(accumulate()(dbFoo.partition10.latency)) "
40  				+ CMP + "(GREATER) " + CON + "(10)";
41  		boolean caughtException = false;
42  		try {
43  			AlertParser.validateAlert(alertName);
44  		} catch (HelixException e) {
45  			caughtException = true;
46  			e.printStackTrace();
47  		}
48  		AssertJUnit.assertFalse(caughtException);
49  	}
50  	
51  	@Test
52  	public void testSingleInSingleOut() {
53  		String alertName = EXP
54  				+ "(accumulate()(dbFoo.partition10.latency)|EXPAND) " + CMP
55  				+ "(GREATER) " + CON + "(10)";
56  		boolean caughtException = false;
57  		try {
58  			AlertParser.validateAlert(alertName);
59  		} catch (HelixException e) {
60  			caughtException = true;
61  			e.printStackTrace();
62  		}
63  		AssertJUnit.assertFalse(caughtException);
64  	}
65  
66  	@Test
67  	public void testDoubleInDoubleOut() {
68  		String alertName = EXP
69  				+ "(accumulate()(dbFoo.partition10.latency, dbFoo.partition11.latency)|EXPAND) "
70  				+ CMP + "(GREATER) " + CON + "(10)";
71  		boolean caughtException = false;
72  		try {
73  			AlertParser.validateAlert(alertName);
74  		} catch (HelixException e) {
75  			caughtException = true;
76  			e.printStackTrace();
77  		}
78  		AssertJUnit.assertTrue(caughtException);
79  	}
80  
81  	@Test
82  	public void testTwoStageOps() {
83  		String alertName = EXP
84  				+ "(accumulate()(dbFoo.partition*.latency, dbFoo.partition*.count)|EXPAND|DIVIDE) "
85  				+ CMP + "(GREATER) " + CON + "(10)";
86  		boolean caughtException = false;
87  		try {
88  			AlertParser.validateAlert(alertName);
89  		} catch (HelixException e) {
90  			caughtException = true;
91  			e.printStackTrace();
92  		}
93  		AssertJUnit.assertFalse(caughtException);
94  	}
95  
96  	@Test
97  	public void testTwoListsIntoOne() {
98  		String alertName = EXP
99  				+ "(accumulate()(dbFoo.partition10.latency, dbFoo.partition11.count)|SUM) "
100 				+ CMP + "(GREATER) " + CON + "(10)";
101 		boolean caughtException = false;
102 		try {
103 			AlertParser.validateAlert(alertName);
104 		} catch (HelixException e) {
105 			caughtException = true;
106 			e.printStackTrace();
107 		}
108 		AssertJUnit.assertFalse(caughtException);
109 	}
110 
111 	@Test
112 	public void testSumEach() {
113 		String alertName = EXP
114 				+ "(accumulate()(dbFoo.partition*.latency, dbFoo.partition*.count)|EXPAND|SUMEACH|DIVIDE) "
115 				+ CMP + "(GREATER) " + CON + "(10)";
116 		boolean caughtException = false;
117 		try {
118 			AlertParser.validateAlert(alertName);
119 		} catch (HelixException e) {
120 			caughtException = true;
121 			e.printStackTrace();
122 		}
123 		AssertJUnit.assertFalse(caughtException);
124 	}
125 
126 	@Test
127 	public void testNeedTwoTuplesGetOne() {
128 		String alertName = EXP
129 				+ "(accumulate()(dbFoo.partition*.latency)|EXPAND|DIVIDE) "
130 				+ CMP + "(GREATER) " + CON + "(10)";
131 		boolean caughtException = false;
132 		try {
133 			AlertParser.validateAlert(alertName);
134 		} catch (HelixException e) {
135 			caughtException = true;
136 			e.printStackTrace();
137 		}
138 		AssertJUnit.assertTrue(caughtException);
139 	}
140 
141 	@Test
142 	public void testExtraPipe() {
143 		String alertName = EXP + "(accumulate()(dbFoo.partition10.latency)|) "
144 				+ CMP + "(GREATER) " + CON + "(10)";
145 		boolean caughtException = false;
146 		try {
147 			AlertParser.validateAlert(alertName);
148 		} catch (HelixException e) {
149 			caughtException = true;
150 			e.printStackTrace();
151 		}
152 		AssertJUnit.assertTrue(caughtException);
153 	}
154 
155 	@Test
156 	public void testAlertUnknownOp() {
157 		String alertName = EXP
158 				+ "(accumulate()(dbFoo.partition10.latency)|BADOP) " + CMP
159 				+ "(GREATER) " + CON + "(10)";
160 		boolean caughtException = false;
161 		try {
162 			AlertParser.validateAlert(alertName);
163 		} catch (HelixException e) {
164 			caughtException = true;
165 			e.printStackTrace();
166 		}
167 		AssertJUnit.assertTrue(caughtException);
168 	}
169 }