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;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.ArgumentCaptor;
39 import org.mockito.Mock;
40 import org.mockito.Mockito;
41 import org.mockito.runners.MockitoJUnitRunner;
42 import org.onap.policy.apex.core.infrastructure.messaging.MessageHolder;
43 import org.onap.policy.apex.core.infrastructure.messaging.MessageListener;
44 import org.onap.policy.apex.core.infrastructure.messaging.MessagingService;
45 import org.onap.policy.apex.core.infrastructure.messaging.MessagingServiceFactory;
46 import org.onap.policy.apex.core.infrastructure.messaging.impl.ws.messageblock.MessageBlock;
47 import org.onap.policy.apex.core.protocols.Message;
48 import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineStatus;
49 import org.onap.policy.apex.core.protocols.engdep.messages.Response;
50 import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
51 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
54 * Test the deployment web socket client.
56 @RunWith(MockitoJUnitRunner.class)
57 public class DeploymentClientTest {
59 private static MessagingServiceFactory<Message> mockServiceFactory;
62 private static MessagingService<Message> mockService;
64 @SuppressWarnings("rawtypes")
65 ArgumentCaptor<MessageListener> messageListener = ArgumentCaptor.forClass(MessageListener.class);
67 @SuppressWarnings("unchecked")
69 public void testDeploymentClientStart() throws Exception {
70 DeploymentClient deploymentClient = new DeploymentClient("localhost", 51332);
72 final Field factoryField = deploymentClient.getClass().getDeclaredField("factory");
73 factoryField.setAccessible(true);
74 factoryField.set(deploymentClient, mockServiceFactory);
76 Mockito.doReturn(mockService).when(mockServiceFactory).createClient(anyObject());
78 Mockito.doNothing().when(mockService).addMessageListener(messageListener.capture());
79 Mockito.doNothing().when(mockService).startConnection();
81 Mockito.doNothing().when(mockService).send((MessageHolder<Message>) anyObject());
83 Thread clientThread = new Thread(deploymentClient);
86 await().atMost(200, TimeUnit.MILLISECONDS).until(() -> deploymentClient.isStarted());
88 assertTrue(deploymentClient.isStarted());
89 assertTrue(clientThread.isAlive());
91 AxArtifactKey engineKey = new AxArtifactKey("MyEngine", "0.0.1");
92 GetEngineStatus getEngineStatus = new GetEngineStatus(engineKey);
93 deploymentClient.sendMessage(new GetEngineStatus(engineKey));
95 Response response = new Response(engineKey, true, getEngineStatus);
96 List<Message> messageList = new ArrayList<>();
97 messageList.add(response);
99 MessageBlock<Message> responseBlock = new MessageBlock<>(messageList, null);
100 messageListener.getValue().onMessage(responseBlock);
103 messageListener.getValue().onMessage("StringMessage");
104 fail("test should throw an exception here");
105 } catch (UnsupportedOperationException use) {
106 assertEquals("String mesages are not supported on the EngDep protocol", use.getMessage());
109 await().atMost(300, TimeUnit.MILLISECONDS).until(() -> deploymentClient.getMessagesReceived() == 2);
110 assertEquals(2, deploymentClient.getMessagesReceived());
112 deploymentClient.stopClient();
116 public void testDeploymentClientStartException() throws Exception {
117 DeploymentClient deploymentClient = new DeploymentClient("localhost", 51273);
119 final Field factoryField = deploymentClient.getClass().getDeclaredField("factory");
120 factoryField.setAccessible(true);
121 factoryField.set(deploymentClient, mockServiceFactory);
123 Mockito.doReturn(mockService).when(mockServiceFactory).createClient(anyObject());
125 Mockito.doNothing().when(mockService).addMessageListener(anyObject());
126 Mockito.doThrow(new ApexRuntimeException("connection start failed")).when(mockService).startConnection();
128 Thread clientThread = new Thread(deploymentClient);
129 clientThread.start();
131 await().atLeast(50, TimeUnit.MILLISECONDS).until(() -> !deploymentClient.isStarted());
133 assertFalse(deploymentClient.isStarted());
134 assertFalse(clientThread.isAlive());
135 assertEquals(0, deploymentClient.getReceiveQueue().size());
137 deploymentClient.stopClient();