2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.config.api;
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;
17 import com.google.common.collect.Lists;
19 import org.junit.Assert;
20 import org.junit.Test;
22 public class ValidationExceptionTest {
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 e = new IllegalStateException(message);
32 public void testCreateFromCollectedValidationExceptions() throws Exception {
33 ValidationException single = ValidationException.createForSingleException(mi, e);
34 ValidationException single2 = ValidationException.createForSingleException(mi2, e);
36 ValidationException collected = ValidationException.createFromCollectedValidationExceptions(Lists.newArrayList(single, single2));
38 Map<String, Map<String, ValidationException.ExceptionMessageWithStackTrace>> failedMap = collected.getFailedValidations();
39 assertEquals(1, failedMap.size());
40 assertTrue(failedMap.containsKey("module"));
42 Map<String, ValidationException.ExceptionMessageWithStackTrace> failedModule = failedMap.get("module");
43 assertEquals(2, failedModule.size());
44 assertTrue(failedModule.containsKey(instance));
45 assertEquals(message, failedModule.get(instance).getMessage());
46 assertEquals(message, failedModule.get(instance2).getMessage());
47 assertEquals(failedModule.get(instance), failedModule.get(instance2));
51 public void testCreateFromCollectedValidationExceptionsWithDuplicate() throws Exception {
52 ValidationException single = ValidationException.createForSingleException(mi, e);
53 ValidationException single2 = ValidationException.createForSingleException(mi, e);
55 ValidationException.createFromCollectedValidationExceptions(Lists.newArrayList(single, single2));
56 } catch (IllegalArgumentException ex) {
57 // Duplicate exception
58 assertThat(ex.getMessage(), containsString("Cannot merge"));
61 fail("Duplicate exception should have failed");
65 public void testGetTrace() throws Exception {
66 ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace();
67 exp.setTrace("trace");
68 Assert.assertEquals(exp.getTrace(), "trace");
72 public void testSetMessage() throws Exception {
73 ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace();
74 exp.setMessage("message");
75 Assert.assertEquals(exp.getMessage(), "message");
79 public void testHashCode() throws Exception {
80 ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace();
81 Assert.assertEquals(exp.hashCode(), new ValidationException.ExceptionMessageWithStackTrace().hashCode());
85 public void testExceptionMessageWithStackTraceConstructor() throws Exception {
86 ValidationException.ExceptionMessageWithStackTrace exp =
87 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
88 Assert.assertEquals(exp, exp);
92 public void testExceptionMessageWithStackTraceConstructor2() throws Exception {
93 ValidationException.ExceptionMessageWithStackTrace exp =
94 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
95 Assert.assertNotEquals(exp, null);
99 public void testExceptionMessageWithStackTraceConstructor3() throws Exception {
100 ValidationException.ExceptionMessageWithStackTrace exp =
101 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
102 Assert.assertNotEquals(exp, new Exception());
106 public void testExceptionMessageWithStackTraceConstructor4() throws Exception {
107 ValidationException.ExceptionMessageWithStackTrace exp =
108 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
109 Assert.assertEquals(exp, new ValidationException.ExceptionMessageWithStackTrace("string1", "string2"));
113 public void testEqual() throws Exception {
114 ValidationException.ExceptionMessageWithStackTrace exp =
115 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
116 ValidationException.ExceptionMessageWithStackTrace exp2 =
117 new ValidationException.ExceptionMessageWithStackTrace(null, "string2");
118 Assert.assertNotEquals(exp, exp2);
122 public void testEqual2() throws Exception {
123 ValidationException.ExceptionMessageWithStackTrace exp =
124 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
125 ValidationException.ExceptionMessageWithStackTrace exp2 =
126 new ValidationException.ExceptionMessageWithStackTrace("different", "string2");
127 Assert.assertNotEquals(exp, exp2);
132 public void testEqual3() throws Exception {
133 ValidationException.ExceptionMessageWithStackTrace exp =
134 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
135 ValidationException.ExceptionMessageWithStackTrace exp2 =
136 new ValidationException.ExceptionMessageWithStackTrace("string1", null);
137 Assert.assertNotEquals(exp, exp2);
141 public void testEqual4() throws Exception {
142 ValidationException.ExceptionMessageWithStackTrace exp =
143 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
144 ValidationException.ExceptionMessageWithStackTrace exp2 =
145 new ValidationException.ExceptionMessageWithStackTrace("string1", "different");
146 Assert.assertNotEquals(exp, exp2);
150 public void testEqual5() throws Exception {
151 ValidationException.ExceptionMessageWithStackTrace exp =
152 new ValidationException.ExceptionMessageWithStackTrace(null, "string2");
153 ValidationException.ExceptionMessageWithStackTrace exp2 =
154 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
155 Assert.assertNotEquals(exp, exp2);
159 public void testEqual6() throws Exception {
160 ValidationException.ExceptionMessageWithStackTrace exp =
161 new ValidationException.ExceptionMessageWithStackTrace("string1", null);
162 ValidationException.ExceptionMessageWithStackTrace exp2 =
163 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
164 Assert.assertNotEquals(exp, exp2);