Update the aai-common with the latest code
[aai/aai-common.git] / aai-core / src / test / java / org / openecomp / aai / exceptions / AAIExceptionWithInfoTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.aai.exceptions;
22
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.openecomp.aai.serialization.queryformats.QueryFormatTestHelper;
26 import org.openecomp.aai.util.AAIConstants;
27
28 import java.util.HashMap;
29
30 import static org.junit.Assert.assertEquals;
31
32 public class AAIExceptionWithInfoTest {
33
34         
35         private static final HashMap<String, Object> map = new HashMap<String, Object>();
36
37         {
38                 map.put("itemInteger", 1);
39                 map.put("itemString", "Two");
40                 map.put("itemThree", Boolean.TRUE);
41         }
42
43         private static final String info = "An error has occurred.";
44         private static final String code = "AAI_4004";
45         private static final String details = "This is a detailed description of the exception.";
46         private static final Throwable cause = new RuntimeException("This is a runtime exception.");
47
48         @BeforeClass
49         public static void configure() throws NoSuchFieldException, SecurityException, Exception {
50                 System.setProperty("AJSC_HOME", ".");
51                 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
52                 QueryFormatTestHelper.setFinalStatic(AAIConstants.class.getField("AAI_HOME_ETC_OXM"), "src/test/resources/org/openecomp/aai/introspection/");
53         }
54
55         /**
56          * Test constructor with 2 params.
57          *
58          * @throws Exception the exception
59          */
60         @Test
61         public void testConstructorWith2Params() throws Exception {
62                 AAIExceptionWithInfo exception = new AAIExceptionWithInfo(map, info);
63
64                 assertEquals(map, exception.getInfoHash());
65                 assertEquals(info, exception.getInfo());
66         }
67
68         /**
69          * Test constructor with 3 params.
70          *
71          * @throws Exception the exception
72          */
73         @Test
74         public void testConstructorWith3Params() throws Exception {
75                 AAIExceptionWithInfo exception = new AAIExceptionWithInfo(code, map, info);
76
77                 assertEquals("4004", exception.getErrorObject().getErrorCode());
78                 assertEquals(map, exception.getInfoHash());
79                 assertEquals(info, exception.getInfo());
80         }
81
82         /**
83          * Test constructor with 4 params I.
84          *
85          * @throws Exception the exception
86          */
87         @Test
88         public void testConstructorWith4ParamsI() throws Exception {
89                 AAIExceptionWithInfo exception = new AAIExceptionWithInfo(code, details, map, info);
90
91                 assertEquals("4004", exception.getErrorObject().getErrorCode());
92                 assertEquals(details, exception.getMessage());
93                 assertEquals(map, exception.getInfoHash());
94                 assertEquals(info, exception.getInfo());
95         }
96
97         /**
98          * Test constructor with 4 params II.
99          *
100          * @throws Exception the exception
101          */
102         @Test
103         public void testConstructorWith4ParamsII() throws Exception {
104                 AAIExceptionWithInfo exception = new AAIExceptionWithInfo(code, cause, map, info);
105
106                 assertEquals("4004", exception.getErrorObject().getErrorCode());
107                 assertEquals(cause.toString(), exception.getMessage());
108                 assertEquals(map, exception.getInfoHash());
109                 assertEquals(info, exception.getInfo());
110         }
111
112         /**
113          * Test constructor with 5 params.
114          *
115          * @throws Exception the exception
116          */
117         @Test
118         public void testConstructorWith5Params() throws Exception {
119                 AAIExceptionWithInfo exception = new AAIExceptionWithInfo(code, cause, details, map, info);
120
121                 assertEquals("4004", exception.getErrorObject().getErrorCode());
122                 assertEquals(details, exception.getMessage());
123                 assertEquals(map, exception.getInfoHash());
124                 assertEquals(info, exception.getInfo());
125         }
126
127         /**
128          * Test set info hash.
129          */
130         @Test
131         public void testSetInfoHash() {
132                 AAIExceptionWithInfo exception = new AAIExceptionWithInfo(map, info);
133
134                 HashMap<String, Object> newMap = new HashMap<String, Object>();
135                 newMap.put("itemInteger", 2);
136                 exception.setInfoHash(newMap);
137                 
138                 assertEquals(newMap, exception.getInfoHash());
139                 assertEquals(info, exception.getInfo());
140         }
141         
142         /**
143          * Test set info.
144          */
145         @Test
146         public void testSetInfo() {
147                 AAIExceptionWithInfo exception = new AAIExceptionWithInfo(map, info);
148
149                 String newInfo = "This is updated info.";
150                 exception.setInfo(newInfo);
151                 
152                 assertEquals(map, exception.getInfoHash());
153                 assertEquals(newInfo, exception.getInfo());
154         }
155
156 }