1 package org.opendaylight.controller.config.api;
3 import static junit.framework.Assert.assertEquals;
4 import static junit.framework.Assert.assertTrue;
5 import static junit.framework.Assert.fail;
6 import static org.junit.Assert.assertNotEquals;
7 import static org.junit.Assert.assertThat;
8 import static org.junit.matchers.JUnitMatchers.containsString;
10 import com.google.common.collect.Lists;
13 import org.junit.Assert;
14 import org.junit.Test;
16 public class ValidationExceptionTest {
18 private String instance = "instance";
19 private final ModuleIdentifier mi = new ModuleIdentifier("module", instance);
20 private String instance2 = "instance2";
21 private final ModuleIdentifier mi2 = new ModuleIdentifier("module", instance2);
22 private final String message = "ex message";
23 private final Exception e = new IllegalStateException(message);
26 public void testCreateFromCollectedValidationExceptions() throws Exception {
27 ValidationException single = ValidationException.createForSingleException(mi, e);
28 ValidationException single2 = ValidationException.createForSingleException(mi2, e);
30 ValidationException collected = ValidationException.createFromCollectedValidationExceptions(Lists.newArrayList(single, single2));
32 Map<String, Map<String, ValidationException.ExceptionMessageWithStackTrace>> failedMap = collected.getFailedValidations();
33 assertEquals(1, failedMap.size());
34 assertTrue(failedMap.containsKey("module"));
36 Map<String, ValidationException.ExceptionMessageWithStackTrace> failedModule = failedMap.get("module");
37 assertEquals(2, failedModule.size());
38 assertTrue(failedModule.containsKey(instance));
39 assertEquals(message, failedModule.get(instance).getMessage());
40 assertEquals(message, failedModule.get(instance2).getMessage());
41 assertEquals(failedModule.get(instance), failedModule.get(instance2));
45 public void testCreateFromCollectedValidationExceptionsWithDuplicate() throws Exception {
46 ValidationException single = ValidationException.createForSingleException(mi, e);
47 ValidationException single2 = ValidationException.createForSingleException(mi, e);
49 ValidationException.createFromCollectedValidationExceptions(Lists.newArrayList(single, single2));
50 } catch (IllegalArgumentException ex) {
51 // Duplicate exception
52 assertThat(ex.getMessage(), containsString("Cannot merge"));
55 fail("Duplicate exception should have failed");
59 public void testGetTrace() throws Exception {
60 ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace();
61 exp.setTrace("trace");
62 Assert.assertEquals(exp.getTrace(), "trace");
66 public void testSetMessage() throws Exception {
67 ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace();
68 exp.setMessage("message");
69 Assert.assertEquals(exp.getMessage(), "message");
73 public void testHashCode() throws Exception {
74 ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace();
75 Assert.assertEquals(exp.hashCode(), new ValidationException.ExceptionMessageWithStackTrace().hashCode());
79 public void testExceptionMessageWithStackTraceConstructor() throws Exception {
80 ValidationException.ExceptionMessageWithStackTrace exp =
81 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
82 Assert.assertEquals(exp, exp);
86 public void testExceptionMessageWithStackTraceConstructor2() throws Exception {
87 ValidationException.ExceptionMessageWithStackTrace exp =
88 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
89 Assert.assertNotEquals(exp, null);
93 public void testExceptionMessageWithStackTraceConstructor3() throws Exception {
94 ValidationException.ExceptionMessageWithStackTrace exp =
95 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
96 Assert.assertNotEquals(exp, new Exception());
100 public void testExceptionMessageWithStackTraceConstructor4() throws Exception {
101 ValidationException.ExceptionMessageWithStackTrace exp =
102 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
103 Assert.assertEquals(exp, new ValidationException.ExceptionMessageWithStackTrace("string1", "string2"));
107 public void testEqual() throws Exception {
108 ValidationException.ExceptionMessageWithStackTrace exp =
109 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
110 ValidationException.ExceptionMessageWithStackTrace exp2 =
111 new ValidationException.ExceptionMessageWithStackTrace(null, "string2");
112 Assert.assertNotEquals(exp, exp2);
116 public void testEqual2() throws Exception {
117 ValidationException.ExceptionMessageWithStackTrace exp =
118 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
119 ValidationException.ExceptionMessageWithStackTrace exp2 =
120 new ValidationException.ExceptionMessageWithStackTrace("different", "string2");
121 Assert.assertNotEquals(exp, exp2);
126 public void testEqual3() throws Exception {
127 ValidationException.ExceptionMessageWithStackTrace exp =
128 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
129 ValidationException.ExceptionMessageWithStackTrace exp2 =
130 new ValidationException.ExceptionMessageWithStackTrace("string1", null);
131 Assert.assertNotEquals(exp, exp2);
135 public void testEqual4() throws Exception {
136 ValidationException.ExceptionMessageWithStackTrace exp =
137 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
138 ValidationException.ExceptionMessageWithStackTrace exp2 =
139 new ValidationException.ExceptionMessageWithStackTrace("string1", "different");
140 Assert.assertNotEquals(exp, exp2);
144 public void testEqual5() throws Exception {
145 ValidationException.ExceptionMessageWithStackTrace exp =
146 new ValidationException.ExceptionMessageWithStackTrace(null, "string2");
147 ValidationException.ExceptionMessageWithStackTrace exp2 =
148 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
149 Assert.assertNotEquals(exp, exp2);
153 public void testEqual6() throws Exception {
154 ValidationException.ExceptionMessageWithStackTrace exp =
155 new ValidationException.ExceptionMessageWithStackTrace("string1", null);
156 ValidationException.ExceptionMessageWithStackTrace exp2 =
157 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
158 Assert.assertNotEquals(exp, exp2);