AT&T 2.0.19 Code drop, stage 2
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / aaf / client / JU_ErrMessageTest.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
4  * * ===========================================================================
5  * * Copyright © 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  ******************************************************************************/
22 package org.onap.aaf.cadi.aaf.client;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.mockito.Mockito.when;
26
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.Answers;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.onap.aaf.cadi.CadiException;
33 import org.onap.aaf.cadi.aaf.client.ErrMessage;
34 import org.onap.aaf.cadi.client.Future;
35 import org.onap.aaf.misc.env.APIException;
36 import org.onap.aaf.misc.env.Data.TYPE;
37 import org.onap.aaf.misc.rosetta.env.RosettaDF;
38 import org.onap.aaf.misc.rosetta.env.RosettaEnv;
39
40 import aaf.v2_0.Error;
41
42 public class JU_ErrMessageTest {
43         
44         @Mock
45         private RosettaEnv env;
46         
47         @Mock(answer=Answers.RETURNS_DEEP_STUBS)
48         private RosettaDF<Object> errDF;
49
50         private ErrMessage errMessage;
51
52         private String attErrJson = "key:value";
53         
54         private Error error;
55
56         private Future<?> future;
57         
58         @Before
59         public void setUp() throws Exception {
60                 MockitoAnnotations.initMocks(this);
61                 
62                 when(env.newDataFactory(Error.class)).thenReturn(errDF);
63                 
64                 future = new Future<Error>() {
65
66                         @Override
67                         public boolean get(int timeout) throws CadiException {
68                                 return false;
69                         }
70
71                         @Override
72                         public int code() {
73                                 return 0;
74                         }
75
76                         @Override
77                         public String body() {
78                                 return "Body";
79                         }
80
81                         @Override
82                         public String header(String tag) {
83                                 return "header";
84                         }
85                 };
86                 
87                 error = new Error();
88                 error.setMessageId("Error Message Id");
89                 error.setText("Error Text");
90                 errMessage = new ErrMessage(env);
91                 
92                 
93         }
94
95         @Test
96         public void testPrintErrMessage() throws APIException {
97                 when(errDF.newData().in(TYPE.JSON).load(attErrJson).asObject()).thenReturn(error);
98                 
99                 errMessage.printErr(System.out, attErrJson);
100         }
101         
102         @Test
103         public void testToMsgJsonErrAttribute() throws APIException {
104                 when(errDF.newData().in(TYPE.JSON).load(attErrJson).asObject()).thenReturn(error);
105                 
106                 StringBuilder sb = new StringBuilder();
107                 errMessage.toMsg(sb,attErrJson);
108                 
109                 assertEquals(sb.toString(),"Error Message Id Error Text");
110         }
111         
112         @Test
113         public void testToMsgFuture() {
114                 StringBuilder sb = errMessage.toMsg(future);
115                 
116                 assertEquals(sb.toString(), "0: Body");
117         }
118
119         
120         @Test
121         public void testToMsgFutureWithoutException() throws APIException {
122                 when(errDF.newData().in(TYPE.JSON).load(future.body()).asObject()).thenReturn(error);
123                 
124                 StringBuilder sb = errMessage.toMsg(future);
125                 
126                 assertEquals(sb.toString(), "Error Message Id Error Text");
127         }
128 }