diff options
author | da490c <dave.adams@amdocs.com> | 2018-06-12 23:11:15 -0400 |
---|---|---|
committer | da490c <dave.adams@amdocs.com> | 2018-06-12 23:54:25 -0400 |
commit | 72da66902a3efaff3068ed2b3f4f7eb3bb5fe43f (patch) | |
tree | 9cfacefcb3543589bff774f9f40cc7fd548e8430 /src/test/java | |
parent | 7753d7e90f5d91b143043a642e90dd30806da2ba (diff) |
Fix Spike Event Processing with Common Format
Issue-ID: AAI-1215
Change-Id: Ic6b575e9194d00cc0da9229cdb532869357e37e5
Signed-off-by: da490c <dave.adams@amdocs.com>
Diffstat (limited to 'src/test/java')
11 files changed, 459 insertions, 319 deletions
diff --git a/src/test/java/org/onap/aai/datarouter/policy/EntityEventPolicyStubbed.java b/src/test/java/org/onap/aai/datarouter/policy/EntityEventPolicyStubbed.java index 02f8ed7..9e10aee 100644 --- a/src/test/java/org/onap/aai/datarouter/policy/EntityEventPolicyStubbed.java +++ b/src/test/java/org/onap/aai/datarouter/policy/EntityEventPolicyStubbed.java @@ -27,30 +27,43 @@ import org.onap.aai.datarouter.policy.EntityEventPolicy; import org.onap.aai.datarouter.policy.EntityEventPolicyConfig; public class EntityEventPolicyStubbed extends EntityEventPolicy { - - - public EntityEventPolicyStubbed(EntityEventPolicyConfig config) throws FileNotFoundException { - super(config); - - } - - protected void handleSearchServiceOperation(DocumentStoreDataEntity eventEntity, String action, String index) { - //Stub out the actual call to Search Data service and instead store/update documents in memory - try { - switch (action.toLowerCase()) { - case "create": - InMemorySearchDatastore.put(eventEntity.getId(), eventEntity.getAsJson()); // they are executed if variable == c1 - break; - case "update": - InMemorySearchDatastore.put(eventEntity.getId(), eventEntity.getAsJson()); // they are executed if variable == c1 - break; - case "delete": - InMemorySearchDatastore.remove(eventEntity.getId()); // they are executed if variable == c1 - break; - default: - break; - } - } catch (Exception ex) { - } - } + + private InMemorySearchDatastore searchDb; + + public EntityEventPolicyStubbed(EntityEventPolicyConfig config) throws FileNotFoundException { + super(config); + + } + + public InMemorySearchDatastore getSearchDb() { + return searchDb; + } + + public EntityEventPolicyStubbed withSearchDb(InMemorySearchDatastore searchDb) { + this.searchDb = searchDb; + return this; + } + + protected void handleSearchServiceOperation(DocumentStoreDataEntity eventEntity, String action, + String index) { + // Stub out the actual call to Search Data service and instead store/update documents in memory + try { + switch (action.toLowerCase()) { + case "create": + searchDb.put(eventEntity.getId(), eventEntity.getAsJson()); // they are executed if + // variable == c1 + break; + case "update": + searchDb.put(eventEntity.getId(), eventEntity.getAsJson()); // they are executed if + // variable == c1 + break; + case "delete": + searchDb.remove(eventEntity.getId()); // they are executed if variable == c1 + break; + default: + break; + } + } catch (Exception ex) { + } + } } diff --git a/src/test/java/org/onap/aai/datarouter/policy/EntityEventPolicyTest.java b/src/test/java/org/onap/aai/datarouter/policy/EntityEventPolicyTest.java index 9d87b6e..adf8ce2 100644 --- a/src/test/java/org/onap/aai/datarouter/policy/EntityEventPolicyTest.java +++ b/src/test/java/org/onap/aai/datarouter/policy/EntityEventPolicyTest.java @@ -41,8 +41,9 @@ import org.powermock.api.mockito.PowerMockito; public class EntityEventPolicyTest { - EntityEventPolicy policy; - String eventJson; + private EntityEventPolicy policy; + private String eventJson; + private InMemorySearchDatastore searchDb; @SuppressWarnings("unchecked") @Before @@ -51,13 +52,9 @@ public class EntityEventPolicyTest { PowerMockito.when(config.getSearchKeystorePwd()).thenReturn("password"); PowerMockito.when(config.getSourceDomain()).thenReturn("JUNIT"); + searchDb = new InMemorySearchDatastore(); + policy = new EntityEventPolicyStubbed(config).withSearchDb(searchDb); - SearchServiceAgent searchServiceAgent = PowerMockito.mock(SearchServiceAgent.class); - - PowerMockito.whenNew(SearchServiceAgent.class).withAnyArguments().thenReturn(searchServiceAgent); - - - policy = new EntityEventPolicyStubbed(config); FileInputStream event = new FileInputStream( new File("src/test/resources/aai_event.json")); eventJson = IOUtils.toString(event, "UTF-8"); @@ -68,16 +65,16 @@ public class EntityEventPolicyTest { policy.process(getExchangeEvent("event1","create")); policy.process(getExchangeEvent("event2","create")); - assertNotNull(InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("event1"))); - assertNotNull(InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("event2"))); + assertNotNull(searchDb.get(NodeUtils.generateUniqueShaDigest("event1"))); + assertNotNull(searchDb.get(NodeUtils.generateUniqueShaDigest("event2"))); policy.process(getExchangeEvent("event1","update")); policy.process(getExchangeEvent("event2","update")); - assertNotNull(InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("event1"))); - assertNotNull(InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("event2"))); + assertNotNull(searchDb.get(NodeUtils.generateUniqueShaDigest("event1"))); + assertNotNull(searchDb.get(NodeUtils.generateUniqueShaDigest("event2"))); policy.process(getExchangeEvent("event2","delete")); - assertNull(InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("event2"))); + assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("event2"))); } private Exchange getExchangeEvent(String link,String action){ diff --git a/src/test/java/org/onap/aai/datarouter/policy/InMemorySearchDatastore.java b/src/test/java/org/onap/aai/datarouter/policy/InMemorySearchDatastore.java index 874cce8..bcca308 100644 --- a/src/test/java/org/onap/aai/datarouter/policy/InMemorySearchDatastore.java +++ b/src/test/java/org/onap/aai/datarouter/policy/InMemorySearchDatastore.java @@ -24,21 +24,21 @@ import java.util.concurrent.ConcurrentHashMap; public final class InMemorySearchDatastore { - private final static ConcurrentHashMap<String, String> documents = new ConcurrentHashMap<String, String>(); + private final ConcurrentHashMap<String, String> documents = new ConcurrentHashMap<String, String>(); - public static ConcurrentHashMap<String, String> getAll() { + public ConcurrentHashMap<String, String> getAll() { return documents; } - public static void put(String key, String value) { + public void put(String key, String value) { documents.put(key, value); } - public static String get(String key) { + public String get(String key) { return documents.get(key); } - public static void remove(String key) { + public void remove(String key) { documents.remove(key); } } diff --git a/src/test/java/org/onap/aai/datarouter/policy/SpikeAggregateGenericVnfProcessorStubbed.java b/src/test/java/org/onap/aai/datarouter/policy/SpikeAggregateGenericVnfProcessorStubbed.java index ffc264b..730a32b 100644 --- a/src/test/java/org/onap/aai/datarouter/policy/SpikeAggregateGenericVnfProcessorStubbed.java +++ b/src/test/java/org/onap/aai/datarouter/policy/SpikeAggregateGenericVnfProcessorStubbed.java @@ -25,30 +25,43 @@ import java.io.FileNotFoundException; import org.onap.aai.datarouter.entity.DocumentStoreDataEntity; public class SpikeAggregateGenericVnfProcessorStubbed extends SpikeAggregateGenericVnfProcessor { - - - public SpikeAggregateGenericVnfProcessorStubbed(SpikeEventPolicyConfig config) throws FileNotFoundException { - super(config); - - } - - protected void handleSearchServiceOperation(DocumentStoreDataEntity eventEntity, String action, String index) { - //Stub out the actual call to Search Data service and instead store/update documents in memory - try { - switch (action.toLowerCase()) { - case "create": - InMemorySearchDatastore.put(eventEntity.getId(), eventEntity.getAsJson()); // they are executed if variable == c1 - break; - case "update": - InMemorySearchDatastore.put(eventEntity.getId(), eventEntity.getAsJson()); // they are executed if variable == c1 - break; - case "delete": - InMemorySearchDatastore.remove(eventEntity.getId()); // they are executed if variable == c1 - break; - default: - break; - } - } catch (Exception ex) { - } - } + + private InMemorySearchDatastore searchDb; + + public SpikeAggregateGenericVnfProcessorStubbed(SpikeEventPolicyConfig config) + throws FileNotFoundException { + super(config); + } + + public SpikeAggregateGenericVnfProcessorStubbed withSearchDb(InMemorySearchDatastore searchDb) { + this.searchDb = searchDb; + return this; + } + + public InMemorySearchDatastore getSearchDb() { + return searchDb; + } + + protected void handleSearchServiceOperation(DocumentStoreDataEntity eventEntity, String action, + String index) { + // Stub out the actual call to Search Data service and instead store/update documents in memory + try { + switch (action.toLowerCase()) { + case "create": + searchDb.put(eventEntity.getId(), eventEntity.getAsJson()); // they are executed if + // variable == c1 + break; + case "update": + searchDb.put(eventEntity.getId(), eventEntity.getAsJson()); // they are executed if + // variable == c1 + break; + case "delete": + searchDb.remove(eventEntity.getId()); // they are executed if variable == c1 + break; + default: + break; + } + } catch (Exception ex) { + } + } } diff --git a/src/test/java/org/onap/aai/datarouter/policy/SpikeAggregateGenericVnfProcessorTest.java b/src/test/java/org/onap/aai/datarouter/policy/SpikeAggregateGenericVnfProcessorTest.java index d1c01af..03970bd 100644 --- a/src/test/java/org/onap/aai/datarouter/policy/SpikeAggregateGenericVnfProcessorTest.java +++ b/src/test/java/org/onap/aai/datarouter/policy/SpikeAggregateGenericVnfProcessorTest.java @@ -34,68 +34,80 @@ import org.apache.commons.io.IOUtils; import org.junit.Before; import org.junit.Test; import org.onap.aai.datarouter.util.NodeUtils; -import org.onap.aai.datarouter.util.SearchServiceAgent; import org.powermock.api.mockito.PowerMockito; - - public class SpikeAggregateGenericVnfProcessorTest { - SpikeAggregateGenericVnfProcessor policy; - String eventJson; - + private SpikeAggregateGenericVnfProcessor policy; + private InMemorySearchDatastore searchDb; - @SuppressWarnings("unchecked") @Before public void init() throws Exception { SpikeEventPolicyConfig config = PowerMockito.mock(SpikeEventPolicyConfig.class); PowerMockito.when(config.getSearchKeystorePwd()).thenReturn("password"); PowerMockito.when(config.getSourceDomain()).thenReturn("JUNIT"); + searchDb = new InMemorySearchDatastore(); + policy = new SpikeAggregateGenericVnfProcessorStubbed(config).withSearchDb(searchDb); + } - SearchServiceAgent searchServiceAgent = PowerMockito.mock(SearchServiceAgent.class); - PowerMockito.whenNew(SearchServiceAgent.class).withAnyArguments() - .thenReturn(searchServiceAgent); + @Test + public void testProcess_success() throws Exception { + String genericVnfEventJsonTemplate = IOUtils.toString( + new FileInputStream(new File("src/test/resources/generic-vnf-spike-event.json")), "UTF-8"); - policy = new SpikeAggregateGenericVnfProcessorStubbed(config); - FileInputStream event = new FileInputStream(new File("src/test/resources/spike_event.json")); - eventJson = IOUtils.toString(event, "UTF-8"); + policy.process( + getExchangeEvent(genericVnfEventJsonTemplate, "update-notification", "CREATE", "gvnf123")); + assertNotNull(searchDb.get(NodeUtils.generateUniqueShaDigest("generic-vnf/gvnf123"))); + + policy.process( + getExchangeEvent(genericVnfEventJsonTemplate, "update-notification", "DELETE", "gvnf123")); + + assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("generic-vnf/gvnf123"))); + + } + /* + * Failure test cases - no searchable attribute for type + */ @Test - public void testProcess_success() throws Exception { - policy.process(getExchangeEvent("12345", "create", "generic-vnf")); - policy.process(getExchangeEvent("23456", "create", "generic-vnf")); - - assertNotNull( - InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("generic-vnf/12345"))); - assertNotNull( - InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("generic-vnf/23456"))); - - - policy.process(getExchangeEvent("23456", "delete", "generic-vnf")); - assertNull(InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("23456"))); + public void testProcess_failure_unknownOxmEntityType() throws Exception { + + String pserverEventJsonTemplate = IOUtils.toString( + new FileInputStream(new File("src/test/resources/optical-router-spike-event.json")), + "UTF-8"); + + policy.process( + getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "optronic123")); + + assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("optical-router/optronic123"))); } + @Test - public void testProcess_fail() throws Exception { - policy.process(getExchangeEvent("666666", "create", "NotValid")); - assertNull( - InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("NotValid/666666"))); - - policy.process(getExchangeEvent("", "create", "generic-vnf")); - assertNull( - InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("generic-vnf/"))); + public void testProcess_failure_missingMandatoryFieldsFromBodyObject() throws Exception { + String pserverEventJsonTemplate = IOUtils.toString( + new FileInputStream( + new File("src/test/resources/pserver-missing-mandtory-field-spike-event.json")), + "UTF-8"); + + policy.process( + getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "pserver123")); + + assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("pserver/pserver123"))); } - - private Exchange getExchangeEvent(String key, String action, String type) { - Object obj = eventJson.replace("$KEY", key).replace("$ACTION", action).replace("$TYPE", type); + private Exchange getExchangeEvent(String payloadTemplate, String eventType, String operationType, + String entityKey) { + Object obj = payloadTemplate.replace("$EVENT_TYPE", eventType) + .replace("$OPERATION_TYPE", operationType).replace("$ENTITY_KEY", entityKey); + Exchange exchange = PowerMockito.mock(Exchange.class); Message inMessage = PowerMockito.mock(Message.class); Message outMessage = PowerMockito.mock(Message.class); - PowerMockito.when(exchange.getIn()).thenReturn(inMessage); + PowerMockito.when(exchange.getIn()).thenReturn(inMessage); PowerMockito.when(inMessage.getBody()).thenReturn(obj); PowerMockito.when(exchange.getOut()).thenReturn(outMessage); @@ -106,6 +118,4 @@ public class SpikeAggregateGenericVnfProcessorTest { } - - } diff --git a/src/test/java/org/onap/aai/datarouter/policy/SpikeAutosuggestProcessorStubbed.java b/src/test/java/org/onap/aai/datarouter/policy/SpikeAutosuggestProcessorStubbed.java index 202746c..81335e5 100644 --- a/src/test/java/org/onap/aai/datarouter/policy/SpikeAutosuggestProcessorStubbed.java +++ b/src/test/java/org/onap/aai/datarouter/policy/SpikeAutosuggestProcessorStubbed.java @@ -25,30 +25,43 @@ import java.io.FileNotFoundException; import org.onap.aai.datarouter.entity.DocumentStoreDataEntity; public class SpikeAutosuggestProcessorStubbed extends SpikeAutosuggestIndexProcessor { - - - public SpikeAutosuggestProcessorStubbed(SpikeEventPolicyConfig config) throws FileNotFoundException { - super(config); - - } - - protected void handleSearchServiceOperation(DocumentStoreDataEntity eventEntity, String action, String index) { - //Stub out the actual call to Search Data service and instead store/update documents in memory - try { - switch (action.toLowerCase()) { - case "create": - InMemorySearchDatastore.put(eventEntity.getId(), eventEntity.getAsJson()); // they are executed if variable == c1 - break; - case "update": - InMemorySearchDatastore.put(eventEntity.getId(), eventEntity.getAsJson()); // they are executed if variable == c1 - break; - case "delete": - InMemorySearchDatastore.remove(eventEntity.getId()); // they are executed if variable == c1 - break; - default: - break; - } - } catch (Exception ex) { - } - } + + private InMemorySearchDatastore searchDb; + + public SpikeAutosuggestProcessorStubbed(SpikeEventPolicyConfig config) + throws FileNotFoundException { + super(config); + } + + public InMemorySearchDatastore getSearchDb() { + return searchDb; + } + + public SpikeAutosuggestProcessorStubbed withSearchDb(InMemorySearchDatastore searchDb) { + this.searchDb = searchDb; + return this; + } + + protected void handleSearchServiceOperation(DocumentStoreDataEntity eventEntity, String action, + String index) { + // Stub out the actual call to Search Data service and instead store/update documents in memory + try { + switch (action.toLowerCase()) { + case "create": + searchDb.put(eventEntity.getId(), eventEntity.getAsJson()); // they are executed if + // variable == c1 + break; + case "update": + searchDb.put(eventEntity.getId(), eventEntity.getAsJson()); // they are executed if + // variable == c1 + break; + case "delete": + searchDb.remove(eventEntity.getId()); // they are executed if variable == c1 + break; + default: + break; + } + } catch (Exception ex) { + } + } } diff --git a/src/test/java/org/onap/aai/datarouter/policy/SpikeAutosuggestProcessorTest.java b/src/test/java/org/onap/aai/datarouter/policy/SpikeAutosuggestProcessorTest.java index 8ef5ab6..c49f17b 100644 --- a/src/test/java/org/onap/aai/datarouter/policy/SpikeAutosuggestProcessorTest.java +++ b/src/test/java/org/onap/aai/datarouter/policy/SpikeAutosuggestProcessorTest.java @@ -34,66 +34,104 @@ import org.apache.commons.io.IOUtils; import org.junit.Before; import org.junit.Test; import org.onap.aai.datarouter.util.NodeUtils; -import org.onap.aai.datarouter.util.SearchServiceAgent; import org.powermock.api.mockito.PowerMockito; - - public class SpikeAutosuggestProcessorTest { - SpikeAutosuggestIndexProcessor policy; - String eventJson; + private SpikeEventPolicyConfig eventPolicyConfig; + private SpikeAutosuggestIndexProcessor policy; + private InMemorySearchDatastore searchDb; - @SuppressWarnings("unchecked") @Before public void init() throws Exception { - SpikeEventPolicyConfig config = PowerMockito.mock(SpikeEventPolicyConfig.class); - PowerMockito.when(config.getSearchKeystorePwd()).thenReturn("password"); - PowerMockito.when(config.getSourceDomain()).thenReturn("JUNIT"); - - - SearchServiceAgent searchServiceAgent = PowerMockito.mock(SearchServiceAgent.class); - PowerMockito.whenNew(SearchServiceAgent.class).withAnyArguments() - .thenReturn(searchServiceAgent); - - - policy = new SpikeAutosuggestProcessorStubbed(config); - FileInputStream event = new FileInputStream(new File("src/test/resources/spike_event.json")); - eventJson = IOUtils.toString(event, "UTF-8"); + + eventPolicyConfig = new SpikeEventPolicyConfig(); + eventPolicyConfig.setSearchKeystorePwd("password"); + eventPolicyConfig.setSourceDomain("JUNIT"); + + searchDb = new InMemorySearchDatastore(); + policy = new SpikeAutosuggestProcessorStubbed(eventPolicyConfig).withSearchDb(searchDb); } @Test public void testProcess_success() throws Exception { - policy.process(getExchangeEvent("77777", "create", "generic-vnf")); + + String genericVnfEventJsonTemplate = IOUtils.toString( + new FileInputStream(new File("src/test/resources/generic-vnf-spike-event.json")), "UTF-8"); + + policy.process(getExchangeEvent(genericVnfEventJsonTemplate, "update-notification", "CREATE", "vserver123")); assertNotNull( - InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("junk and Running VNFs"))); + searchDb.get(NodeUtils.generateUniqueShaDigest("junk and Running VNFs"))); assertNotNull( - InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("junk VNFs"))); + searchDb.get(NodeUtils.generateUniqueShaDigest("junk VNFs"))); assertNotNull( - InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("Running VNFs"))); - + searchDb.get(NodeUtils.generateUniqueShaDigest("Running VNFs"))); } + + /* + * Failure test cases + * - no searchable attribute for type + */ + @Test - public void testProcess_fail() throws Exception { - policy.process(getExchangeEvent("666666", "create", "NotValid")); - assertNull( - InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("NotValid/666666"))); + public void testProcess_failure_unknownOxmEntityType() throws Exception { + + String pserverEventJsonTemplate = IOUtils.toString( + new FileInputStream(new File("src/test/resources/optical-router-spike-event.json")), "UTF-8"); - policy.process(getExchangeEvent("", "create", "generic-vnf")); + policy.process(getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "optronic123")); + assertNull( - InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("generic-vnf/"))); + searchDb.get(NodeUtils.generateUniqueShaDigest("optical-router/optronic123"))); + } + + @Test + public void testProcess_failure_missingMandatoryFieldsFromBodyObject() throws Exception { + + String pserverEventJsonTemplate = IOUtils.toString( + new FileInputStream(new File("src/test/resources/pserver-missing-mandtory-field-spike-event.json")), "UTF-8"); + + policy.process(getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "pserver123")); + assertNull( + searchDb.get(NodeUtils.generateUniqueShaDigest("pserver/pserver123"))); } + + @Test + public void testProcess_failure_missingMandatoryVertexProperties() throws Exception { + + String pserverEventJsonTemplate = IOUtils.toString( + new FileInputStream(new File("src/test/resources/pserver-missing-primary-key-spike-event.json")), "UTF-8"); + + policy.process(getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "pserver123")); + assertNull( + searchDb.get(NodeUtils.generateUniqueShaDigest("pserver/pserver123"))); + } - private Exchange getExchangeEvent(String key, String action, String type) { - Object obj = eventJson.replace("$KEY", key).replace("$ACTION", action).replace("$TYPE", type); + @Test + public void testProcess_failure_noSuggestibleAttributesForEntityType() throws Exception { + + String pserverEventJsonTemplate = IOUtils.toString( + new FileInputStream(new File("src/test/resources/vserver-spike-event.json")), "UTF-8"); + + policy.process(getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "vserver123")); + + assertNull( + searchDb.get(NodeUtils.generateUniqueShaDigest("vserver/vserver123"))); + } + + private Exchange getExchangeEvent(String payloadTemplate, String eventType, String operationType, + String entityKey) { + Object obj = payloadTemplate.replace("$EVENT_TYPE", eventType) + .replace("$OPERATION_TYPE", operationType).replace("$ENTITY_KEY", entityKey); + Exchange exchange = PowerMockito.mock(Exchange.class); Message inMessage = PowerMockito.mock(Message.class); Message outMessage = PowerMockito.mock(Message.class); - PowerMockito.when(exchange.getIn()).thenReturn(inMessage); + PowerMockito.when(exchange.getIn()).thenReturn(inMessage); PowerMockito.when(inMessage.getBody()).thenReturn(obj); PowerMockito.when(exchange.getOut()).thenReturn(outMessage); @@ -105,5 +143,4 @@ public class SpikeAutosuggestProcessorTest { } - } diff --git a/src/test/java/org/onap/aai/datarouter/policy/SpikeEntityEventPolicyStubbed.java b/src/test/java/org/onap/aai/datarouter/policy/SpikeEntityEventPolicyStubbed.java index 17a76e8..e0356a9 100644 --- a/src/test/java/org/onap/aai/datarouter/policy/SpikeEntityEventPolicyStubbed.java +++ b/src/test/java/org/onap/aai/datarouter/policy/SpikeEntityEventPolicyStubbed.java @@ -25,30 +25,43 @@ import java.io.FileNotFoundException; import org.onap.aai.datarouter.entity.DocumentStoreDataEntity; public class SpikeEntityEventPolicyStubbed extends SpikeEntityEventPolicy { - - - public SpikeEntityEventPolicyStubbed(SpikeEntityEventPolicyConfig config) throws FileNotFoundException { - super(config); - - } - - protected void handleSearchServiceOperation(DocumentStoreDataEntity eventEntity, String action, String index) { - //Stub out the actual call to Search Data service and instead store/update documents in memory - try { - switch (action.toLowerCase()) { - case "create": - InMemorySearchDatastore.put(eventEntity.getId(), eventEntity.getAsJson()); // they are executed if variable == c1 - break; - case "update": - InMemorySearchDatastore.put(eventEntity.getId(), eventEntity.getAsJson()); // they are executed if variable == c1 - break; - case "delete": - InMemorySearchDatastore.remove(eventEntity.getId()); // they are executed if variable == c1 - break; - default: - break; - } - } catch (Exception ex) { - } - } + + private InMemorySearchDatastore searchDb; + + public SpikeEntityEventPolicyStubbed(SpikeEntityEventPolicyConfig config) + throws FileNotFoundException { + super(config); + } + + public SpikeEntityEventPolicyStubbed withSearchDb(InMemorySearchDatastore searchDb) { + this.searchDb = searchDb; + return this; + } + + public InMemorySearchDatastore getSearchDb() { + return searchDb; + } + + protected void handleSearchServiceOperation(DocumentStoreDataEntity eventEntity, String action, + String index) { + // Stub out the actual call to Search Data service and instead store/update documents in memory + try { + switch (action.toLowerCase()) { + case "create": + searchDb.put(eventEntity.getId(), eventEntity.getAsJson()); // they are executed if + // variable == c1 + break; + case "update": + searchDb.put(eventEntity.getId(), eventEntity.getAsJson()); // they are executed if + // variable == c1 + break; + case "delete": + searchDb.remove(eventEntity.getId()); // they are executed if variable == c1 + break; + default: + break; + } + } catch (Exception ex) { + } + } } diff --git a/src/test/java/org/onap/aai/datarouter/policy/SpikeEntityEventPolicyTest.java b/src/test/java/org/onap/aai/datarouter/policy/SpikeEntityEventPolicyTest.java index 69d3336..68edb74 100644 --- a/src/test/java/org/onap/aai/datarouter/policy/SpikeEntityEventPolicyTest.java +++ b/src/test/java/org/onap/aai/datarouter/policy/SpikeEntityEventPolicyTest.java @@ -20,8 +20,7 @@ */ package org.onap.aai.datarouter.policy; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.junit.Assert.*; import static org.mockito.Matchers.anyObject; import static org.mockito.Matchers.anyString; @@ -34,67 +33,83 @@ import org.apache.commons.io.IOUtils; import org.junit.Before; import org.junit.Test; import org.onap.aai.datarouter.util.NodeUtils; -import org.onap.aai.datarouter.util.SearchServiceAgent; import org.powermock.api.mockito.PowerMockito; - - public class SpikeEntityEventPolicyTest { - SpikeEntityEventPolicy policy; - String eventJson; - - @SuppressWarnings("unchecked") + private SpikeEntityEventPolicyConfig eventPolicyConfig; + private SpikeEntityEventPolicy policy; + private InMemorySearchDatastore searchDb; + + @Before public void init() throws Exception { - SpikeEntityEventPolicyConfig config = PowerMockito.mock(SpikeEntityEventPolicyConfig.class); - PowerMockito.when(config.getSearchKeystorePwd()).thenReturn("password"); - PowerMockito.when(config.getSourceDomain()).thenReturn("JUNIT"); - - - SearchServiceAgent searchServiceAgent = PowerMockito.mock(SearchServiceAgent.class); - PowerMockito.whenNew(SearchServiceAgent.class).withAnyArguments() - .thenReturn(searchServiceAgent); - + + eventPolicyConfig = new SpikeEntityEventPolicyConfig(); + eventPolicyConfig.setSearchKeystorePwd("password"); + eventPolicyConfig.setSourceDomain("JUNIT"); - policy = new SpikeEntityEventPolicyStubbed(config); - FileInputStream event = new FileInputStream(new File("src/test/resources/spike_event.json")); - eventJson = IOUtils.toString(event, "UTF-8"); + searchDb = new InMemorySearchDatastore(); + policy = new SpikeEntityEventPolicyStubbed(eventPolicyConfig).withSearchDb(searchDb); } @Test public void testProcess_success() throws Exception { - policy.process(getExchangeEvent("12345", "create", "generic-vnf")); - policy.process(getExchangeEvent("23456", "create", "generic-vnf")); + + String pserverEventJsonTemplate = IOUtils.toString( + new FileInputStream(new File("src/test/resources/pserver-spike-event.json")), "UTF-8"); + + policy.process(getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "pserver123")); assertNotNull( - InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("generic-vnf/12345"))); - assertNotNull( - InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("generic-vnf/23456"))); - - - policy.process(getExchangeEvent("23456", "delete", "generic-vnf")); - assertNull(InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("23456"))); + searchDb.get(NodeUtils.generateUniqueShaDigest("pserver/pserver123"))); } + @Test - public void testProcess_fail() throws Exception { - policy.process(getExchangeEvent("12345", "create", "NotValid")); - assertNull( - InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("NotValid/12345"))); + public void testProcess_failure_unknownOxmEntityType() throws Exception { - policy.process(getExchangeEvent("", "create", "generic-vnf")); + String pserverEventJsonTemplate = IOUtils.toString( + new FileInputStream(new File("src/test/resources/optical-router-spike-event.json")), "UTF-8"); + + policy.process(getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "optronic123")); + assertNull( - InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("generic-vnf/"))); + searchDb.get(NodeUtils.generateUniqueShaDigest("optical-router/optronic123"))); + } + + @Test + public void testProcess_failure_missingMandatoryFieldsFromBodyObject() throws Exception { + + String pserverEventJsonTemplate = IOUtils.toString( + new FileInputStream(new File("src/test/resources/pserver-missing-mandtory-field-spike-event.json")), "UTF-8"); + + policy.process(getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "pserver123")); + assertNull( + searchDb.get(NodeUtils.generateUniqueShaDigest("pserver/pserver123"))); } + + @Test + public void testProcess_failure_missingMandatoryVertexProperties() throws Exception { + + String pserverEventJsonTemplate = IOUtils.toString( + new FileInputStream(new File("src/test/resources/pserver-missing-primary-key-spike-event.json")), "UTF-8"); + + policy.process(getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "pserver123")); + assertNull( + searchDb.get(NodeUtils.generateUniqueShaDigest("pserver/pserver123"))); + } - private Exchange getExchangeEvent(String key, String action, String type) { - Object obj = eventJson.replace("$KEY", key).replace("$ACTION", action).replace("$TYPE", type); + private Exchange getExchangeEvent(String payloadTemplate, String eventType, String operationType, + String entityKey) { + Object obj = payloadTemplate.replace("$EVENT_TYPE", eventType) + .replace("$OPERATION_TYPE", operationType).replace("$ENTITY_KEY", entityKey); + Exchange exchange = PowerMockito.mock(Exchange.class); Message inMessage = PowerMockito.mock(Message.class); Message outMessage = PowerMockito.mock(Message.class); - PowerMockito.when(exchange.getIn()).thenReturn(inMessage); + PowerMockito.when(exchange.getIn()).thenReturn(inMessage); PowerMockito.when(inMessage.getBody()).thenReturn(obj); PowerMockito.when(exchange.getOut()).thenReturn(outMessage); diff --git a/src/test/java/org/onap/aai/datarouter/policy/SpikeEntitySearchProcessorStubbed.java b/src/test/java/org/onap/aai/datarouter/policy/SpikeEntitySearchProcessorStubbed.java index 100ff0a..5c82c4d 100644 --- a/src/test/java/org/onap/aai/datarouter/policy/SpikeEntitySearchProcessorStubbed.java +++ b/src/test/java/org/onap/aai/datarouter/policy/SpikeEntitySearchProcessorStubbed.java @@ -25,30 +25,44 @@ import java.io.FileNotFoundException; import org.onap.aai.datarouter.entity.DocumentStoreDataEntity; public class SpikeEntitySearchProcessorStubbed extends SpikeEntitySearchProcessor { - - - public SpikeEntitySearchProcessorStubbed(SpikeEventPolicyConfig config) throws FileNotFoundException { - super(config); - - } - - protected void handleSearchServiceOperation(DocumentStoreDataEntity eventEntity, String action, String index) { - //Stub out the actual call to Search Data service and instead store/update documents in memory - try { - switch (action.toLowerCase()) { - case "create": - InMemorySearchDatastore.put(eventEntity.getId(), eventEntity.getAsJson()); // they are executed if variable == c1 - break; - case "update": - InMemorySearchDatastore.put(eventEntity.getId(), eventEntity.getAsJson()); // they are executed if variable == c1 - break; - case "delete": - InMemorySearchDatastore.remove(eventEntity.getId()); // they are executed if variable == c1 - break; - default: - break; - } - } catch (Exception ex) { - } - } + + private InMemorySearchDatastore searchDb; + + public SpikeEntitySearchProcessorStubbed(SpikeEventPolicyConfig config) + throws FileNotFoundException { + super(config); + } + + public SpikeEntitySearchProcessorStubbed withSearchDb(InMemorySearchDatastore searchDb) { + this.searchDb = searchDb; + return this; + } + + + public InMemorySearchDatastore getSearchDb() { + return searchDb; + } + + protected void handleSearchServiceOperation(DocumentStoreDataEntity eventEntity, String action, + String index) { + // Stub out the actual call to Search Data service and instead store/update documents in memory + try { + switch (action.toLowerCase()) { + case "create": + searchDb.put(eventEntity.getId(), eventEntity.getAsJson()); // they are executed if + // variable == c1 + break; + case "update": + searchDb.put(eventEntity.getId(), eventEntity.getAsJson()); // they are executed if + // variable == c1 + break; + case "delete": + searchDb.remove(eventEntity.getId()); // they are executed if variable == c1 + break; + default: + break; + } + } catch (Exception ex) { + } + } } diff --git a/src/test/java/org/onap/aai/datarouter/policy/SpikeEntitySearchProcessorTest.java b/src/test/java/org/onap/aai/datarouter/policy/SpikeEntitySearchProcessorTest.java index e6bf390..5b40c1a 100644 --- a/src/test/java/org/onap/aai/datarouter/policy/SpikeEntitySearchProcessorTest.java +++ b/src/test/java/org/onap/aai/datarouter/policy/SpikeEntitySearchProcessorTest.java @@ -34,104 +34,119 @@ import org.apache.commons.io.IOUtils; import org.junit.Before; import org.junit.Test; import org.onap.aai.datarouter.util.NodeUtils; -import org.onap.aai.datarouter.util.SearchServiceAgent; import org.powermock.api.mockito.PowerMockito; - - public class SpikeEntitySearchProcessorTest { - SpikeEntitySearchProcessor policy; - String eventJson; + private SpikeEntitySearchProcessor policy; + private String eventJson; + private InMemorySearchDatastore searchDb; - @SuppressWarnings("unchecked") @Before public void init() throws Exception { SpikeEventPolicyConfig config = PowerMockito.mock(SpikeEventPolicyConfig.class); PowerMockito.when(config.getSearchKeystorePwd()).thenReturn("password"); PowerMockito.when(config.getSourceDomain()).thenReturn("JUNIT"); + searchDb = new InMemorySearchDatastore(); + policy = new SpikeEntitySearchProcessorStubbed(config).withSearchDb(searchDb); + + } + + @Test + public void testProcess_success() throws Exception { + + String genericVnfEventJsonTemplate = IOUtils.toString( + new FileInputStream(new File("src/test/resources/generic-vnf-spike-event.json")), "UTF-8"); - SearchServiceAgent searchServiceAgent = PowerMockito.mock(SearchServiceAgent.class); - PowerMockito.whenNew(SearchServiceAgent.class).withAnyArguments() - .thenReturn(searchServiceAgent); + policy.process( + getExchangeEvent(genericVnfEventJsonTemplate, "update-notification", "CREATE", "gvnf123")); + assertNotNull(searchDb.get(NodeUtils.generateUniqueShaDigest("generic-vnf/gvnf123"))); - policy = new SpikeEntitySearchProcessorStubbed(config); - FileInputStream event = new FileInputStream(new File("src/test/resources/spike_event.json")); - eventJson = IOUtils.toString(event, "UTF-8"); + policy.process( + getExchangeEvent(genericVnfEventJsonTemplate, "update-notification", "DELETE", "gvnf123")); + assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("generic-vnf/gvnf123"))); + + } + /* + * Failure test cases - no searchable attribute for type + */ @Test - public void testProcess_success() throws Exception { - policy.process(getExchangeEvent("12345", "create", "generic-vnf")); - policy.process(getExchangeEvent("23456", "create", "generic-vnf")); + public void testProcess_failure_unknownOxmEntityType() throws Exception { - assertNotNull( - InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("generic-vnf/12345"))); - assertNotNull( - InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("generic-vnf/23456"))); + String pserverEventJsonTemplate = IOUtils.toString( + new FileInputStream(new File("src/test/resources/optical-router-spike-event.json")), + "UTF-8"); - - policy.process(getExchangeEvent("23456", "delete", "generic-vnf")); - assertNull(InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("generic-vnf/23456"))); - - policy.process(getExchangeEvent("333333", "", "generic-vnf")); - assertNull(InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("generic-vnf/333333"))); + policy.process( + getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "optronic123")); + + assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("optical-router/optronic123"))); } + @Test - public void testProcess_fail() throws Exception { - policy.process(getExchangeEvent("xxxxx", "create", "NotValid")); - assertNull( - InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("NotValid/xxxxx"))); - - policy.process(getExchangeEvent("", "create", "generic-vnf")); - assertNull( - InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("generic-vnf/"))); - - policy.process(getExchangeEvent("yyyy", "create", "")); - assertNull( - InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("/yyyy"))); - policy.process(getExchangeEvent("", "create", "")); - assertNull( - InMemorySearchDatastore.get(NodeUtils.generateUniqueShaDigest("/"))); + public void testProcess_failure_missingMandatoryFieldsFromBodyObject() throws Exception { + + String pserverEventJsonTemplate = IOUtils.toString( + new FileInputStream( + new File("src/test/resources/pserver-missing-mandtory-field-spike-event.json")), + "UTF-8"); + + policy.process( + getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "pserver123")); + + assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("pserver/pserver123"))); } + @Test - public void testProcess_null() throws Exception { - policy.process(getExchangeEvent()); + public void testProcess_failure_missingMandatoryVertexProperties() throws Exception { + + String pserverEventJsonTemplate = + IOUtils.toString( + new FileInputStream( + new File("src/test/resources/pserver-missing-primary-key-spike-event.json")), + "UTF-8"); + + policy.process( + getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "pserver123")); + + assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("pserver/pserver123"))); } - - private Exchange getExchangeEvent(String key, String action, String type) { - Object obj = eventJson.replace("$KEY", key).replace("$ACTION", action).replace("$TYPE", type); - Exchange exchange = PowerMockito.mock(Exchange.class); - Message inMessage = PowerMockito.mock(Message.class); - Message outMessage = PowerMockito.mock(Message.class); - PowerMockito.when(exchange.getIn()).thenReturn(inMessage); - PowerMockito.when(inMessage.getBody()).thenReturn(obj); - PowerMockito.when(exchange.getOut()).thenReturn(outMessage); - PowerMockito.doNothing().when(outMessage).setBody(anyObject()); - PowerMockito.doNothing().when(outMessage).setHeader(anyString(), anyObject()); + @Test + public void testProcess_failure_noSuggestibleAttributesForEntityType() throws Exception { - return exchange; + String pserverEventJsonTemplate = IOUtils.toString( + new FileInputStream(new File("src/test/resources/vserver-spike-event.json")), "UTF-8"); + policy.process( + getExchangeEvent(pserverEventJsonTemplate, "update-notification", "CREATE", "vserver123")); + + assertNull(searchDb.get(NodeUtils.generateUniqueShaDigest("vserver/vserver123"))); } - - private Exchange getExchangeEvent() { - Object obj = ""; + + private Exchange getExchangeEvent(String payloadTemplate, String eventType, String operationType, + String entityKey) { + Object obj = payloadTemplate.replace("$EVENT_TYPE", eventType) + .replace("$OPERATION_TYPE", operationType).replace("$ENTITY_KEY", entityKey); + Exchange exchange = PowerMockito.mock(Exchange.class); Message inMessage = PowerMockito.mock(Message.class); Message outMessage = PowerMockito.mock(Message.class); - PowerMockito.when(exchange.getIn()).thenReturn(inMessage); + PowerMockito.when(exchange.getIn()).thenReturn(inMessage); PowerMockito.when(inMessage.getBody()).thenReturn(obj); PowerMockito.when(exchange.getOut()).thenReturn(outMessage); PowerMockito.doNothing().when(outMessage).setBody(anyObject()); PowerMockito.doNothing().when(outMessage).setHeader(anyString(), anyObject()); - + return exchange; } + } |