Fix checkstyle reported by odlparent-3.0.0
[controller.git] / opendaylight / config / config-api / src / test / java / org / opendaylight / controller / config / api / ValidationExceptionTest.java
1 /*
2  * Copyright (c) 2014, 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.config.api;
10
11 import static org.hamcrest.CoreMatchers.containsString;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertThat;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16
17 import com.google.common.collect.Lists;
18 import java.util.Map;
19 import org.junit.Assert;
20 import org.junit.Test;
21
22 public class ValidationExceptionTest {
23
24     private final String instance = "instance";
25     private final ModuleIdentifier mi = new ModuleIdentifier("module", instance);
26     private final String instance2 = "instance2";
27     private final ModuleIdentifier mi2 = new ModuleIdentifier("module", instance2);
28     private final String message = "ex message";
29     private final Exception exception = new IllegalStateException(message);
30
31     @Test
32     public void testCreateFromCollectedValidationExceptions() throws Exception {
33         ValidationException single = ValidationException.createForSingleException(mi, exception);
34         ValidationException single2 = ValidationException.createForSingleException(mi2, exception);
35
36         ValidationException collected = ValidationException
37                 .createFromCollectedValidationExceptions(Lists.newArrayList(single, single2));
38
39         Map<String, Map<String, ValidationException.ExceptionMessageWithStackTrace>> failedMap = collected
40                 .getFailedValidations();
41         assertEquals(1, failedMap.size());
42         assertTrue(failedMap.containsKey("module"));
43
44         Map<String, ValidationException.ExceptionMessageWithStackTrace> failedModule = failedMap.get("module");
45         assertEquals(2, failedModule.size());
46         assertTrue(failedModule.containsKey(instance));
47         assertEquals(message, failedModule.get(instance).getMessage());
48         assertEquals(message, failedModule.get(instance2).getMessage());
49         assertEquals(failedModule.get(instance), failedModule.get(instance2));
50     }
51
52     @Test
53     public void testCreateFromCollectedValidationExceptionsWithDuplicate() throws Exception {
54         ValidationException single = ValidationException.createForSingleException(mi, exception);
55         ValidationException single2 = ValidationException.createForSingleException(mi, exception);
56         try {
57             ValidationException.createFromCollectedValidationExceptions(Lists.newArrayList(single, single2));
58         } catch (final IllegalArgumentException ex) {
59             // Duplicate exception
60             assertThat(ex.getMessage(), containsString("Cannot merge"));
61             return;
62         }
63         fail("Duplicate exception should have failed");
64     }
65
66     @Test
67     public void testGetTrace() throws Exception {
68         ValidationException.ExceptionMessageWithStackTrace exp =
69                 new ValidationException.ExceptionMessageWithStackTrace();
70         exp.setStackTrace("trace");
71         Assert.assertEquals(exp.getTrace(), "trace");
72     }
73
74     @Test
75     public void testSetMessage() throws Exception {
76         ValidationException.ExceptionMessageWithStackTrace exp =
77                 new ValidationException.ExceptionMessageWithStackTrace();
78         exp.setMessage("message");
79         Assert.assertEquals(exp.getMessage(), "message");
80     }
81
82     @Test
83     public void testHashCode() throws Exception {
84         ValidationException.ExceptionMessageWithStackTrace exp =
85                 new ValidationException.ExceptionMessageWithStackTrace();
86         Assert.assertEquals(exp.hashCode(), new ValidationException.ExceptionMessageWithStackTrace().hashCode());
87     }
88
89     @Test
90     public void testExceptionMessageWithStackTraceConstructor() throws Exception {
91         ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace(
92                 "string1", "string2");
93         Assert.assertEquals(exp, exp);
94     }
95
96     @Test
97     public void testExceptionMessageWithStackTraceConstructor2() throws Exception {
98         ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace(
99                 "string1", "string2");
100         Assert.assertNotEquals(exp, null);
101     }
102
103     @Test
104     public void testExceptionMessageWithStackTraceConstructor3() throws Exception {
105         ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace(
106                 "string1", "string2");
107         Assert.assertNotEquals(exp, new Exception());
108     }
109
110     @Test
111     public void testExceptionMessageWithStackTraceConstructor4() throws Exception {
112         ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace(
113                 "string1", "string2");
114         Assert.assertEquals(exp, new ValidationException.ExceptionMessageWithStackTrace("string1", "string2"));
115     }
116
117     @Test
118     public void testEqual() throws Exception {
119         ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace(
120                 "string1", "string2");
121         ValidationException.ExceptionMessageWithStackTrace exp2 =
122                 new ValidationException.ExceptionMessageWithStackTrace(
123                 null, "string2");
124         Assert.assertNotEquals(exp, exp2);
125     }
126
127     @Test
128     public void testEqual2() throws Exception {
129         ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace(
130                 "string1", "string2");
131         ValidationException.ExceptionMessageWithStackTrace exp2 =
132                 new ValidationException.ExceptionMessageWithStackTrace(
133                 "different", "string2");
134         Assert.assertNotEquals(exp, exp2);
135     }
136
137     @Test
138     public void testEqual3() throws Exception {
139         ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace(
140                 "string1", "string2");
141         ValidationException.ExceptionMessageWithStackTrace exp2 =
142                 new ValidationException.ExceptionMessageWithStackTrace(
143                 "string1", null);
144         Assert.assertNotEquals(exp, exp2);
145     }
146
147     @Test
148     public void testEqual4() throws Exception {
149         ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace(
150                 "string1", "string2");
151         ValidationException.ExceptionMessageWithStackTrace exp2 =
152                 new ValidationException.ExceptionMessageWithStackTrace(
153                 "string1", "different");
154         Assert.assertNotEquals(exp, exp2);
155     }
156
157     @Test
158     public void testEqual5() throws Exception {
159         ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace(
160                 null, "string2");
161         ValidationException.ExceptionMessageWithStackTrace exp2 =
162                 new ValidationException.ExceptionMessageWithStackTrace(
163                 "string1", "string2");
164         Assert.assertNotEquals(exp, exp2);
165     }
166
167     @Test
168     public void testEqual6() throws Exception {
169         ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace(
170                 "string1", null);
171         ValidationException.ExceptionMessageWithStackTrace exp2 =
172                 new ValidationException.ExceptionMessageWithStackTrace(
173                 "string1", "string2");
174         Assert.assertNotEquals(exp, exp2);
175     }
176 }