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