From 9ebb9c1aa320e49c581ba1a66202cc2f2bda0c5a Mon Sep 17 00:00:00 2001 From: "Determe, Sebastien (sd378r)" Date: Wed, 14 Feb 2018 15:30:07 +0100 Subject: Rework the SDC cache Rework the SDC cache that was broken and add unit tests to validate it Issue-ID: CLAMP-85 Change-Id: I43503702733d8f8f0ddbf391f94fb4e5416be98f Signed-off-by: Determe, Sebastien (sd378r) --- .../java/org/onap/clamp/clds/it/CldsDaoItCase.java | 37 ++++- .../org/onap/clamp/clds/it/CldsServiceItCase.java | 48 +++++- .../clamp/clds/model/CldsDBServiceCacheTest.java | 67 +++++++++ .../expected-result/sdc-properties-4cc5b45a.json | 161 +++++++++++++++++++++ 4 files changed, 308 insertions(+), 5 deletions(-) create mode 100644 src/test/java/org/onap/clamp/clds/model/CldsDBServiceCacheTest.java create mode 100644 src/test/resources/example/sdc/expected-result/sdc-properties-4cc5b45a.json (limited to 'src/test') diff --git a/src/test/java/org/onap/clamp/clds/it/CldsDaoItCase.java b/src/test/java/org/onap/clamp/clds/it/CldsDaoItCase.java index c684d445..1e9a9ed5 100644 --- a/src/test/java/org/onap/clamp/clds/it/CldsDaoItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/CldsDaoItCase.java @@ -25,30 +25,35 @@ package org.onap.clamp.clds.it; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import com.att.aft.dme2.internal.apache.commons.lang.RandomStringUtils; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.ArrayList; import java.util.List; import javax.ws.rs.NotFoundException; +import org.apache.commons.codec.DecoderException; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.onap.clamp.clds.AbstractItCase; +import org.onap.clamp.clds.client.req.sdc.SdcCatalogServices; import org.onap.clamp.clds.dao.CldsDao; import org.onap.clamp.clds.model.CLDSMonitoringDetails; +import org.onap.clamp.clds.model.CldsDBServiceCache; import org.onap.clamp.clds.model.CldsEvent; import org.onap.clamp.clds.model.CldsModel; +import org.onap.clamp.clds.model.CldsServiceData; import org.onap.clamp.clds.model.CldsTemplate; import org.onap.clamp.clds.util.ResourceFileUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; /** @@ -57,7 +62,6 @@ import org.springframework.test.context.junit4.SpringRunner; */ @RunWith(SpringRunner.class) @SpringBootTest -@TestPropertySource(locations = "classpath:application-no-camunda.properties") public class CldsDaoItCase extends AbstractItCase { protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsDao.class); @@ -66,6 +70,8 @@ public class CldsDaoItCase extends AbstractItCase { private String bpmnText; private String imageText; private String bpmnPropText; + @Autowired + private SdcCatalogServices sdcCatalogServices; /** * Setup the variable before the tests execution. @@ -161,4 +167,31 @@ public class CldsDaoItCase extends AbstractItCase { assertNotNull(clName.getCloseloopName()); }); } + + @Test + public void testCldsServiceCache() throws GeneralSecurityException, DecoderException, IOException { + CldsServiceData cldsServiceData = sdcCatalogServices + .getCldsServiceDataWithAlarmConditions("4cc5b45a-1f63-4194-8100-cd8e14248c92"); + // Test not in cache so should be null + CldsServiceData cldsServiceDataCache = cldsDao.getCldsServiceCache("4cc5b45a-1f63-4194-8100-cd8e14248c92"); + assertNull(cldsServiceDataCache); + cldsDao.setCldsServiceCache(new CldsDBServiceCache(cldsServiceData)); + cldsServiceDataCache = cldsDao.getCldsServiceCache("4cc5b45a-1f63-4194-8100-cd8e14248c92"); + assertNotNull(cldsServiceDataCache); + assertEquals("56441b4b-0467-41dc-9a0e-e68613838219", cldsServiceDataCache.getServiceUUID()); + assertEquals("4cc5b45a-1f63-4194-8100-cd8e14248c92", cldsServiceDataCache.getServiceInvariantUUID()); + assertEquals(2, cldsServiceDataCache.getCldsVfs().size()); + assertNotNull(cldsServiceDataCache.getAgeOfRecord()); + assertEquals(4, cldsServiceDataCache.getCldsVfs().get(0).getCldsVfcs().size()); + assertEquals("07e266fc-49ab-4cd7-8378-ca4676f1b9ec", + cldsServiceDataCache.getCldsVfs().get(0).getVfInvariantResourceUUID()); + assertEquals(0, cldsServiceDataCache.getCldsVfs().get(0).getCldsKPIList().size()); + // Second update + cldsServiceData.setCldsVfs(null); + cldsDao.setCldsServiceCache(new CldsDBServiceCache(cldsServiceData)); + cldsServiceDataCache = cldsDao.getCldsServiceCache("4cc5b45a-1f63-4194-8100-cd8e14248c92"); + assertNotNull(cldsServiceDataCache); + assertNull(cldsServiceDataCache.getCldsVfs()); + cldsDao.clearServiceCache(); + } } diff --git a/src/test/java/org/onap/clamp/clds/it/CldsServiceItCase.java b/src/test/java/org/onap/clamp/clds/it/CldsServiceItCase.java index 9dac2dc1..932434de 100644 --- a/src/test/java/org/onap/clamp/clds/it/CldsServiceItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/CldsServiceItCase.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP CLAMP * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights * reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -26,17 +26,21 @@ package org.onap.clamp.clds.it; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import com.att.aft.dme2.internal.apache.commons.lang.RandomStringUtils; import java.io.IOException; import java.io.InputStream; +import java.security.GeneralSecurityException; import java.security.Principal; import java.util.Properties; import javax.ws.rs.core.SecurityContext; +import org.apache.commons.codec.DecoderException; +import org.json.JSONException; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -46,13 +50,14 @@ import org.onap.clamp.clds.dao.CldsDao; import org.onap.clamp.clds.model.CldsHealthCheck; import org.onap.clamp.clds.model.CldsInfo; import org.onap.clamp.clds.model.CldsModel; +import org.onap.clamp.clds.model.CldsServiceData; import org.onap.clamp.clds.model.CldsTemplate; import org.onap.clamp.clds.service.CldsService; import org.onap.clamp.clds.util.ResourceFileUtil; +import org.skyscreamer.jsonassert.JSONAssert; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** @@ -60,7 +65,6 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; */ @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) -@TestPropertySource(locations = "classpath:application-no-camunda.properties") public class CldsServiceItCase extends AbstractItCase { @Autowired @@ -170,4 +174,42 @@ public class CldsServiceItCase extends AbstractItCase { // Verify whether it has been added properly or not assertNotNull(cldsDao.getModel(randomNameModel)); } + + @Test + public void testGetSdcServices() throws GeneralSecurityException, DecoderException, JSONException, IOException { + String result = cldsService.getSdcServices(); + JSONAssert.assertEquals( + ResourceFileUtil.getResourceAsString("example/sdc/expected-result/all-sdc-services.json"), result, + true); + } + + @Test + public void testGetSdcPropertiesByServiceUUIDForRefresh() + throws GeneralSecurityException, DecoderException, JSONException, IOException { + SecurityContext securityContext = Mockito.mock(SecurityContext.class); + Principal principal = Mockito.mock(Principal.class); + Mockito.when(principal.getName()).thenReturn("admin"); + Mockito.when(securityContext.getUserPrincipal()).thenReturn(principal); + Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|read")).thenReturn(true); + Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|update")).thenReturn(true); + Mockito.when(securityContext.isUserInRole("permission-type-template|dev|read")).thenReturn(true); + Mockito.when(securityContext.isUserInRole("permission-type-template|dev|update")).thenReturn(true); + Mockito.when(securityContext.isUserInRole("permission-type-filter-vf|dev|*")).thenReturn(true); + cldsService.setSecurityContext(securityContext); + // Test basic functionalities + String result = cldsService.getSdcPropertiesByServiceUUIDForRefresh("4cc5b45a-1f63-4194-8100-cd8e14248c92", + false); + JSONAssert.assertEquals( + ResourceFileUtil.getResourceAsString("example/sdc/expected-result/sdc-properties-4cc5b45a.json"), + result, true); + // Now test the Cache effect + CldsServiceData cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46"); + // Should not be there, so should be null + assertNull(cldsServiceDataCache); + cldsService.getSdcPropertiesByServiceUUIDForRefresh("c95b0e7c-c1f0-4287-9928-7964c5377a46", true); + // Should be there now, so should NOT be null + cldsServiceDataCache = cldsDao.getCldsServiceCache("c95b0e7c-c1f0-4287-9928-7964c5377a46"); + assertNotNull(cldsServiceDataCache); + cldsDao.clearServiceCache(); + } } diff --git a/src/test/java/org/onap/clamp/clds/model/CldsDBServiceCacheTest.java b/src/test/java/org/onap/clamp/clds/model/CldsDBServiceCacheTest.java new file mode 100644 index 00000000..ad168915 --- /dev/null +++ b/src/test/java/org/onap/clamp/clds/model/CldsDBServiceCacheTest.java @@ -0,0 +1,67 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + * =================================================================== + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ + +package org.onap.clamp.clds.model; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; +import java.io.ObjectInputStream; +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; + +public class CldsDBServiceCacheTest { + + @Test + public void testConstructor() throws IOException, ClassNotFoundException { + CldsServiceData cldsServiceData = new CldsServiceData(); + cldsServiceData.setServiceUUID("testUUID"); + cldsServiceData.setAgeOfRecord(Long.valueOf(100)); + cldsServiceData.setServiceInvariantUUID("testInvariantUUID"); + List cldsVfs = new ArrayList<>(); + CldsVfData cldsVfData = new CldsVfData(); + cldsVfData.setVfName("vf"); + List cldsKPIList = new ArrayList<>(); + CldsVfKPIData cldsVfKPIData = new CldsVfKPIData(); + cldsVfKPIData.setFieldPath("fieldPath"); + cldsVfKPIData.setFieldPathValue("fieldValue"); + cldsKPIList.add(cldsVfKPIData); + cldsVfData.setCldsKPIList(cldsKPIList); + cldsVfs.add(cldsVfData); + cldsServiceData.setCldsVfs(cldsVfs); + CldsDBServiceCache cldsDBServiceCache = new CldsDBServiceCache(cldsServiceData); + ObjectInputStream reader = new ObjectInputStream(cldsDBServiceCache.getCldsDataInstream()); + CldsServiceData cldsServiceDataResult = (CldsServiceData) reader.readObject(); + assertNotNull(cldsServiceDataResult); + assertNotNull(cldsServiceDataResult.getCldsVfs()); + assertEquals(cldsServiceDataResult.getCldsVfs().size(), 1); + assertNotNull(cldsServiceDataResult.getCldsVfs().get(0).getCldsKPIList()); + assertEquals(cldsServiceDataResult.getCldsVfs().get(0).getCldsKPIList().size(), 1); + assertEquals(cldsServiceDataResult.getServiceInvariantUUID(), "testInvariantUUID"); + assertEquals(cldsServiceDataResult.getServiceUUID(), "testUUID"); + assertEquals(cldsServiceDataResult.getAgeOfRecord(), Long.valueOf(100L)); + } +} diff --git a/src/test/resources/example/sdc/expected-result/sdc-properties-4cc5b45a.json b/src/test/resources/example/sdc/expected-result/sdc-properties-4cc5b45a.json new file mode 100644 index 00000000..8c5f39d8 --- /dev/null +++ b/src/test/resources/example/sdc/expected-result/sdc-properties-4cc5b45a.json @@ -0,0 +1,161 @@ +{ + "tca": { + "tname": "New_Set", + "tcaInt": "1", + "tcaVio": "1", + "eventName": { + "vCPEvGMUXPacketLoss": "vCPEvGMUXPacketLoss", + "vLoadBalancer": "vLoadBalancer", + "vFirewallBroadcastPackets": "vFirewallBroadcastPackets" + }, + "fieldPathM": { + "$.event.measurementsForVfScalingFields.vNicPerformanceArray[*].receivedBroadcastPacketsAccumulated": "receivedBroadcastPacketsAccumulated", + "$.event.measurementsForVfScalingFields.vNicPerformanceArray[*].receivedDiscardedPacketsDelta": "receivedDiscardedPacketsDelta" + }, + "operator": { + ">": "GREATER", + ">=": "GREATER_OR_EQUAL", + "=": "EQUAL", + "<=": "LESS_OR_EQUAL", + "<": "LESS" + }, + "opsPolicy": { + "POLICY_test_X": "POLICY_test_X", + "POLICY_test_Y": "POLICY_test_Y" + }, + "controlLoopSchemaType": { + "": "", + "VM": "VM", + "VNF": "VNF" + }, + "closedLoopEventStatus": { + "": "", + "ONSET": "ONSET", + "ABATED": "ABATED" + } + }, + "global": { + "actionSet": { + "vnfRecipe": "VNF", + "enbRecipe": "eNodeB" + }, + "location": { + "DC1": "Data Center 1", + "DC2": "Data Center 2", + "DC3": "Data Center 3" + } + }, + "policy": { + "pname": "0", + "timeout": 345, + "vnfRecipe": { + "": "", + "restart": "Restart", + "rebuild": "Rebuild", + "migrate": "Migrate", + "healthCheck": "Health Check" + }, + "enbRecipe": { + "": "", + "reset": "Reset" + }, + "maxRetries": "3", + "retryTimeLimit": 180, + "resource": { + "vCTS": "vCTS", + "v3CDB": "v3CDB", + "vUDR": "vUDR", + "vCOM": "vCOM", + "vRAR": "vRAR", + "vLCS": "vLCS", + "vUDR-BE": "vUDR-BE", + "vDBE": "vDBE" + }, + "parentPolicyConditions": { + "Failure_Retries": "Failure: Max Retries Exceeded", + "Failure_Timeout": "Failure: Time Limit Exceeded", + "Failure_Guard": "Failure: Guard", + "Failure_Exception": "Failure: Exception", + "Failure": "Failure: Other", + "Success": "Success" + } + }, + "shared": { + "byService": { + "4cc5b45a-1f63-4194-8100-cd8e14248c92": { + "vf": { + "07e266fc-49ab-4cd7-8378-ca4676f1b9ec": "vFirewall 0", + "023a3f0d-1161-45ff-b4cf-8918a8ccf3ad": "vPacketGen 0" + } + } + }, + "byVf": { + "07e266fc-49ab-4cd7-8378-ca4676f1b9ec": { + "vfc": { + }, + "kpi": { + "": "" + } + }, + "023a3f0d-1161-45ff-b4cf-8918a8ccf3ad": { + "vfc": { + }, + "kpi": { + "": "" + } + } + }, + "byKpi": { + }, + "byVfc": { + "53ebeed7-84db-4638-b1f3-8ed44c75985b": { + "alarmCondition": { + }, + "alertDescription": { + } + }, + "1a12347c-6166-4d21-9861-b2c432722a23": { + "alarmCondition": { + }, + "alertDescription": { + } + }, + "74805001-19f5-4c2c-9928-03014161c32a": { + "alarmCondition": { + }, + "alertDescription": { + } + }, + "d66c0bce-d7e1-41ad-bdaf-468d442d0543": { + "alarmCondition": { + }, + "alertDescription": { + } + }, + "28142b9a-7925-4921-bc81-178c5bae4a9b": { + "alarmCondition": { + }, + "alertDescription": { + } + }, + "86769df9-139b-489f-949d-05efb7f0ed6a": { + "alarmCondition": { + }, + "alertDescription": { + } + } + }, + "byAlarmCondition": { + "": { + "eventSourceType": "", + "eventSeverity": "" + } + }, + "byAlertDescription": { + "": { + "eventSourceType": "", + "eventSeverity": "" + } + } + } +} -- cgit 1.2.3-korg