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 ******************************************************************************/
22 package org.onap.aaf.cadi.aaf.client.test;
24 import static org.junit.Assert.assertEquals;
25 import static org.mockito.Mockito.when;
27 import java.io.ByteArrayOutputStream;
28 import java.io.PrintStream;
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;
43 import aaf.v2_0.Error;
45 public class JU_ErrMessageTest {
48 private RosettaEnv env;
50 @Mock(answer=Answers.RETURNS_DEEP_STUBS)
51 private RosettaDF<Object> errDF;
53 private ErrMessage errMessage;
55 private String attErrJson = "key:value";
59 private Future<?> future;
61 private ByteArrayOutputStream errStream;
64 public void setUp() throws Exception {
65 MockitoAnnotations.initMocks(this);
67 when(env.newDataFactory(Error.class)).thenReturn(errDF);
69 future = new Future<Error>() {
72 public boolean get(int timeout) throws CadiException {
82 public String body() {
87 public String header(String tag) {
93 error.setMessageId("Error Message Id");
94 error.setText("Error Text");
95 errMessage = new ErrMessage(env);
97 errStream = new ByteArrayOutputStream();
101 public void testPrintErrMessage() throws APIException {
102 when(errDF.newData().in(TYPE.JSON).load(attErrJson).asObject()).thenReturn(error);
104 errMessage.printErr(new PrintStream(errStream), attErrJson);
105 assertEquals("Error Message Id Error Text" + System.lineSeparator(), errStream.toString());
109 public void testToMsgJsonErrAttribute() throws APIException {
110 when(errDF.newData().in(TYPE.JSON).load(attErrJson).asObject()).thenReturn(error);
112 StringBuilder sb = new StringBuilder();
113 errMessage.toMsg(sb,attErrJson);
115 assertEquals(sb.toString(),"Error Message Id Error Text");
119 public void testToMsgFuture() {
120 StringBuilder sb = errMessage.toMsg(future);
122 assertEquals(sb.toString(), "0: Body");
127 public void testToMsgFutureWithoutException() throws APIException {
128 when(errDF.newData().in(TYPE.JSON).load(future.body()).asObject()).thenReturn(error);
130 StringBuilder sb = errMessage.toMsg(future);
132 assertEquals(sb.toString(), "Error Message Id Error Text");