1 /*******************************************************************************
2 * ============LICENSE_START====================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
21 ******************************************************************************/
23 package org.onap.aaf.cadi.aaf.client.test;
25 import static org.junit.Assert.assertEquals;
26 import static org.mockito.Mockito.when;
28 import java.io.ByteArrayOutputStream;
29 import java.io.PrintStream;
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;
44 import aaf.v2_0.Error;
46 public class JU_ErrMessageTest {
49 private RosettaEnv env;
51 @Mock(answer=Answers.RETURNS_DEEP_STUBS)
52 private RosettaDF<Object> errDF;
54 private ErrMessage errMessage;
56 private String attErrJson = "key:value";
60 private Future<?> future;
62 private ByteArrayOutputStream errStream;
65 public void setUp() throws Exception {
66 MockitoAnnotations.initMocks(this);
68 when(env.newDataFactory(Error.class)).thenReturn(errDF);
70 future = new Future<Error>() {
73 public boolean get(int timeout) throws CadiException {
83 public String body() {
88 public String header(String tag) {
94 error.setMessageId("Error Message Id");
95 error.setText("Error Text");
96 errMessage = new ErrMessage(env);
98 errStream = new ByteArrayOutputStream();
102 public void testPrintErrMessage() throws APIException {
103 when(errDF.newData().in(TYPE.JSON).load(attErrJson).asObject()).thenReturn(error);
105 errMessage.printErr(new PrintStream(errStream), attErrJson);
106 assertEquals("Error Message Id Error Text" + System.lineSeparator(), errStream.toString());
110 public void testToMsgJsonErrAttribute() throws APIException {
111 when(errDF.newData().in(TYPE.JSON).load(attErrJson).asObject()).thenReturn(error);
113 StringBuilder sb = new StringBuilder();
114 errMessage.toMsg(sb,attErrJson);
116 assertEquals(sb.toString(),"Error Message Id Error Text");
120 public void testToMsgFuture() {
121 StringBuilder sb = errMessage.toMsg(future);
123 assertEquals(sb.toString(), "0: Body");
128 public void testToMsgFutureWithoutException() throws APIException {
129 when(errDF.newData().in(TYPE.JSON).load(future.body()).asObject()).thenReturn(error);
131 StringBuilder sb = errMessage.toMsg(future);
133 assertEquals(sb.toString(), "Error Message Id Error Text");