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