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;
31 import org.junit.Test;
32 import org.onap.policy.apex.model.modelapi.ApexApiResult.Result;
36 * @author Liam Fallon (liam.fallon@ericsson.com)
38 public class ApexApiResultTest {
41 public void testApiResult() {
42 assertNotNull(new ApexApiResult());
44 for (final Result result : Result.values()) {
45 assertNotNull(new ApexApiResult(result));
48 assertNotNull(new ApexApiResult(Result.SUCCESS, "Result Message"));
49 assertNotNull(new ApexApiResult(Result.FAILED, new IOException("IO Exception message")));
50 assertNotNull(new ApexApiResult(Result.FAILED, "Result Message", new IOException("IO Exception message")));
52 final ApexApiResult result =
53 new ApexApiResult(Result.FAILED, "Result Message", new IOException("IO Exception message"));
55 assertFalse(result.isOk());
56 assertTrue(result.isNok());
57 assertEquals(Result.FAILED, result.getResult());
58 assertEquals("Result Message\nIO Exception message\njava.io.IOExce", result.getMessage().substring(0, 50));
60 final ApexApiResult result2 = new ApexApiResult(Result.SUCCESS);
61 result2.addMessage(null);
62 assertEquals("", result2.getMessage());
63 result2.addMessage("");
64 assertEquals("", result2.getMessage());
65 result2.addMessage("funky message");
66 assertEquals("funky message\n", result2.getMessage());
68 result2.setResult(Result.OTHER_ERROR);
69 assertEquals(Result.OTHER_ERROR, result2.getResult());
71 final String[] messages = {"First Message", "Second Message", "Third Message"};
72 result2.setMessages(Arrays.asList(messages));
73 assertEquals("First Message", result2.getMessages().get(0));
74 assertEquals("Second Message", result2.getMessages().get(1));
75 assertEquals("Third Message", result2.getMessages().get(2));
77 assertEquals("result: OTHER_ERROR\nFirst Message\nSecond Message\nThird Message\n", result2.toString());
78 assertEquals("{\n" + "\"result\": \"OTHER_ERROR\",\n" + "\"messages\": [\n" + "\"First Message\",\n"
79 + "\"Second Message\",\n" + "\"Third Message\"]\n" + "}\n", result2.toJson());