2 * Copyright (c) 2014, 2017 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 exception = new IllegalStateException(message);
32 public void testCreateFromCollectedValidationExceptions() throws Exception {
33 ValidationException single = ValidationException.createForSingleException(mi, exception);
34 ValidationException single2 = ValidationException.createForSingleException(mi2, exception);
36 ValidationException collected = ValidationException
37 .createFromCollectedValidationExceptions(Lists.newArrayList(single, single2));
39 Map<String, Map<String, ValidationException.ExceptionMessageWithStackTrace>> failedMap = collected
40 .getFailedValidations();
41 assertEquals(1, failedMap.size());
42 assertTrue(failedMap.containsKey("module"));
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));
53 public void testCreateFromCollectedValidationExceptionsWithDuplicate() throws Exception {
54 ValidationException single = ValidationException.createForSingleException(mi, exception);
55 ValidationException single2 = ValidationException.createForSingleException(mi, exception);
57 ValidationException.createFromCollectedValidationExceptions(Lists.newArrayList(single, single2));
58 } catch (final IllegalArgumentException ex) {
59 // Duplicate exception
60 assertThat(ex.getMessage(), containsString("Cannot merge"));
63 fail("Duplicate exception should have failed");
67 public void testGetTrace() throws Exception {
68 ValidationException.ExceptionMessageWithStackTrace exp =
69 new ValidationException.ExceptionMessageWithStackTrace();
70 exp.setStackTrace("trace");
71 Assert.assertEquals(exp.getTrace(), "trace");
75 public void testSetMessage() throws Exception {
76 ValidationException.ExceptionMessageWithStackTrace exp =
77 new ValidationException.ExceptionMessageWithStackTrace();
78 exp.setMessage("message");
79 Assert.assertEquals(exp.getMessage(), "message");
83 public void testHashCode() throws Exception {
84 ValidationException.ExceptionMessageWithStackTrace exp =
85 new ValidationException.ExceptionMessageWithStackTrace();
86 Assert.assertEquals(exp.hashCode(), new ValidationException.ExceptionMessageWithStackTrace().hashCode());
90 public void testExceptionMessageWithStackTraceConstructor() throws Exception {
91 ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace(
92 "string1", "string2");
93 Assert.assertEquals(exp, exp);
97 public void testExceptionMessageWithStackTraceConstructor2() throws Exception {
98 ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace(
99 "string1", "string2");
100 Assert.assertNotEquals(exp, null);
104 public void testExceptionMessageWithStackTraceConstructor3() throws Exception {
105 ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace(
106 "string1", "string2");
107 Assert.assertNotEquals(exp, new Exception());
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"));
118 public void testEqual() throws Exception {
119 ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace(
120 "string1", "string2");
121 ValidationException.ExceptionMessageWithStackTrace exp2 =
122 new ValidationException.ExceptionMessageWithStackTrace(
124 Assert.assertNotEquals(exp, exp2);
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);
138 public void testEqual3() throws Exception {
139 ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace(
140 "string1", "string2");
141 ValidationException.ExceptionMessageWithStackTrace exp2 =
142 new ValidationException.ExceptionMessageWithStackTrace(
144 Assert.assertNotEquals(exp, exp2);
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);
158 public void testEqual5() throws Exception {
159 ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace(
161 ValidationException.ExceptionMessageWithStackTrace exp2 =
162 new ValidationException.ExceptionMessageWithStackTrace(
163 "string1", "string2");
164 Assert.assertNotEquals(exp, exp2);
168 public void testEqual6() throws Exception {
169 ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace(
171 ValidationException.ExceptionMessageWithStackTrace exp2 =
172 new ValidationException.ExceptionMessageWithStackTrace(
173 "string1", "string2");
174 Assert.assertNotEquals(exp, exp2);