Add or Delete a PNF to an Active Service
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / java / org / onap / so / bpmn / infrastructure / pnf / dmaap / PnfEventReadyDmaapClientTest.java
index 9f31e2a..bbb6aad 100644 (file)
 package org.onap.so.bpmn.infrastructure.pnf.dmaap;
 
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
 import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyObject;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verifyZeroInteractions;
 import static org.mockito.Mockito.when;
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
-import java.io.UnsupportedEncodingException;
 import java.lang.reflect.Field;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -43,18 +42,15 @@ import org.apache.http.HttpResponse;
 import org.apache.http.ProtocolVersion;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpGet;
-import org.apache.http.entity.StringEntity;
+import org.apache.http.entity.InputStreamEntity;
 import org.apache.http.message.BasicHttpResponse;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.ArgumentCaptor;
-import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
-import org.onap.so.bpmn.infrastructure.pnf.PnfNotificationEvent;
 import org.onap.so.bpmn.infrastructure.pnf.dmaap.PnfEventReadyDmaapClient.DmaapTopicListenerThread;
-import org.springframework.context.ApplicationEventPublisher;
 import org.springframework.core.env.Environment;
 
 @RunWith(MockitoJUnitRunner.class)
@@ -71,9 +67,11 @@ public class PnfEventReadyDmaapClientTest {
     private static final int PORT = 1234;
     private static final String PROTOCOL = "http";
     private static final String URI_PATH_PREFIX = "eventsForTesting";
-    private static final String EVENT_TOPIC_TEST = "eventTopicTest";
-    private static final String CONSUMER_ID = "consumerTestId";
-    private static final String CONSUMER_GROUP = "consumerGroupTest";
+    private static final String TOPIC_NAME = "unauthenticated.PNF_READY";
+    private static final String TOPIC_NAME_UPDATE = "unauthenticated.PNF_UPDATE";
+    private static final String CONSUMER_ID = "so-bpmn-infra-pnfready";
+    private static final String CONSUMER_ID_UPDATE = "so-bpmn-infra-pnfupdate";
+    private static final String CONSUMER_GROUP = "so-consumer";
     private static final int TOPIC_LISTENER_DELAY_IN_SECONDS = 5;
 
     @Mock
@@ -85,21 +83,20 @@ public class PnfEventReadyDmaapClientTest {
     private Runnable threadMockToNotifyCamundaFlow;
     private ScheduledThreadPoolExecutor executorMock;
 
-    @Mock
-    private ApplicationEventPublisher applicationEventPublisher;
-
     @Before
     public void init() throws NoSuchFieldException, IllegalAccessException {
         when(env.getProperty(eq("pnf.dmaap.port"), eq(Integer.class))).thenReturn(PORT);
         when(env.getProperty(eq("pnf.dmaap.host"))).thenReturn(HOST);
         when(env.getProperty(eq("pnf.dmaap.protocol"))).thenReturn(PROTOCOL);
         when(env.getProperty(eq("pnf.dmaap.uriPathPrefix"))).thenReturn(URI_PATH_PREFIX);
-        when(env.getProperty(eq("pnf.dmaap.topicName"))).thenReturn(EVENT_TOPIC_TEST);
+        when(env.getProperty(eq("pnf.dmaap.pnfReadyTopicName"))).thenReturn(TOPIC_NAME);
+        when(env.getProperty(eq("pnf.dmaap.pnfUpdateTopicName"))).thenReturn(TOPIC_NAME_UPDATE);
         when(env.getProperty(eq("pnf.dmaap.consumerId"))).thenReturn(CONSUMER_ID);
+        when(env.getProperty(eq("pnf.dmaap.consumerIdUpdate"))).thenReturn(CONSUMER_ID_UPDATE);
         when(env.getProperty(eq("pnf.dmaap.consumerGroup"))).thenReturn(CONSUMER_GROUP);
         when(env.getProperty(eq("pnf.dmaap.topicListenerDelayInSeconds"), eq(Integer.class)))
                 .thenReturn(TOPIC_LISTENER_DELAY_IN_SECONDS);
-        testedObject = new PnfEventReadyDmaapClient(env, applicationEventPublisher);
+        testedObject = new PnfEventReadyDmaapClient(env);
         testedObjectInnerClassThread = testedObject.new DmaapTopicListenerThread();
         httpClientMock = mock(HttpClient.class);
         threadMockToNotifyCamundaFlow = mock(Runnable.class);
@@ -118,26 +115,45 @@ public class PnfEventReadyDmaapClientTest {
      * and shutdown the executor because of empty map
      */
     @Test
-    public void pnfCorrelationIdIsFoundInHttpResponse_notifyAboutPnfReady() throws IOException {
+    public void pnfCorrelationIdIsFoundInHttpResponse_notifyAboutPnfUpdate() throws IOException {
         when(httpClientMock.execute(any(HttpGet.class)))
                 .thenReturn(createResponse(String.format(JSON_EXAMPLE_WITH_PNF_CORRELATION_ID, PNF_CORRELATION_ID)));
         testedObjectInnerClassThread.run();
         ArgumentCaptor<HttpGet> captor1 = ArgumentCaptor.forClass(HttpGet.class);
         verify(httpClientMock).execute(captor1.capture());
+        assertEquals(captor1.getValue().getURI().getHost(), HOST);
+        assertEquals(captor1.getValue().getURI().getPort(), PORT);
+        assertEquals(captor1.getValue().getURI().getScheme(), PROTOCOL);
+        assertEquals(captor1.getValue().getURI().getPath(),
+                "/" + URI_PATH_PREFIX + "/" + TOPIC_NAME_UPDATE + "/" + CONSUMER_GROUP + "/" + CONSUMER_ID_UPDATE + "");
 
+        verify(threadMockToNotifyCamundaFlow).run();
+        verify(executorMock).shutdown();
+    }
+
+
+    @Test
+    public void pnfCorrelationIdIsFoundInHttpResponse_notifyAboutPnfReady() throws IOException {
+        ArgumentCaptor<HttpGet> captor1 = ArgumentCaptor.forClass(HttpGet.class);
+        when(httpClientMock.execute(any(HttpGet.class)))
+                .thenReturn(createResponse_forReady(
+                        String.format(JSON_EXAMPLE_WITH_PNF_CORRELATION_ID, PNF_CORRELATION_ID)))
+                .thenReturn(createResponse(String.format(JSON_EXAMPLE_WITH_PNF_CORRELATION_ID, PNF_CORRELATION_ID)));
+        testedObjectInnerClassThread.run();
+        verify(httpClientMock, times(2)).execute(captor1.capture());
         assertEquals(captor1.getValue().getURI().getHost(), HOST);
         assertEquals(captor1.getValue().getURI().getPort(), PORT);
         assertEquals(captor1.getValue().getURI().getScheme(), PROTOCOL);
+
         assertEquals(captor1.getValue().getURI().getPath(),
-                "/" + URI_PATH_PREFIX + "/" + EVENT_TOPIC_TEST + "/" + CONSUMER_GROUP + "/" + CONSUMER_ID + "");
+                "/" + URI_PATH_PREFIX + "/" + TOPIC_NAME + "/" + CONSUMER_GROUP + "/" + CONSUMER_ID + "");
+
 
-        /**
-         * Two PNF returned from HTTP request.
-         */
-        verify(applicationEventPublisher, times(2)).publishEvent(any(PnfNotificationEvent.class));
+        verify(threadMockToNotifyCamundaFlow).run();
         verify(executorMock).shutdown();
     }
 
+
     /**
      * Test run method, where the are following conditions:
      * <p>
@@ -193,8 +209,8 @@ public class PnfEventReadyDmaapClientTest {
         threadRunFlag.setAccessible(false);
     }
 
-    private HttpResponse createResponse(String json) throws UnsupportedEncodingException {
-        HttpEntity entity = new StringEntity(json);
+    private HttpResponse createResponse(String json) {
+        HttpEntity entity = new InputStreamEntity(new ByteArrayInputStream(json.getBytes()));
         ProtocolVersion protocolVersion = new ProtocolVersion("", 1, 1);
         HttpResponse response = new BasicHttpResponse(protocolVersion, 1, "");
         response.setEntity(entity);
@@ -202,4 +218,13 @@ public class PnfEventReadyDmaapClientTest {
         return response;
     }
 
+    private HttpResponse createResponse_forReady(String json) {
+        HttpEntity entity = new InputStreamEntity(new ByteArrayInputStream(json.getBytes()));
+        ProtocolVersion protocolVersion = new ProtocolVersion("", 1, 1);
+        HttpResponse response = new BasicHttpResponse(protocolVersion, 1, "");
+        response.setEntity(entity);
+        response.setStatusCode(500);
+        return response;
+    }
+
 }