Merge "AAI id mapping in SOL002 Adapter"
[so.git] / graph-inventory / aai-client / 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 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.List;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.onap.so.client.aai.entities.AAIError;
33 import org.onap.so.client.aai.entities.RequestError;
34 import org.onap.so.client.aai.entities.ServiceException;
35
36 public class AAIErrorFormatterTest {
37
38     @Mock
39     private AAIError errorObj;
40
41     @Before
42     public void init() {
43         MockitoAnnotations.initMocks(this);
44     }
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 errorMessageOnPercentEncodedTest() {
83         ServiceException svcException = new ServiceException();
84         svcException.setText("test my%20Test %1 message - %2");
85         svcException.setVariables(Arrays.asList("error", "service exception %1 test"));
86
87         RequestError requestError = new RequestError();
88         requestError.setServiceException(svcException);
89
90         doReturn(requestError).when(errorObj).getRequestError();
91
92         AAIErrorFormatter formatter = new AAIErrorFormatter(errorObj);
93         String result = formatter.getMessage();
94         assertEquals("equal", "test my%20Test error message - service exception error test", result);
95     }
96
97     @Test
98     public void testGetMessageNoParsable() {
99         errorObj.setRequestError(null);
100         AAIErrorFormatter formatter = new AAIErrorFormatter(errorObj);
101         String result = formatter.getMessage();
102         assertEquals("equal", "no parsable error message found", result);
103     }
104 }