43a9fa42eb965af924c01dac9bf746a835fc42f3
[policy/apex-pdp.git] / model / model-api / src / test / java / org / onap / policy / apex / model / modelapi / ApexApiResultTest.java
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.modelapi;
22
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;
27
28 import java.io.IOException;
29 import java.util.Arrays;
30
31 import org.junit.Test;
32 import org.onap.policy.apex.model.modelapi.ApexApiResult.Result;
33
34 /**
35  * Test API results.
36  * @author Liam Fallon (liam.fallon@ericsson.com)
37  */
38 public class ApexApiResultTest {
39
40     @Test
41     public void testApiResult() {
42         assertNotNull(new ApexApiResult());
43
44         for (final Result result : Result.values()) {
45             assertNotNull(new ApexApiResult(result));
46         }
47
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")));
51
52         final ApexApiResult result =
53                 new ApexApiResult(Result.FAILED, "Result Message", new IOException("IO Exception message"));
54
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));
59
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());
67
68         result2.setResult(Result.OTHER_ERROR);
69         assertEquals(Result.OTHER_ERROR, result2.getResult());
70
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));
76
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());
80     }
81 }