3f4bd858237de6fd712c9337fb1da190adb941dd
[controller.git] / opendaylight / config / config-api / src / test / java / org / opendaylight / controller / config / api / ValidationExceptionTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.config.api;
10
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;
16
17 import com.google.common.collect.Lists;
18 import java.util.Map;
19 import org.junit.Assert;
20 import org.junit.Test;
21
22 public class ValidationExceptionTest {
23
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);
30
31     @Test
32     public void testCreateFromCollectedValidationExceptions() throws Exception {
33         ValidationException single = ValidationException.createForSingleException(mi, e);
34         ValidationException single2 = ValidationException.createForSingleException(mi2, e);
35
36         ValidationException collected = ValidationException.createFromCollectedValidationExceptions(Lists.newArrayList(single, single2));
37
38         Map<String, Map<String, ValidationException.ExceptionMessageWithStackTrace>> failedMap = collected.getFailedValidations();
39         assertEquals(1, failedMap.size());
40         assertTrue(failedMap.containsKey("module"));
41
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));
48     }
49
50     @Test
51     public void testCreateFromCollectedValidationExceptionsWithDuplicate() throws Exception {
52         ValidationException single = ValidationException.createForSingleException(mi, e);
53         ValidationException single2 = ValidationException.createForSingleException(mi, e);
54         try {
55             ValidationException.createFromCollectedValidationExceptions(Lists.newArrayList(single, single2));
56         } catch (final IllegalArgumentException ex) {
57             // Duplicate exception
58             assertThat(ex.getMessage(), containsString("Cannot merge"));
59             return;
60         }
61         fail("Duplicate exception should have failed");
62     }
63
64     @Test
65     public void testGetTrace() throws Exception {
66         ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace();
67         exp.setTrace("trace");
68         Assert.assertEquals(exp.getTrace(), "trace");
69     }
70
71     @Test
72     public void testSetMessage() throws Exception {
73         ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace();
74         exp.setMessage("message");
75         Assert.assertEquals(exp.getMessage(), "message");
76     }
77
78     @Test
79     public void testHashCode() throws Exception {
80         ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace();
81         Assert.assertEquals(exp.hashCode(), new ValidationException.ExceptionMessageWithStackTrace().hashCode());
82     }
83
84     @Test
85     public void testExceptionMessageWithStackTraceConstructor() throws Exception {
86         ValidationException.ExceptionMessageWithStackTrace exp =
87                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
88         Assert.assertEquals(exp, exp);
89     }
90
91     @Test
92     public void testExceptionMessageWithStackTraceConstructor2() throws Exception {
93         ValidationException.ExceptionMessageWithStackTrace exp =
94                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
95         Assert.assertNotEquals(exp, null);
96     }
97
98     @Test
99     public void testExceptionMessageWithStackTraceConstructor3() throws Exception {
100         ValidationException.ExceptionMessageWithStackTrace exp =
101                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
102         Assert.assertNotEquals(exp, new Exception());
103     }
104
105     @Test
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"));
110     }
111
112     @Test
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);
119     }
120
121     @Test
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);
128     }
129
130
131     @Test
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);
138     }
139
140     @Test
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);
147     }
148
149     @Test
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);
156     }
157
158     @Test
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);
165     }
166 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.