Merge "Fix checkstyle warnings in config-api"
[controller.git] / opendaylight / config / config-api / src / test / java / org / opendaylight / controller / config / api / ValidationExceptionTest.java
1 package org.opendaylight.controller.config.api;
2
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
9 import com.google.common.collect.Lists;
10 import java.util.Map;
11 import org.junit.Assert;
12 import org.junit.Test;
13
14 public class ValidationExceptionTest {
15
16     private final String instance = "instance";
17     private final ModuleIdentifier mi = new ModuleIdentifier("module", instance);
18     private final String instance2 = "instance2";
19     private final ModuleIdentifier mi2 = new ModuleIdentifier("module", instance2);
20     private final String message = "ex message";
21     private final Exception e = new IllegalStateException(message);
22
23     @Test
24     public void testCreateFromCollectedValidationExceptions() throws Exception {
25         ValidationException single = ValidationException.createForSingleException(mi, e);
26         ValidationException single2 = ValidationException.createForSingleException(mi2, e);
27
28         ValidationException collected = ValidationException.createFromCollectedValidationExceptions(Lists.newArrayList(single, single2));
29
30         Map<String, Map<String, ValidationException.ExceptionMessageWithStackTrace>> failedMap = collected.getFailedValidations();
31         assertEquals(1, failedMap.size());
32         assertTrue(failedMap.containsKey("module"));
33
34         Map<String, ValidationException.ExceptionMessageWithStackTrace> failedModule = failedMap.get("module");
35         assertEquals(2, failedModule.size());
36         assertTrue(failedModule.containsKey(instance));
37         assertEquals(message, failedModule.get(instance).getMessage());
38         assertEquals(message, failedModule.get(instance2).getMessage());
39         assertEquals(failedModule.get(instance), failedModule.get(instance2));
40     }
41
42     @Test
43     public void testCreateFromCollectedValidationExceptionsWithDuplicate() throws Exception {
44         ValidationException single = ValidationException.createForSingleException(mi, e);
45         ValidationException single2 = ValidationException.createForSingleException(mi, e);
46         try {
47             ValidationException.createFromCollectedValidationExceptions(Lists.newArrayList(single, single2));
48         } catch (IllegalArgumentException ex) {
49             // Duplicate exception
50             assertThat(ex.getMessage(), containsString("Cannot merge"));
51             return;
52         }
53         fail("Duplicate exception should have failed");
54     }
55
56     @Test
57     public void testGetTrace() throws Exception {
58         ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace();
59         exp.setTrace("trace");
60         Assert.assertEquals(exp.getTrace(), "trace");
61     }
62
63     @Test
64     public void testSetMessage() throws Exception {
65         ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace();
66         exp.setMessage("message");
67         Assert.assertEquals(exp.getMessage(), "message");
68     }
69
70     @Test
71     public void testHashCode() throws Exception {
72         ValidationException.ExceptionMessageWithStackTrace exp = new ValidationException.ExceptionMessageWithStackTrace();
73         Assert.assertEquals(exp.hashCode(), new ValidationException.ExceptionMessageWithStackTrace().hashCode());
74     }
75
76     @Test
77     public void testExceptionMessageWithStackTraceConstructor() throws Exception {
78         ValidationException.ExceptionMessageWithStackTrace exp =
79                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
80         Assert.assertEquals(exp, exp);
81     }
82
83     @Test
84     public void testExceptionMessageWithStackTraceConstructor2() throws Exception {
85         ValidationException.ExceptionMessageWithStackTrace exp =
86                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
87         Assert.assertNotEquals(exp, null);
88     }
89
90     @Test
91     public void testExceptionMessageWithStackTraceConstructor3() throws Exception {
92         ValidationException.ExceptionMessageWithStackTrace exp =
93                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
94         Assert.assertNotEquals(exp, new Exception());
95     }
96
97     @Test
98     public void testExceptionMessageWithStackTraceConstructor4() throws Exception {
99         ValidationException.ExceptionMessageWithStackTrace exp =
100                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
101         Assert.assertEquals(exp, new ValidationException.ExceptionMessageWithStackTrace("string1", "string2"));
102     }
103
104     @Test
105     public void testEqual() throws Exception {
106         ValidationException.ExceptionMessageWithStackTrace exp =
107                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
108         ValidationException.ExceptionMessageWithStackTrace exp2 =
109                 new ValidationException.ExceptionMessageWithStackTrace(null, "string2");
110         Assert.assertNotEquals(exp, exp2);
111     }
112
113     @Test
114     public void testEqual2() throws Exception {
115         ValidationException.ExceptionMessageWithStackTrace exp =
116                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
117         ValidationException.ExceptionMessageWithStackTrace exp2 =
118                 new ValidationException.ExceptionMessageWithStackTrace("different", "string2");
119         Assert.assertNotEquals(exp, exp2);
120     }
121
122
123     @Test
124     public void testEqual3() throws Exception {
125         ValidationException.ExceptionMessageWithStackTrace exp =
126                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
127         ValidationException.ExceptionMessageWithStackTrace exp2 =
128                 new ValidationException.ExceptionMessageWithStackTrace("string1", null);
129         Assert.assertNotEquals(exp, exp2);
130     }
131
132     @Test
133     public void testEqual4() throws Exception {
134         ValidationException.ExceptionMessageWithStackTrace exp =
135                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
136         ValidationException.ExceptionMessageWithStackTrace exp2 =
137                 new ValidationException.ExceptionMessageWithStackTrace("string1", "different");
138         Assert.assertNotEquals(exp, exp2);
139     }
140
141     @Test
142     public void testEqual5() throws Exception {
143         ValidationException.ExceptionMessageWithStackTrace exp =
144                 new ValidationException.ExceptionMessageWithStackTrace(null, "string2");
145         ValidationException.ExceptionMessageWithStackTrace exp2 =
146                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
147         Assert.assertNotEquals(exp, exp2);
148     }
149
150     @Test
151     public void testEqual6() throws Exception {
152         ValidationException.ExceptionMessageWithStackTrace exp =
153                 new ValidationException.ExceptionMessageWithStackTrace("string1", null);
154         ValidationException.ExceptionMessageWithStackTrace exp2 =
155                 new ValidationException.ExceptionMessageWithStackTrace("string1", "string2");
156         Assert.assertNotEquals(exp, exp2);
157     }
158 }