bea927afa5cb27fe190e593f44587cfea006e2cb
[so.git] / common / src / test / java / org / onap / so / client / aai / AAIErrorFormatterTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.onap.so.client.aai;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Mockito.doReturn;
25
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
29
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.onap.so.client.aai.entities.AAIError;
35 import org.onap.so.client.aai.entities.RequestError;
36 import org.onap.so.client.aai.entities.ServiceException;
37
38 public class AAIErrorFormatterTest {
39
40         @Mock private AAIError errorObj;
41         
42         @Before
43         public void init() {
44                 MockitoAnnotations.initMocks(this);
45         }
46         @Test
47         public void testFillInTemplateWithReplace() {
48                 String error = "Error %1 on %2";
49                 List<String> list = Arrays.asList("PUT", "hello %1");
50                 AAIErrorFormatter formatter = new AAIErrorFormatter(errorObj);
51                 String result = formatter.fillInTemplate(error, list);
52                 assertEquals("equal", "Error PUT on hello PUT", result);
53                 
54         }
55         
56         @Test
57         public void testFillInTemplateWithoutReplace() {
58                 String error = "Error";
59                 List<String> list = new ArrayList<>();
60                 AAIErrorFormatter formatter = new AAIErrorFormatter(errorObj);
61                 String result = formatter.fillInTemplate(error, list);
62                 assertEquals("equal", "Error", result);
63         }
64         
65         @Test
66         public void testGetMessage() {
67                 ServiceException svcException = new ServiceException();
68                 svcException.setText("test %1 message - %2");
69                 svcException.setVariables(Arrays.asList("error", "service exception %1 test"));
70                 
71                 RequestError requestError = new RequestError();
72                 requestError.setServiceException(svcException);
73                 
74                 doReturn(requestError).when(errorObj).getRequestError();
75                 
76                 AAIErrorFormatter formatter = new AAIErrorFormatter(errorObj);
77                 String result = formatter.getMessage();
78                 assertEquals("equal", "test error message - service exception error test", result);
79         }
80         
81         @Test
82         public void testGetMessageNoParsable() {
83                 errorObj.setRequestError(null);
84                 AAIErrorFormatter formatter = new AAIErrorFormatter(errorObj);
85                 String result = formatter.getMessage();
86                 assertEquals("equal", "no parsable error message found", result);
87         }
88 }