X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=rulemgt%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fholmes%2Frulemgt%2Fdcae%2FConfigFileScanningTaskTest.java;h=a4ab15b42003efaeb7233b411791142c3a3b25aa;hb=4627750077003db93420c053faa66310b27af7b1;hp=053612b38fe1506538ae739dfcea213a3890d4c5;hpb=5cc6fc1a762c547f420ce281deac69f02f6c2b83;p=holmes%2Frule-management.git diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/ConfigFileScanningTaskTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/ConfigFileScanningTaskTest.java index 053612b..a4ab15b 100644 --- a/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/ConfigFileScanningTaskTest.java +++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/ConfigFileScanningTaskTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2021 ZTE Corporation. + * Copyright 2021-2022 ZTE Corporation. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,9 @@ package org.onap.holmes.rulemgt.dcae; import org.easymock.EasyMock; +import org.junit.Rule; import org.junit.Test; +import org.junit.contrib.java.lang.system.SystemOutRule; import org.junit.runner.RunWith; import org.onap.holmes.common.ConfigFileScanner; import org.onap.holmes.common.utils.FileUtils; @@ -26,27 +28,25 @@ 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.HashMap; -import java.util.Map; - -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsEqual.equalTo; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.core.IsNot.not; import static org.junit.Assert.assertThat; @RunWith(PowerMockRunner.class) @PrepareForTest({JerseyClient.class}) -@SuppressStaticInitializationFor({"org.onap.holmes.common.utils.JerseyClient"}) public class ConfigFileScanningTaskTest { + @Rule + public final SystemOutRule systemOut = new SystemOutRule().enableLog(); + @Test - public void run_add_rules() throws Exception { - String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b"; + public void run_failed_to_get_existing_rules() throws Exception { + System.setProperty("ENABLE_ENCRYPT", "true"); + String indexPath = getFilePath("index-add.json"); - String contents = FileUtils.readTextFile(indexPath); ConfigFileScanningTask cfst = new ConfigFileScanningTask(null); Whitebox.setInternalState(cfst, "configFile", indexPath); @@ -54,77 +54,73 @@ public class ConfigFileScanningTaskTest { // mock for getExistingRules JerseyClient jcMock = PowerMock.createMock(JerseyClient.class); PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes(); - RuleQueryListResponse rqlr = new RuleQueryListResponse(); - rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents)); - EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr); - - // mock for deployRule - EasyMock.expect(jcMock.header(EasyMock.anyString(), EasyMock.anyObject())).andReturn(jcMock); - EasyMock.expect(jcMock.put(EasyMock.anyString(), EasyMock.anyObject())).andReturn(""); + EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andThrow(new RuntimeException()); PowerMock.replayAll(); cfst.run(); PowerMock.verifyAll(); - Map config = Whitebox.getInternalState(cfst, "configInEffect"); - assertThat(config.size(), is(1)); + assertThat(systemOut.getLog(), containsString("Failed to get existing rules for comparison.")); } @Test - public void run_remove_rules_normal() throws Exception { + public void run_add_rules() throws Exception { + System.setProperty("ENABLE_ENCRYPT", "true"); + String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b"; String indexPath = getFilePath("index-add.json"); String contents = FileUtils.readTextFile(indexPath); - Map configInEffect = new HashMap<>(); - configInEffect.put(clName, contents); - ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner()); - Whitebox.setInternalState(cfst, "configFile", getFilePath("index-empty.json")); - Whitebox.setInternalState(cfst, "configInEffect", configInEffect); + ConfigFileScanningTask cfst = new ConfigFileScanningTask(null); + Whitebox.setInternalState(cfst, "configFile", indexPath); // mock for getExistingRules JerseyClient jcMock = PowerMock.createMock(JerseyClient.class); PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes(); RuleQueryListResponse rqlr = new RuleQueryListResponse(); - rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents)); EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr); - // mock for deleteRule - EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn(""); + // mock for deployRule + EasyMock.expect(jcMock.header(EasyMock.anyString(), EasyMock.anyObject())).andReturn(jcMock); + EasyMock.expect(jcMock.put(EasyMock.anyString(), EasyMock.anyObject())).andReturn(""); PowerMock.replayAll(); cfst.run(); PowerMock.verifyAll(); - Map config = Whitebox.getInternalState(cfst, "configInEffect"); - assertThat(config.size(), is(0)); + assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been deployed.")); + + System.clearProperty("ENABLE_ENCRYPT"); } @Test - public void run_remove_rules_delete_null_pointer() throws Exception { + public void run_remove_rules_normal() throws Exception { + System.setProperty("ENABLE_ENCRYPT", "false"); + String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b"; String indexPath = getFilePath("index-add.json"); String contents = FileUtils.readTextFile(indexPath); - Map configInEffect = new HashMap<>(); - configInEffect.put(clName, contents); ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner()); - Whitebox.setInternalState(cfst, "configFile", indexPath); - Whitebox.setInternalState(cfst, "configInEffect", configInEffect); + Whitebox.setInternalState(cfst, "configFile", getFilePath("index-empty.json")); // mock for getExistingRules JerseyClient jcMock = PowerMock.createMock(JerseyClient.class); PowerMock.expectNew(JerseyClient.class).andReturn(jcMock).anyTimes(); RuleQueryListResponse rqlr = new RuleQueryListResponse(); - rqlr.getCorrelationRules().add(getRuleResult4API("a-non-existing-rule", contents)); + rqlr.getCorrelationRules().add(getRuleResult4API(clName, contents)); EasyMock.expect(jcMock.get(EasyMock.anyString(), EasyMock.anyObject())).andReturn(rqlr); + // mock for deleteRule + EasyMock.expect(jcMock.delete(EasyMock.anyString())).andReturn(""); + PowerMock.replayAll(); cfst.run(); PowerMock.verifyAll(); - Map config = Whitebox.getInternalState(cfst, "configInEffect"); - assertThat(config.size(), is(1)); + assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been removed.")); + + System.clearProperty("ENABLE_ENCRYPT"); } @Test @@ -132,12 +128,9 @@ public class ConfigFileScanningTaskTest { String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b"; String indexPath = getFilePath("index-add.json"); String contents = FileUtils.readTextFile(indexPath); - Map configInEffect = new HashMap<>(); - configInEffect.put(clName, contents); ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner()); Whitebox.setInternalState(cfst, "configFile", indexPath); - Whitebox.setInternalState(cfst, "configInEffect", configInEffect); // mock for getExistingRules JerseyClient jcMock = PowerMock.createMock(JerseyClient.class); @@ -153,8 +146,7 @@ public class ConfigFileScanningTaskTest { cfst.run(); PowerMock.verifyAll(); - Map config = Whitebox.getInternalState(cfst, "configInEffect"); - assertThat(config.size(), is(1)); + assertThat(systemOut.getLog(), containsString("Failed to delete rule, the rule id is: ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b")); } @Test @@ -162,12 +154,9 @@ public class ConfigFileScanningTaskTest { String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b"; String oldDrlPath = getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl"); String oldDrlContents = FileUtils.readTextFile(oldDrlPath); - Map configInEffect = new HashMap<>(); - configInEffect.put(clName, oldDrlContents); ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner()); Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-changed.json")); - Whitebox.setInternalState(cfst, "configInEffect", configInEffect); // mock for getExistingRules JerseyClient jcMock = PowerMock.createMock(JerseyClient.class); @@ -187,11 +176,7 @@ public class ConfigFileScanningTaskTest { cfst.run(); PowerMock.verifyAll(); - Map config = Whitebox.getInternalState(cfst, "configInEffect"); - assertThat(config.size(), is(1)); - assertThat(config.get(clName), - equalTo(FileUtils.readTextFile( - getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b-changed.drl")))); + assertThat(systemOut.getLog(), containsString("Rule 'ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b' has been updated.")); } @Test @@ -199,12 +184,9 @@ public class ConfigFileScanningTaskTest { String clName = "ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b"; String oldDrlPath = getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl"); String oldDrlContents = FileUtils.readTextFile(oldDrlPath); - Map configInEffect = new HashMap<>(); - configInEffect.put(clName, oldDrlContents); ConfigFileScanningTask cfst = new ConfigFileScanningTask(new ConfigFileScanner()); Whitebox.setInternalState(cfst, "configFile", getFilePath("index-rule-spaces-test.json")); - Whitebox.setInternalState(cfst, "configInEffect", configInEffect); // mock for getExistingRules JerseyClient jcMock = PowerMock.createMock(JerseyClient.class); @@ -217,11 +199,7 @@ public class ConfigFileScanningTaskTest { cfst.run(); PowerMock.verifyAll(); - Map config = Whitebox.getInternalState(cfst, "configInEffect"); - assertThat(config.size(), is(1)); - assertThat(config.get(clName), - equalTo(FileUtils.readTextFile( - getFilePath("ControlLoop-VOLTE-2179b738-fd36-4843-a71a-a8c24c70c55b.drl")))); + assertThat(systemOut.getLog(), not(containsString("has been updated."))); } private String getFilePath(String fileName) { @@ -236,6 +214,7 @@ public class ConfigFileScanningTaskTest { ruleResult4API.setContent(contents); ruleResult4API.setDescription(""); ruleResult4API.setEnabled(1); + ruleResult4API.setCreator("__SYSTEM__DEFAULT__"); return ruleResult4API; } } \ No newline at end of file