2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.core.deployment;
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29 import static org.mockito.Matchers.anyObject;
31 import java.lang.reflect.Field;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.concurrent.TimeUnit;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.ArgumentCaptor;
38 import org.mockito.Mock;
39 import org.mockito.Mockito;
40 import org.mockito.runners.MockitoJUnitRunner;
41 import org.onap.policy.apex.core.infrastructure.messaging.MessageHolder;
42 import org.onap.policy.apex.core.infrastructure.messaging.MessageListener;
43 import org.onap.policy.apex.core.infrastructure.messaging.MessagingService;
44 import org.onap.policy.apex.core.infrastructure.messaging.MessagingServiceFactory;
45 import org.onap.policy.apex.core.infrastructure.messaging.impl.ws.messageblock.MessageBlock;
46 import org.onap.policy.apex.core.protocols.Message;
47 import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineStatus;
48 import org.onap.policy.apex.core.protocols.engdep.messages.Response;
49 import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
50 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
53 * Test the deployment web socket client.
55 @RunWith(MockitoJUnitRunner.class)
56 public class DeploymentClientTest {
58 private static MessagingServiceFactory<Message> mockServiceFactory;
61 private static MessagingService<Message> mockService;
63 @SuppressWarnings("rawtypes")
64 ArgumentCaptor<MessageListener> messageListener = ArgumentCaptor.forClass(MessageListener.class);
66 @SuppressWarnings("unchecked")
68 public void testDeploymentClientStart() throws Exception {
69 DeploymentClient deploymentClient = new DeploymentClient("localhost", 51332);
71 final Field factoryField = deploymentClient.getClass().getDeclaredField("factory");
72 factoryField.setAccessible(true);
73 factoryField.set(deploymentClient, mockServiceFactory);
75 Mockito.doReturn(mockService).when(mockServiceFactory).createClient(anyObject());
77 Mockito.doNothing().when(mockService).addMessageListener(messageListener.capture());
78 Mockito.doNothing().when(mockService).startConnection();
80 Mockito.doNothing().when(mockService).send((MessageHolder<Message>) anyObject());
82 Thread clientThread = new Thread(deploymentClient);
85 await().atMost(200, TimeUnit.MILLISECONDS).until(() -> deploymentClient.isStarted());
87 assertTrue(deploymentClient.isStarted());
88 assertTrue(clientThread.isAlive());
90 AxArtifactKey engineKey = new AxArtifactKey("MyEngine", "0.0.1");
91 GetEngineStatus getEngineStatus = new GetEngineStatus(engineKey);
92 deploymentClient.sendMessage(new GetEngineStatus(engineKey));
94 Response response = new Response(engineKey, true, getEngineStatus);
95 List<Message> messageList = new ArrayList<>();
96 messageList.add(response);
98 MessageBlock<Message> responseBlock = new MessageBlock<>(messageList, null);
99 messageListener.getValue().onMessage(responseBlock);
102 messageListener.getValue().onMessage("StringMessage");
103 fail("test should throw an exception here");
104 } catch (UnsupportedOperationException use) {
105 assertEquals("String mesages are not supported on the EngDep protocol", use.getMessage());
108 await().atMost(300, TimeUnit.MILLISECONDS).until(() -> deploymentClient.getMessagesReceived() == 2);
109 assertEquals(2, deploymentClient.getMessagesReceived());
111 deploymentClient.stopClient();
115 public void testDeploymentClientStartException() throws Exception {
116 DeploymentClient deploymentClient = new DeploymentClient("localhost", 51273);
118 final Field factoryField = deploymentClient.getClass().getDeclaredField("factory");
119 factoryField.setAccessible(true);
120 factoryField.set(deploymentClient, mockServiceFactory);
122 Mockito.doReturn(mockService).when(mockServiceFactory).createClient(anyObject());
124 Mockito.doNothing().when(mockService).addMessageListener(anyObject());
125 Mockito.doThrow(new ApexRuntimeException("connection start failed")).when(mockService).startConnection();
127 Thread clientThread = new Thread(deploymentClient);
128 clientThread.start();
130 await().atLeast(50, TimeUnit.MILLISECONDS).until(() -> !deploymentClient.isStarted());
132 assertFalse(deploymentClient.isStarted());
133 assertFalse(clientThread.isAlive());
134 assertEquals(0, deploymentClient.getReceiveQueue().size());
136 deploymentClient.stopClient();