X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=rulemgt%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fholmes%2Frulemgt%2Fdcae%2FDcaeConfigurationPollingTest.java;h=b6e71495cbcc29334da8c60a74695831181283b4;hb=refs%2Fchanges%2F33%2F121633%2F2;hp=6b640b24546787a9f5bdd8067ad4d1a897d919d6;hpb=c2a7c78225a54445098d666d72c6e84c21ca70ca;p=holmes%2Frule-management.git diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java index 6b640b2..b6e7149 100644 --- a/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2017-2020 ZTE Corporation. + * Copyright 2017-2021 ZTE Corporation. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,49 +15,31 @@ */ package org.onap.holmes.rulemgt.dcae; -import org.apache.http.HttpResponse; -import org.apache.http.StatusLine; -import org.apache.http.client.methods.HttpDelete; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPut; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; -import org.junit.After; -import org.junit.Before; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.onap.holmes.common.dcae.DcaeConfigurationQuery; import org.onap.holmes.common.dcae.entity.DcaeConfigurations; import org.onap.holmes.common.dcae.entity.Rule; -import org.onap.holmes.common.utils.GsonUtil; -import org.onap.holmes.common.utils.HttpsUtils; +import org.onap.holmes.common.utils.JerseyClient; import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse; import org.onap.holmes.rulemgt.bean.response.RuleResult4API; +import org.powermock.api.easymock.PowerMock; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import static org.easymock.EasyMock.anyObject; -import static org.easymock.EasyMock.expect; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; +import static org.easymock.EasyMock.*; import static org.powermock.api.easymock.PowerMock.*; -@PrepareForTest({HttpsUtils.class, DcaeConfigurationQuery.class}) -@SuppressStaticInitializationFor("org.onap.holmes.common.utils.HttpsUtils") @RunWith(PowerMockRunner.class) +@SuppressStaticInitializationFor("org.onap.holmes.common.utils.JerseyClient") +@PrepareForTest({DcaeConfigurationPolling.class, DcaeConfigurationQuery.class}) public class DcaeConfigurationPollingTest { - @org.junit.Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test public void run() throws Exception { DcaeConfigurations dcaeConfigurations = new DcaeConfigurations(); @@ -69,7 +51,7 @@ public class DcaeConfigurationPollingTest { Whitebox.setInternalState(dcaeConfigurationPolling, "url", "http://127.0.0.1"); RuleQueryListResponse ruleQueryListResponse = new RuleQueryListResponse(); - List ruleResult4APIList = new ArrayList(){ + List ruleResult4APIList = new ArrayList() { { add(new RuleResult4API()); } @@ -77,31 +59,16 @@ public class DcaeConfigurationPollingTest { ruleQueryListResponse.setCorrelationRules(ruleResult4APIList); ruleQueryListResponse.setTotalCount(ruleResult4APIList.size()); - CloseableHttpClient clientMock = createMock(CloseableHttpClient.class); - HttpResponse httpResponseMock = createMock(HttpResponse.class); - expect(HttpsUtils.getConditionalHttpsClient(30000)).andReturn(clientMock); - expect(HttpsUtils.get(anyObject(HttpGet.class), anyObject(HashMap.class), anyObject(CloseableHttpClient.class))) - .andReturn(httpResponseMock); - expect(HttpsUtils.extractResponseEntity(httpResponseMock)).andReturn(GsonUtil.beanToJson(ruleQueryListResponse)); - clientMock.close(); - expectLastCall(); - - expect(HttpsUtils.getConditionalHttpsClient(30000)).andReturn(clientMock); - expect(HttpsUtils.delete(anyObject(HttpDelete.class), anyObject(HashMap.class), anyObject(CloseableHttpClient.class))) - .andReturn(httpResponseMock); - clientMock.close(); - expectLastCall(); - - expect(HttpsUtils.getConditionalHttpsClient(30000)).andReturn(clientMock); - expect(HttpsUtils.put(anyObject(HttpPut.class), anyObject(HashMap.class), anyObject(HashMap.class), - anyObject(StringEntity.class), anyObject(CloseableHttpClient.class))) - .andReturn(httpResponseMock); - clientMock.close(); - expectLastCall(); - - StatusLine sl = createMock(StatusLine.class); - expect(httpResponseMock.getStatusLine()).andReturn(sl); - expect(sl.getStatusCode()).andReturn(200); + JerseyClient mockedJerseyClient = PowerMock.createMock(JerseyClient.class); + PowerMock.expectNew(JerseyClient.class).andReturn(mockedJerseyClient); + expect(mockedJerseyClient.get(anyString(), anyObject())).andReturn(ruleQueryListResponse); + + PowerMock.expectNew(JerseyClient.class).andReturn(mockedJerseyClient); + expect(mockedJerseyClient.delete(anyString())).andReturn("true"); + + PowerMock.expectNew(JerseyClient.class).andReturn(mockedJerseyClient); + expect(mockedJerseyClient.header(anyString(), anyString())).andReturn(mockedJerseyClient); + expect(mockedJerseyClient.put(anyString(), anyObject())).andReturn("true"); replayAll(); @@ -116,38 +83,28 @@ public class DcaeConfigurationPollingTest { dcaeConfigurations.addDefaultRule(new Rule("test", "clName", "contents", 1)); mockStatic(DcaeConfigurationQuery.class); expect(DcaeConfigurationQuery.getDcaeConfigurations(anyObject(String.class))).andReturn(dcaeConfigurations).times(2); - DcaeConfigurationPolling dcaeConfigurationPolling = createPartialMock(DcaeConfigurationPolling.class, - "getAllCorrelationRules"); + DcaeConfigurationPolling dcaeConfigurationPolling = new DcaeConfigurationPolling("localhost"); + Whitebox.setInternalState(dcaeConfigurationPolling, "url", "http://127.0.0.1"); RuleQueryListResponse ruleQueryListResponse = new RuleQueryListResponse(); - List ruleResult4APIList = new ArrayList(){ + List ruleResult4APIList = new ArrayList() { { add(new RuleResult4API()); } }; ruleQueryListResponse.setCorrelationRules(ruleResult4APIList); ruleQueryListResponse.setTotalCount(ruleResult4APIList.size()); - expect(dcaeConfigurationPolling.getAllCorrelationRules()).andReturn(ruleQueryListResponse); - - CloseableHttpClient clientMock = createMock(CloseableHttpClient.class); - HttpResponse httpResponseMock = createMock(HttpResponse.class); - expect(HttpsUtils.getConditionalHttpsClient(30000)).andReturn(clientMock); - expect(HttpsUtils.delete(anyObject(HttpDelete.class), anyObject(HashMap.class), anyObject(CloseableHttpClient.class))) - .andReturn(httpResponseMock); - clientMock.close(); - expectLastCall(); - - expect(HttpsUtils.getConditionalHttpsClient(30000)).andReturn(clientMock); - expect(HttpsUtils.put(anyObject(HttpPut.class), anyObject(HashMap.class), anyObject(HashMap.class), - anyObject(StringEntity.class), anyObject(CloseableHttpClient.class))) - .andReturn(httpResponseMock); - clientMock.close(); - expectLastCall(); - - StatusLine sl = createMock(StatusLine.class); - expect(httpResponseMock.getStatusLine()).andReturn(sl); - expect(sl.getStatusCode()).andReturn(200); + JerseyClient mockedJerseyClient = PowerMock.createMock(JerseyClient.class); + PowerMock.expectNew(JerseyClient.class).andReturn(mockedJerseyClient); + expect(mockedJerseyClient.get(anyString(), anyObject())).andReturn(ruleQueryListResponse); + + PowerMock.expectNew(JerseyClient.class).andReturn(mockedJerseyClient); + expect(mockedJerseyClient.delete(anyString())).andReturn("true"); + + PowerMock.expectNew(JerseyClient.class).andReturn(mockedJerseyClient); + expect(mockedJerseyClient.header(anyString(), anyString())).andReturn(mockedJerseyClient); + expect(mockedJerseyClient.put(anyString(), anyObject())).andReturn("true"); replayAll(); @@ -156,35 +113,4 @@ public class DcaeConfigurationPollingTest { verifyAll(); } - - - - @Test - public void getAllCorrelationRules() throws Exception { - - CloseableHttpClient clientMock = createMock(CloseableHttpClient.class); - HttpResponse httpResponseMock = createMock(HttpResponse.class); - expect(HttpsUtils.getConditionalHttpsClient(30000)).andReturn(clientMock); - expect(HttpsUtils.get(anyObject(HttpGet.class), anyObject(HashMap.class), anyObject(CloseableHttpClient.class))) - .andReturn(httpResponseMock); - expect(HttpsUtils.extractResponseEntity(httpResponseMock)).andReturn("{\"correlationRules\": [], \"totalCount\": 0}"); - clientMock.close(); - expectLastCall(); - - replayAll(); - DcaeConfigurationPolling daceConfigurationPolling = new DcaeConfigurationPolling("holmes-rule-mgmt"); - RuleQueryListResponse response = daceConfigurationPolling.getAllCorrelationRules(); - assertThat(response.getTotalCount(), is(0)); - verifyAll(); - } - - @Before - public void setUp() { - mockStatic(HttpsUtils.class); - } - - @After - public void tearDown() { - resetAll(); - } } \ No newline at end of file