1 package org.opendaylight.controller.config.api;
3 import static org.hamcrest.CoreMatchers.containsString;
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertThat;
6 import static org.junit.Assert.assertTrue;
7 import static org.junit.Assert.fail;
9 import com.google.common.collect.Lists;
11 import org.junit.Assert;
12 import org.junit.Test;
14 public class ValidationExceptionTest {
16 private final String instance = "instance";
17 private final ModuleIdentifier mi = new ModuleIdentifier("module", instance);
18 private final String instance2 = "instance2";
19 private final ModuleIdentifier mi2 = new ModuleIdentifier("module", instance2);
20 private final String message = "ex message";
21 private final Exception e = new IllegalStateException(message);
24 public void testCreateFromCollectedValidationExceptions() throws Exception {
25 ValidationException single = ValidationException.createForSingleException(mi, e);
26 ValidationException single2 = ValidationException.createForSingleException(mi2, e);
28 ValidationException collected = ValidationException.createFromCollectedValidationExceptions(Lists.newArrayList(single, single2));
30 Map<String, Map<String, ValidationException.ExceptionMessageWithStackTrace>> failedMap = collected.getFailedValidations();
31 assertEquals(1, failedMap.size());
32 assertTrue(failedMap.containsKey("module"));
34 Map<String, ValidationException.ExceptionMessageWithStackTrace> failedModule = failedMap.get("module");
35 assertEquals(2, failedModule.size());
36 assertTrue(failedModule.containsKey(instance));
37 assertEquals(message, failedModule.get(instance).getMessage());
38 assertEquals(message, failedModule.get(instance2).getMessage());
39 assertEquals(failedModule.get(instance), failedModule.get(instance2));
43 public void testCreateFromCollectedValidationExceptionsWithDuplicate() throws Exception {
44 ValidationException single = ValidationException.createForSingleException(mi, e);
45 ValidationException single2 = ValidationException.createForSingleException(mi, e);
47 ValidationException.createFromCollectedValidationExceptions(Lists.newArrayList(single, single2));
48 } catch (IllegalArgumentException ex) {
49 // Duplicate exception
50 assertThat(ex.getMessage(), containsString("Cannot merge"));
53 fail("Duplicate exception should have failed");
57 public void testGetTrace() throws Exception {
58 ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace();
59 exp.setTrace("trace");
60 Assert.assertEquals(exp.getTrace(), "trace");
64 public void testSetMessage() throws Exception {
65 ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace();
66 exp.setMessage("message");
67 Assert.assertEquals(exp.getMessage(), "message");
71 public void testHashCode() throws Exception {
72 ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace();
73 Assert.assertEquals(exp.hashCode(), new ValidationException.ExceptionMessageWithStackTrace().hashCode());
77 public void testExceptionMessageWithStackTraceConstructor() throws Exception {
78 ValidationException.ExceptionMessageWithStackTrace exp =
79 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
80 Assert.assertEquals(exp, exp);
84 public void testExceptionMessageWithStackTraceConstructor2() throws Exception {
85 ValidationException.ExceptionMessageWithStackTrace exp =
86 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
87 Assert.assertNotEquals(exp, null);
91 public void testExceptionMessageWithStackTraceConstructor3() throws Exception {
92 ValidationException.ExceptionMessageWithStackTrace exp =
93 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
94 Assert.assertNotEquals(exp, new Exception());
98 public void testExceptionMessageWithStackTraceConstructor4() throws Exception {
99 ValidationException.ExceptionMessageWithStackTrace exp =
100 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
101 Assert.assertEquals(exp, new ValidationException.ExceptionMessageWithStackTrace("string1", "string2"));
105 public void testEqual() throws Exception {
106 ValidationException.ExceptionMessageWithStackTrace exp =
107 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
108 ValidationException.ExceptionMessageWithStackTrace exp2 =
109 new ValidationException.ExceptionMessageWithStackTrace(null, "string2");
110 Assert.assertNotEquals(exp, exp2);
114 public void testEqual2() throws Exception {
115 ValidationException.ExceptionMessageWithStackTrace exp =
116 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
117 ValidationException.ExceptionMessageWithStackTrace exp2 =
118 new ValidationException.ExceptionMessageWithStackTrace("different", "string2");
119 Assert.assertNotEquals(exp, exp2);
124 public void testEqual3() throws Exception {
125 ValidationException.ExceptionMessageWithStackTrace exp =
126 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
127 ValidationException.ExceptionMessageWithStackTrace exp2 =
128 new ValidationException.ExceptionMessageWithStackTrace("string1", null);
129 Assert.assertNotEquals(exp, exp2);
133 public void testEqual4() throws Exception {
134 ValidationException.ExceptionMessageWithStackTrace exp =
135 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
136 ValidationException.ExceptionMessageWithStackTrace exp2 =
137 new ValidationException.ExceptionMessageWithStackTrace("string1", "different");
138 Assert.assertNotEquals(exp, exp2);
142 public void testEqual5() throws Exception {
143 ValidationException.ExceptionMessageWithStackTrace exp =
144 new ValidationException.ExceptionMessageWithStackTrace(null, "string2");
145 ValidationException.ExceptionMessageWithStackTrace exp2 =
146 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
147 Assert.assertNotEquals(exp, exp2);
151 public void testEqual6() throws Exception {
152 ValidationException.ExceptionMessageWithStackTrace exp =
153 new ValidationException.ExceptionMessageWithStackTrace("string1", null);
154 ValidationException.ExceptionMessageWithStackTrace exp2 =
155 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
156 Assert.assertNotEquals(exp, exp2);