Changes for checkstyle 8.32
[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 import org.junit.Test;
31 import org.onap.policy.apex.model.modelapi.ApexApiResult.Result;
32
33 /**
34  * Test API results.
35  * @author Liam Fallon (liam.fallon@ericsson.com)
36  */
37 public class ApexApiResultTest {
38
39     @Test
40     public void testApiResult() {
41         assertNotNull(new ApexApiResult());
42
43         for (final Result result : Result.values()) {
44             assertNotNull(new ApexApiResult(result));
45         }
46
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")));
50
51         final ApexApiResult result =
52                 new ApexApiResult(Result.FAILED, "Result Message", new IOException("IO Exception message"));
53
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));
58
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());
66
67         result2.setResult(Result.OTHER_ERROR);
68         assertEquals(Result.OTHER_ERROR, result2.getResult());
69
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));
75
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());
79     }
80 }