Simplified HTTP(s) Related Tools
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / dmaap / PublisherTest.java
index be9f74f..8ad154d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2017 ZTE Corporation.
+ * Copyright 2017-2020 ZTE Corporation.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  */
 package org.onap.holmes.common.dmaap;
 
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
-import java.util.HashMap;
-import javax.ws.rs.client.Client;
-import javax.ws.rs.client.ClientBuilder;
-import javax.ws.rs.client.Entity;
-import javax.ws.rs.client.Invocation.Builder;
-import javax.ws.rs.client.WebTarget;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-import org.apache.http.HttpResponse;
-import org.apache.http.HttpStatus;
-import org.apache.http.StatusLine;
-import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.entity.StringEntity;
-import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.commons.lang3.StringUtils;
 import org.easymock.EasyMock;
-import org.junit.Rule;
 import org.junit.Test;
-import org.junit.rules.ExpectedException;
 import org.junit.runner.RunWith;
-import org.mockito.Matchers;
 import org.onap.holmes.common.dmaap.entity.PolicyMsg;
-import org.onap.holmes.common.exception.CorrelationException;
-import org.onap.holmes.common.utils.HttpsUtils;
+import org.onap.holmes.common.utils.JerseyClient;
 import org.powermock.api.easymock.PowerMock;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.core.classloader.annotations.PowerMockIgnore;
 import org.powermock.modules.junit4.PowerMockRunner;
+import org.powermock.reflect.internal.WhiteboxImpl;
 
-@PrepareForTest({HttpsUtils.class, HttpResponse.class})
-@RunWith(PowerMockRunner.class)
-public class PublisherTest {
-
-    @Rule
-    public ExpectedException thrown = ExpectedException.none();
-
-    private static final String URL = "http://localhost/dmaapTopic";
-
-    @Test
-    public void publish_exception() throws Exception {
-
-        Publisher publisher = new Publisher();
-        publisher.setUrl(URL);
+import javax.ws.rs.client.Entity;
 
-        thrown.expect(CorrelationException.class);
-        thrown.expectMessage("Failed to connect to DCAE");
+import static org.easymock.EasyMock.anyObject;
 
-        publisher.publish(new PolicyMsg());
-    }
+@RunWith(PowerMockRunner.class)
+@PowerMockIgnore({"javax.net.ssl.*", "javax.security.*"})
+public class PublisherTest {
 
     @Test
-    public void publish_normal() throws Exception {
+    public void publish_normal() {
 
         Publisher publisher = new Publisher();
-        publisher.setUrl(URL);
+        publisher.setUrl("http://localhost/dmaapTopic");
 
-        PowerMockito.mockStatic(HttpsUtils.class);
-        HttpResponse httpResponse = PowerMockito.mock(HttpResponse.class);
-        PowerMockito.when(HttpsUtils
-                .post(Matchers.any(HttpPost.class), Matchers.any(HashMap.class),
-                        Matchers.any(HashMap.class), Matchers.any(StringEntity.class),
-                        Matchers.any(CloseableHttpClient.class))).thenReturn(httpResponse);
-        StatusLine statusLine = PowerMockito.mock(StatusLine.class);
-        PowerMockito.when(httpResponse.getStatusLine()).thenReturn(statusLine);
-        PowerMockito.when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_OK);
+        JerseyClient mockedJerseyClient = PowerMock.createMock(JerseyClient.class);
+        WhiteboxImpl.setInternalState(publisher, "client", mockedJerseyClient);
+        EasyMock.expect(mockedJerseyClient.post(anyObject(String.class), anyObject(Entity.class)))
+                .andReturn(StringUtils.EMPTY);
 
         PowerMock.replayAll();
 
-        assertThat(publisher.publish(new PolicyMsg()), is(true));
+        publisher.publish(new PolicyMsg());
 
         PowerMock.verifyAll();
     }