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;
8 import com.google.common.collect.Lists;
10 import org.junit.Assert;
11 import org.junit.Test;
13 public class ValidationExceptionTest {
15 private final String instance = "instance";
16 private final ModuleIdentifier mi = new ModuleIdentifier("module", instance);
17 private final String instance2 = "instance2";
18 private final ModuleIdentifier mi2 = new ModuleIdentifier("module", instance2);
19 private final String message = "ex message";
20 private final Exception e = new IllegalStateException(message);
23 public void testCreateFromCollectedValidationExceptions() throws Exception {
24 ValidationException single = ValidationException.createForSingleException(mi, e);
25 ValidationException single2 = ValidationException.createForSingleException(mi2, e);
27 ValidationException collected = ValidationException.createFromCollectedValidationExceptions(Lists.newArrayList(single, single2));
29 Map<String, Map<String, ValidationException.ExceptionMessageWithStackTrace>> failedMap = collected.getFailedValidations();
30 assertEquals(1, failedMap.size());
31 assertTrue(failedMap.containsKey("module"));
33 Map<String, ValidationException.ExceptionMessageWithStackTrace> failedModule = failedMap.get("module");
34 assertEquals(2, failedModule.size());
35 assertTrue(failedModule.containsKey(instance));
36 assertEquals(message, failedModule.get(instance).getMessage());
37 assertEquals(message, failedModule.get(instance2).getMessage());
38 assertEquals(failedModule.get(instance), failedModule.get(instance2));
42 public void testCreateFromCollectedValidationExceptionsWithDuplicate() throws Exception {
43 ValidationException single = ValidationException.createForSingleException(mi, e);
44 ValidationException single2 = ValidationException.createForSingleException(mi, e);
46 ValidationException.createFromCollectedValidationExceptions(Lists.newArrayList(single, single2));
47 } catch (IllegalArgumentException ex) {
48 // Duplicate exception
49 assertThat(ex.getMessage(), containsString("Cannot merge"));
52 fail("Duplicate exception should have failed");
56 public void testGetTrace() throws Exception {
57 ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace();
58 exp.setTrace("trace");
59 Assert.assertEquals(exp.getTrace(), "trace");
63 public void testSetMessage() throws Exception {
64 ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace();
65 exp.setMessage("message");
66 Assert.assertEquals(exp.getMessage(), "message");
70 public void testHashCode() throws Exception {
71 ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace();
72 Assert.assertEquals(exp.hashCode(), new ValidationException.ExceptionMessageWithStackTrace().hashCode());
76 public void testExceptionMessageWithStackTraceConstructor() throws Exception {
77 ValidationException.ExceptionMessageWithStackTrace exp =
78 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
79 Assert.assertEquals(exp, exp);
83 public void testExceptionMessageWithStackTraceConstructor2() throws Exception {
84 ValidationException.ExceptionMessageWithStackTrace exp =
85 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
86 Assert.assertNotEquals(exp, null);
90 public void testExceptionMessageWithStackTraceConstructor3() throws Exception {
91 ValidationException.ExceptionMessageWithStackTrace exp =
92 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
93 Assert.assertNotEquals(exp, new Exception());
97 public void testExceptionMessageWithStackTraceConstructor4() throws Exception {
98 ValidationException.ExceptionMessageWithStackTrace exp =
99 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
100 Assert.assertEquals(exp, new ValidationException.ExceptionMessageWithStackTrace("string1", "string2"));
104 public void testEqual() throws Exception {
105 ValidationException.ExceptionMessageWithStackTrace exp =
106 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
107 ValidationException.ExceptionMessageWithStackTrace exp2 =
108 new ValidationException.ExceptionMessageWithStackTrace(null, "string2");
109 Assert.assertNotEquals(exp, exp2);
113 public void testEqual2() throws Exception {
114 ValidationException.ExceptionMessageWithStackTrace exp =
115 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
116 ValidationException.ExceptionMessageWithStackTrace exp2 =
117 new ValidationException.ExceptionMessageWithStackTrace("different", "string2");
118 Assert.assertNotEquals(exp, exp2);
123 public void testEqual3() throws Exception {
124 ValidationException.ExceptionMessageWithStackTrace exp =
125 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
126 ValidationException.ExceptionMessageWithStackTrace exp2 =
127 new ValidationException.ExceptionMessageWithStackTrace("string1", null);
128 Assert.assertNotEquals(exp, exp2);
132 public void testEqual4() throws Exception {
133 ValidationException.ExceptionMessageWithStackTrace exp =
134 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
135 ValidationException.ExceptionMessageWithStackTrace exp2 =
136 new ValidationException.ExceptionMessageWithStackTrace("string1", "different");
137 Assert.assertNotEquals(exp, exp2);
141 public void testEqual5() throws Exception {
142 ValidationException.ExceptionMessageWithStackTrace exp =
143 new ValidationException.ExceptionMessageWithStackTrace(null, "string2");
144 ValidationException.ExceptionMessageWithStackTrace exp2 =
145 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
146 Assert.assertNotEquals(exp, exp2);
150 public void testEqual6() throws Exception {
151 ValidationException.ExceptionMessageWithStackTrace exp =
152 new ValidationException.ExceptionMessageWithStackTrace("string1", null);
153 ValidationException.ExceptionMessageWithStackTrace exp2 =
154 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
155 Assert.assertNotEquals(exp, exp2);