2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.modelapi;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
28 import java.io.IOException;
29 import java.util.Arrays;
30 import org.junit.Test;
31 import org.onap.policy.apex.model.modelapi.ApexApiResult.Result;
35 * @author Liam Fallon (liam.fallon@ericsson.com)
37 public class ApexApiResultTest {
40 public void testApiResult() {
41 assertNotNull(new ApexApiResult());
43 for (final Result result : Result.values()) {
44 assertNotNull(new ApexApiResult(result));
47 assertNotNull(new ApexApiResult(Result.SUCCESS, "Result Message"));
48 assertNotNull(new ApexApiResult(Result.FAILED, new IOException("IO Exception message")));
49 assertNotNull(new ApexApiResult(Result.FAILED, "Result Message", new IOException("IO Exception message")));
51 final ApexApiResult result =
52 new ApexApiResult(Result.FAILED, "Result Message", new IOException("IO Exception message"));
54 assertFalse(result.isOk());
55 assertTrue(result.isNok());
56 assertEquals(Result.FAILED, result.getResult());
57 assertEquals("Result Message\nIO Exception message\njava.io.IOExce", result.getMessage().substring(0, 50));
59 final ApexApiResult result2 = new ApexApiResult(Result.SUCCESS);
60 result2.addMessage(null);
61 assertEquals("", result2.getMessage());
62 result2.addMessage("");
63 assertEquals("", result2.getMessage());
64 result2.addMessage("funky message");
65 assertEquals("funky message\n", result2.getMessage());
67 result2.setResult(Result.OTHER_ERROR);
68 assertEquals(Result.OTHER_ERROR, result2.getResult());
70 final String[] messages = {"First Message", "Second Message", "Third Message"};
71 result2.setMessages(Arrays.asList(messages));
72 assertEquals("First Message", result2.getMessages().get(0));
73 assertEquals("Second Message", result2.getMessages().get(1));
74 assertEquals("Third Message", result2.getMessages().get(2));
76 assertEquals("result: OTHER_ERROR\nFirst Message\nSecond Message\nThird Message\n", result2.toString());
77 assertEquals("{\n" + "\"result\": \"OTHER_ERROR\",\n" + "\"messages\": [\n" + "\"First Message\",\n"
78 + "\"Second Message\",\n" + "\"Third Message\"]\n" + "}\n", result2.toJson());