7d193ce17019e585a39f19c01c9e63aff2090f9f
[policy/gui.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
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.gui.editors.apex.rest.handling;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24
25 import java.util.Random;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.Mockito;
29 import org.onap.policy.apex.model.modelapi.ApexApiResult;
30 import org.onap.policy.apex.model.modelapi.ApexModel;
31
32 public class KeyInfoHandlerTest {
33     private final Random random = new Random();
34     private KeyInfoHandler handler;
35
36     @Before
37     public void setUp() {
38         handler = new KeyInfoHandler();
39     }
40
41     @Test
42     public void testExecuteRestCommand() {
43         final var sessionId = random.nextInt();
44         final var session = new RestSession(sessionId);
45         final var commandType = RestCommandType.EVENT;
46         final var command = RestCommand.ANALYSE;
47
48         final var actual = handler.executeRestCommand(session, commandType, command);
49
50         assertThat(actual.getResult()).isEqualTo(ApexApiResult.Result.FAILED);
51         assertThat(actual.getMessage()).contains(Integer.toString(sessionId));
52         assertThat(actual.getMessage()).contains(commandType.toString());
53         assertThat(actual.getMessage()).contains(command.toString());
54     }
55
56     @Test
57     public void testExecuteRestCommandWithJsonString() {
58         final var sessionId = random.nextInt();
59         final var session = new RestSession(sessionId);
60         final var commandType = RestCommandType.EVENT;
61         final var command = RestCommand.ANALYSE;
62         final var emptyString = "";
63
64         final var actual = handler.executeRestCommand(session, commandType, command, emptyString);
65
66         assertThat(actual.getResult()).isEqualTo(ApexApiResult.Result.FAILED);
67         assertThat(actual.getMessage()).contains(Integer.toString(sessionId));
68         assertThat(actual.getMessage()).contains(commandType.toString());
69         assertThat(actual.getMessage()).contains(command.toString());
70     }
71
72     @Test
73     public void testExecuteRestCommandWithNameAndVersion() {
74         final var sessionId = random.nextInt();
75         final var session = new RestSession(sessionId);
76         final var commandType = RestCommandType.EVENT;
77         final var command = RestCommand.ANALYSE;
78         final var name = "";
79         final var version = "";
80
81         final var actual = handler.executeRestCommand(session, commandType, command, name, version);
82
83         assertThat(actual.getResult()).isEqualTo(ApexApiResult.Result.FAILED);
84         assertThat(actual.getMessage()).contains(Integer.toString(sessionId));
85         assertThat(actual.getMessage()).contains(commandType.toString());
86         assertThat(actual.getMessage()).contains(command.toString());
87     }
88
89     @Test
90     public void testExecuteRestCommandWithNameAndVersion2() {
91         final var session = Mockito.mock(RestSession.class);
92         final var commandType = RestCommandType.KEY_INFO;
93         final var command = RestCommand.LIST;
94         final var name = "";
95         final var version = "version";
96         final var expected = new ApexApiResult();
97         final var apexModel = Mockito.mock(ApexModel.class);
98
99         Mockito.when(session.getApexModel()).thenReturn(apexModel);
100         Mockito.when(apexModel.listKeyInformation(null, version)).thenReturn(expected);
101
102         final var actual = handler.executeRestCommand(session, commandType, command, name, version);
103
104         assertThat(actual).isEqualTo(expected);
105     }
106 }