f51b2337ca8d773dce92b21ca8cbf5cf50f7ff30
[policy/apex-pdp.git] / core / core-deployment / src / test / java / org / onap / policy / apex / core / deployment / DeploymentClientTest.java
1 /*-
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
9  * 
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  * 
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.
17  * 
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.core.deployment;
23
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;
30
31 import java.lang.reflect.Field;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.concurrent.TimeUnit;
35
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;
52
53 /**
54  * Test the deployment web socket client.
55  */
56 @RunWith(MockitoJUnitRunner.class)
57 public class DeploymentClientTest {
58     @Mock
59     private static MessagingServiceFactory<Message> mockServiceFactory;
60
61     @Mock
62     private static MessagingService<Message> mockService;
63
64     @SuppressWarnings("rawtypes")
65     ArgumentCaptor<MessageListener> messageListener = ArgumentCaptor.forClass(MessageListener.class);
66
67     @SuppressWarnings("unchecked")
68     @Test
69     public void testDeploymentClientStart() throws Exception {
70         DeploymentClient deploymentClient = new DeploymentClient("localhost", 51332);
71
72         final Field factoryField = deploymentClient.getClass().getDeclaredField("factory");
73         factoryField.setAccessible(true);
74         factoryField.set(deploymentClient, mockServiceFactory);
75
76         Mockito.doReturn(mockService).when(mockServiceFactory).createClient(anyObject());
77
78         Mockito.doNothing().when(mockService).addMessageListener(messageListener.capture());
79         Mockito.doNothing().when(mockService).startConnection();
80         
81         Mockito.doNothing().when(mockService).send((MessageHolder<Message>) anyObject());
82         
83         Thread clientThread = new Thread(deploymentClient);
84         clientThread.start();
85
86         await().atMost(200, TimeUnit.MILLISECONDS).until(() -> deploymentClient.isStarted());
87
88         assertTrue(deploymentClient.isStarted());
89         assertTrue(clientThread.isAlive());
90         
91         AxArtifactKey engineKey = new AxArtifactKey("MyEngine", "0.0.1");
92         GetEngineStatus getEngineStatus = new GetEngineStatus(engineKey);
93         deploymentClient.sendMessage(new GetEngineStatus(engineKey));
94
95         Response response = new Response(engineKey, true, getEngineStatus);
96         List<Message> messageList = new ArrayList<>();
97         messageList.add(response);
98         
99         MessageBlock<Message> responseBlock = new MessageBlock<>(messageList, null);
100         messageListener.getValue().onMessage(responseBlock);
101         
102         try {
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());
107         }
108
109         await().atMost(300, TimeUnit.MILLISECONDS).until(() -> deploymentClient.getMessagesReceived() == 2);
110         assertEquals(2, deploymentClient.getMessagesReceived());
111         
112         deploymentClient.stopClient();
113     }
114
115     @Test
116     public void testDeploymentClientStartException() throws Exception {
117         DeploymentClient deploymentClient = new DeploymentClient("localhost", 51273);
118
119         final Field factoryField = deploymentClient.getClass().getDeclaredField("factory");
120         factoryField.setAccessible(true);
121         factoryField.set(deploymentClient, mockServiceFactory);
122
123         Mockito.doReturn(mockService).when(mockServiceFactory).createClient(anyObject());
124
125         Mockito.doNothing().when(mockService).addMessageListener(anyObject());
126         Mockito.doThrow(new ApexRuntimeException("connection start failed")).when(mockService).startConnection();
127
128         Thread clientThread = new Thread(deploymentClient);
129         clientThread.start();
130
131         await().atLeast(50, TimeUnit.MILLISECONDS).until(() -> !deploymentClient.isStarted());
132
133         assertFalse(deploymentClient.isStarted());
134         assertFalse(clientThread.isAlive());
135         assertEquals(0, deploymentClient.getReceiveQueue().size());
136
137         deploymentClient.stopClient();
138     }
139 }