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