diff --git a/integration-tests/src/test/java/oracle/weblogic/kubernetes/ItKubernetesEvents.java b/integration-tests/src/test/java/oracle/weblogic/kubernetes/ItKubernetesEvents.java index ba771eadaa2..d8a5884fade 100644 --- a/integration-tests/src/test/java/oracle/weblogic/kubernetes/ItKubernetesEvents.java +++ b/integration-tests/src/test/java/oracle/weblogic/kubernetes/ItKubernetesEvents.java @@ -710,9 +710,8 @@ public void testK8SEventsStartStopWatchingNSWithDedicated() { logger.info("verify NamespaceWatchingStarted event is logged in {0}", opNamespace); checkEvent(opNamespace, opNamespace, null, NAMESPACE_WATCHING_STARTED, "Normal", timestamp); - // TODO: enable the following check when https://jira.oraclecorp.com/jira/browse/OWLS-87181 is fixed - //logger.info("verify NamespaceWatchingStopped event is logged in {0}", domainNamespace4); - //checkNamespaceWatchingStoppedEvent(opNamespace, domainNamespace4, null, "Normal", timestamp, false); + logger.info("verify NamespaceWatchingStopped event is logged in {0}", domainNamespace4); + checkNamespaceWatchingStoppedEvent(opNamespace, domainNamespace4, null, "Normal", timestamp, false); } /** diff --git a/operator/src/main/java/oracle/kubernetes/operator/DomainNamespaces.java b/operator/src/main/java/oracle/kubernetes/operator/DomainNamespaces.java index a46e3ab9a07..f6bc57abfc7 100644 --- a/operator/src/main/java/oracle/kubernetes/operator/DomainNamespaces.java +++ b/operator/src/main/java/oracle/kubernetes/operator/DomainNamespaces.java @@ -118,6 +118,8 @@ void stopNamespace(String ns) { podDisruptionBudgetWatchers.removeWatcher(ns); configMapWatchers.removeWatcher(ns); jobWatchers.removeWatcher(ns); + + DomainProcessorImpl.cleanupNamespace(ns); } ConfigMapWatcher getConfigMapWatcher(String namespace) { diff --git a/operator/src/main/java/oracle/kubernetes/operator/DomainProcessorImpl.java b/operator/src/main/java/oracle/kubernetes/operator/DomainProcessorImpl.java index 9a2afcf20ab..2f66f0f8711 100644 --- a/operator/src/main/java/oracle/kubernetes/operator/DomainProcessorImpl.java +++ b/operator/src/main/java/oracle/kubernetes/operator/DomainProcessorImpl.java @@ -122,6 +122,13 @@ private static DomainPresenceInfo getExistingDomainPresenceInfo(String ns, Strin return DOMAINS.computeIfAbsent(ns, k -> new ConcurrentHashMap<>()).get(domainUid); } + static void cleanupNamespace(String namespace) { + DOMAINS.remove(namespace); + domainEventK8SObjects.remove(namespace); + namespaceEventK8SObjects.remove(namespace); + statusUpdaters.remove((namespace)); + } + static void registerDomainPresenceInfo(DomainPresenceInfo info) { DOMAINS .computeIfAbsent(info.getNamespace(), k -> new ConcurrentHashMap<>()) @@ -129,10 +136,16 @@ static void registerDomainPresenceInfo(DomainPresenceInfo info) { } private static void unregisterPresenceInfo(String ns, String domainUid) { - Map map = DOMAINS.get(ns); - if (map != null) { - map.remove(domainUid); - } + Optional.ofNullable(DOMAINS.get(ns)).map(m -> m.remove(domainUid)); + } + + private static void unregisterEventK8SObject(String ns, String domainUid) { + Optional.ofNullable(domainEventK8SObjects.get(ns)).map(m -> m.remove(domainUid)); + } + + private static void unregisterDomain(String ns, String domainUid) { + unregisterPresenceInfo(ns, domainUid); + unregisterEventK8SObject(ns, domainUid); } private static void registerStatusUpdater( @@ -1189,7 +1202,7 @@ private static class UnregisterStep extends Step { @Override public NextAction apply(Packet packet) { - unregisterPresenceInfo(info.getNamespace(), info.getDomainUid()); + unregisterDomain(info.getNamespace(), info.getDomainUid()); return doNext(packet); } } diff --git a/operator/src/main/java/oracle/kubernetes/operator/DomainRecheck.java b/operator/src/main/java/oracle/kubernetes/operator/DomainRecheck.java index 640cd70b3e6..8225ea6bcd8 100644 --- a/operator/src/main/java/oracle/kubernetes/operator/DomainRecheck.java +++ b/operator/src/main/java/oracle/kubernetes/operator/DomainRecheck.java @@ -124,7 +124,8 @@ class ReadNamespacesStepsVisitor implements NamespaceStrategyVisitor { @Override public Step getDedicatedStrategySelection() { - return createStartNamespacesStep(Collections.singletonList(getOperatorNamespace())); + return Step.chain(new Namespaces.NamespaceListAfterStep(domainNamespaces), + createStartNamespacesStep(Collections.singletonList(getOperatorNamespace()))); } @Override diff --git a/operator/src/main/java/oracle/kubernetes/operator/Namespaces.java b/operator/src/main/java/oracle/kubernetes/operator/Namespaces.java index 4f9b2e7d1ad..74787ec2d3d 100644 --- a/operator/src/main/java/oracle/kubernetes/operator/Namespaces.java +++ b/operator/src/main/java/oracle/kubernetes/operator/Namespaces.java @@ -16,6 +16,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; +import jakarta.validation.constraints.NotNull; import oracle.kubernetes.operator.helpers.EventHelper.EventData; import oracle.kubernetes.operator.helpers.HelmAccess; import oracle.kubernetes.operator.helpers.NamespaceHelper; @@ -60,6 +61,12 @@ static boolean isDomainNamespace(String ns) { return getSelectionStrategy().getConfiguredDomainNamespaces(); } + /** + * Returns a (possibly empty) collection of strings which designate namespaces for the operator to manage. + */ + static @NotNull Collection getFoundDomainNamespaces(Packet packet) { + return getSelectionStrategy().getFoundDomainNamespaces(packet); + } /** * Returns an array of the label selectors that will determine that a namespace is being used to manage domains. @@ -157,6 +164,11 @@ public Collection getConfiguredDomainNamespaces() { public V getSelection(NamespaceStrategyVisitor visitor) { return visitor.getDedicatedStrategySelection(); } + + @Override + public Collection getFoundDomainNamespaces(Packet packet) { + return Collections.singleton(getOperatorNamespace()); + } }; static final String[] NO_SELECTORS = new String[0]; @@ -174,22 +186,23 @@ public String[] getLabelSelectors() { public abstract V getSelection(NamespaceStrategyVisitor visitor); private static final Map compiledPatterns = new WeakHashMap<>(); - } - /** - * Returns a modifiable collection of found namespace names in a packet. - * Callers should use this to add to the collection. - * - * @param packet the packet passed to a step - */ - @SuppressWarnings("unchecked") - static Collection getFoundDomainNamespaces(Packet packet) { - if (!packet.containsKey(ALL_DOMAIN_NAMESPACES)) { - packet.put(ALL_DOMAIN_NAMESPACES, new HashSet<>()); + /** + * Returns a modifiable collection of found namespace names in a packet. + * Callers should use this to add to the collection. + * + * @param packet the packet passed to a step + */ + @SuppressWarnings("unchecked") + Collection getFoundDomainNamespaces(Packet packet) { + if (!packet.containsKey(ALL_DOMAIN_NAMESPACES)) { + packet.put(ALL_DOMAIN_NAMESPACES, new HashSet<>()); + } + return (Collection) packet.get(ALL_DOMAIN_NAMESPACES); } - return (Collection) packet.get(ALL_DOMAIN_NAMESPACES); } + /** * Gets the configured domain namespace selection strategy. * @@ -271,9 +284,6 @@ public NextAction apply(Packet packet) { return doForkJoin(getNext(), packet, nsStopEventDetails); } } - - - } @Nonnull diff --git a/operator/src/test/java/oracle/kubernetes/operator/MainTest.java b/operator/src/test/java/oracle/kubernetes/operator/MainTest.java index 973b8b489e1..afd364049b9 100644 --- a/operator/src/test/java/oracle/kubernetes/operator/MainTest.java +++ b/operator/src/test/java/oracle/kubernetes/operator/MainTest.java @@ -345,6 +345,10 @@ private void recheckDomains() { testSupport.runSteps(main.createDomainRecheckSteps()); } + private void runCreateReadNamespacesStep() { + testSupport.runSteps(new DomainRecheck(delegate, false).createReadNamespacesStep()); + } + @Test public void whenNoCRD_logReasonForFailure() { loggerControl.withLogLevel(Level.SEVERE).collectLogMessages(logRecords, CRD_NOT_INSTALLED); @@ -394,14 +398,14 @@ public void afterMissingCRDcorrected_subsequentFailureLogsReasonForFailure() { } @Test - public void withNamespaceList_onStartNamespaceStep_startsNamespaces() { + public void withNamespaceList_onCreateReadNamespaces_startsNamespaces() { defineSelectionStrategy(SelectionStrategy.List); HelmAccessStub.defineVariable(HelmAccess.OPERATOR_DOMAIN_NAMESPACES, String.join(",", NS_WEBLOGIC1, NS_WEBLOGIC2, NS_WEBLOGIC3)); testSupport.defineResources(NAMESPACE_WEBLOGIC1, NAMESPACE_WEBLOGIC2, NAMESPACE_WEBLOGIC3, NAMESPACE_WEBLOGIC4, NAMESPACE_WEBLOGIC5); - testSupport.runSteps(createDomainRecheck().readExistingNamespaces()); + runCreateReadNamespacesStep(); assertThat(getStartingNamespaces(), contains(NS_WEBLOGIC1, NS_WEBLOGIC2, NS_WEBLOGIC3)); } @@ -443,25 +447,25 @@ public void describeTo(Description description) { } @Test - public void withRegExp_onReadExistingNamespaces_startsNamespaces() { + public void withRegExp_onCreateReadNamespaces_startsNamespaces() { defineSelectionStrategy(SelectionStrategy.RegExp); testSupport.defineResources(NAMESPACE_WEBLOGIC1, NAMESPACE_WEBLOGIC2, NAMESPACE_WEBLOGIC3, NAMESPACE_WEBLOGIC4, NAMESPACE_WEBLOGIC5); TuningParameters.getInstance().put("domainNamespaceRegExp", REGEXP); - testSupport.runSteps(createDomainRecheck().readExistingNamespaces()); + runCreateReadNamespacesStep(); assertThat(getStartingNamespaces(), contains(NS_WEBLOGIC2, NS_WEBLOGIC4)); } @Test - public void withLabelSelector_onReadExistingNamespaces_startsNamespaces() { + public void withLabelSelector_onCreateReadNamespaces_startsNamespaces() { defineSelectionStrategy(SelectionStrategy.LabelSelector); testSupport.defineResources(NAMESPACE_WEBLOGIC1, NAMESPACE_WEBLOGIC2, NAMESPACE_WEBLOGIC3, NAMESPACE_WEBLOGIC4, NAMESPACE_WEBLOGIC5); TuningParameters.getInstance().put("domainNamespaceLabelSelector", LABEL + "=" + VALUE); - testSupport.runSteps(createDomainRecheck().readExistingNamespaces()); + runCreateReadNamespacesStep(); assertThat(getStartingNamespaces(), contains(NS_WEBLOGIC1, NS_WEBLOGIC3, NS_WEBLOGIC5)); } @@ -750,7 +754,7 @@ public void withNamespaceList_onCreateStartNamespacesStep_startManagingNSEventCr testSupport.defineResources(NAMESPACE_WEBLOGIC1, NAMESPACE_WEBLOGIC2); - List namespaces = Arrays.asList(NS_WEBLOGIC1); + List namespaces = Collections.singletonList(NS_WEBLOGIC1); testSupport.runSteps( createDomainRecheck().createStartNamespacesStep(namespaces)); @@ -781,42 +785,41 @@ public void withNamespaceList_onCreateStartNamespacesStep_foundExpectedLogMessag } @Test - public void withNamespaceList_onReadExistingNamespaces_whenConfiguredDomainNamespaceMissing_noEventCreated() { + public void withNamespaceList_onCreateReadNamespaces_whenConfiguredDomainNamespaceMissing_noEventCreated() { defineSelectionStrategy(SelectionStrategy.List); String namespaceString = "NS" + LAST_NAMESPACE_NUM + ",NS" + DEFAULT_CALL_LIMIT; HelmAccessStub.defineVariable(HelmAccess.OPERATOR_DOMAIN_NAMESPACES, namespaceString); createNamespaces(LAST_NAMESPACE_NUM - 1); - testSupport.runSteps(createDomainRecheck().readExistingNamespaces()); + runCreateReadNamespacesStep(); assertThat("Found no event", containsEvent(getEvents(testSupport), NAMESPACE_WATCHING_STARTED_EVENT), is(false)); } @Test - public void withNamespaceList_onReadExistingNamespaces_whenDomainNamespaceRemoved_nsWatchStoppedEventCreated() { + public void withNamespaceList_onCreateReadNamespaces_whenDomainNamespaceRemoved_nsWatchStoppedEventCreated() { domainNamespaces.isStopping("NS3"); defineSelectionStrategy(SelectionStrategy.List); String namespaceString = "NS1,NS2"; HelmAccessStub.defineVariable(HelmAccess.OPERATOR_DOMAIN_NAMESPACES, namespaceString); createNamespaces(4); - testSupport.runSteps(createDomainRecheck().readExistingNamespaces()); - + runCreateReadNamespacesStep(); assertThat("Found NAMESPACE_WATCHING_STOPPED event with expected message", containsEventWithMessageForNamespaces(getEvents(testSupport), NAMESPACE_WATCHING_STOPPED, Collections.singletonList("NS3")), is(true)); } @Test - public void withNamespaceList_onReadExistingNamespaces_whenDomainNamespaceRemoved_stopManagingNSEventCreated() { + public void withNamespaceList_onCreateReadNamespaces_whenDomainNamespaceRemoved_stopManagingNSEventCreated() { domainNamespaces.isStopping("NS3"); defineSelectionStrategy(SelectionStrategy.List); String namespaceString = "NS1,NS2"; HelmAccessStub.defineVariable(HelmAccess.OPERATOR_DOMAIN_NAMESPACES, namespaceString); createNamespaces(4); - testSupport.runSteps(createDomainRecheck().readExistingNamespaces()); + runCreateReadNamespacesStep(); assertThat("Found NAMESPACE_WATCHING_STOPPED event with expected message", containsEventWithMessageForNamespaces(getEvents(testSupport), @@ -824,7 +827,7 @@ public void withNamespaceList_onReadExistingNamespaces_whenDomainNamespaceRemove } @Test - public void withNamespaceList_onReadExistingNamespaces_whenDomainNamespaceRemoved_foundExpectedLogMessage() { + public void withNamespaceList_onCreateReadNamespaces_whenDomainNamespaceRemoved_foundExpectedLogMessage() { logRecords.clear(); loggerControl.withLogLevel(Level.INFO).collectLogMessages(logRecords, MessageKeys.END_MANAGING_NAMESPACE); domainNamespaces.isStopping("NS3"); @@ -833,10 +836,69 @@ public void withNamespaceList_onReadExistingNamespaces_whenDomainNamespaceRemove HelmAccessStub.defineVariable(HelmAccess.OPERATOR_DOMAIN_NAMESPACES, namespaceString); createNamespaces(4); - testSupport.runSteps(createDomainRecheck().readExistingNamespaces()); + runCreateReadNamespacesStep(); assertThat(logRecords, containsInfo(MessageKeys.END_MANAGING_NAMESPACE, "NS3")); } + @Test + public void withNamespaceList_changeToDedicated_onCreateReadNamespaces_nsWatchStoppedEventCreated() { + defineNamespaceListStrategy("NS1"); + runCreateReadNamespacesStep(); + + defineSelectionStrategy(SelectionStrategy.Dedicated); + runCreateReadNamespacesStep(); + + assertThat("Found NAMESPACE_WATCHING_STOPPED event with expected message", + containsEventWithMessageForNamespaces(getEvents(testSupport), + NAMESPACE_WATCHING_STOPPED, Collections.singletonList("NS1")), is(true)); + } + + @Test + public void withNamespaceList_changeToDedicated_onCreateReadNamespaces_nsWatchStartedEventCreatedInOpNS() { + defineNamespaceListStrategy("NS1"); + runCreateReadNamespacesStep(); + + defineSelectionStrategy(SelectionStrategy.Dedicated); + runCreateReadNamespacesStep(); + + assertThat("Found NAMESPACE_WATCHING_STARTED event with expected message", + containsEventWithMessageForNamespaces(getEvents(testSupport), + NAMESPACE_WATCHING_STARTED, Collections.singletonList(OP_NS)), is(true)); + } + + private void defineNamespaceListStrategy(String namespaceString) { + defineSelectionStrategy(SelectionStrategy.List); + HelmAccessStub.defineVariable(HelmAccess.OPERATOR_DOMAIN_NAMESPACES, namespaceString); + createNamespaces(4); + } + + @Test + public void withNamespaceList_changeToDedicated_onCreateReadNamespaces_StartManagingEventCreatedInOpNS() { + defineNamespaceListStrategy("NS1"); + runCreateReadNamespacesStep(); + + defineSelectionStrategy(SelectionStrategy.Dedicated); + runCreateReadNamespacesStep(); + + assertThat("Found START_MANAGING_NAMESPACE event with expected message", + containsEventWithMessageForNamespaces(getEvents(testSupport), + START_MANAGING_NAMESPACE, Collections.singletonList(OP_NS)), is(true)); + } + + @Test + public void withNamespaceList_changeToDedicated_onCreateReadNamespaces_nsEventMapIsEmpty() { + defineNamespaceListStrategy("NS1"); + runCreateReadNamespacesStep(); + + assertThat("Confirmed that the event maps for the namespace are empty before changing strategy", + eventMapsEmpty("NS1"), is(false)); + + defineSelectionStrategy(SelectionStrategy.Dedicated); + runCreateReadNamespacesStep(); + + assertThat("Confirmed that the event maps for the namespace are empty", eventMapsEmpty("NS1"), is(true)); + } + @Test public void withNamespaceLabelSelector_onCreateStartNamespacesStep_nsWatchStartedEventCreatedWithExpectedMessage() { defineSelectionStrategy(SelectionStrategy.LabelSelector); @@ -873,7 +935,7 @@ public void withNamespaceLabelSelector_onCreateStartNamespacesStep_startManaging @Test - public void withNamespaceLabelSelector_onReadExistingNamespaces_whenLabelRemoved_nsWatchStoppedEventCreated() { + public void withNamespaceLabelSelector_onCreateReadNamespaces_whenLabelRemoved_nsWatchStoppedEventCreated() { domainNamespaces.isStopping("NS3"); testSupport.defineResources(NAMESPACE_WEBLOGIC1, NAMESPACE_WEBLOGIC2, NAMESPACE_WEBLOGIC3, NAMESPACE_WEBLOGIC4, NAMESPACE_WEBLOGIC5); @@ -881,8 +943,7 @@ public void withNamespaceLabelSelector_onReadExistingNamespaces_whenLabelRemoved defineSelectionStrategy(SelectionStrategy.LabelSelector); TuningParameters.getInstance().put("domainNamespaceLabelSelector", LABEL + "=" + VALUE); - testSupport.runSteps(createDomainRecheck().readExistingNamespaces()); - + runCreateReadNamespacesStep(); assertThat("Found NAMESPACE_WATCHING_STOPPED event with expected message", containsEventWithMessageForNamespaces(getEvents(testSupport), @@ -890,15 +951,14 @@ public void withNamespaceLabelSelector_onReadExistingNamespaces_whenLabelRemoved } @Test - public void withNamespaceLabelSelector_onReadExistingNamespaces_whenLabelRemoved_stopManagingNSEventCreated() { + public void withNamespaceLabelSelector_onCreateReadNamespaces_whenLabelRemoved_stopManagingNSEventCreated() { domainNamespaces.isStopping("NS3"); testSupport.defineResources(NAMESPACE_WEBLOGIC1, NAMESPACE_WEBLOGIC2, NAMESPACE_WEBLOGIC3, NAMESPACE_WEBLOGIC4, NAMESPACE_WEBLOGIC5); - defineSelectionStrategy(SelectionStrategy.LabelSelector); TuningParameters.getInstance().put("domainNamespaceLabelSelector", LABEL + "=" + VALUE); - testSupport.runSteps(createDomainRecheck().readExistingNamespaces()); + runCreateReadNamespacesStep(); assertThat("Found NAMESPACE_WATCHING_STOPPED event with expected message", containsEventWithMessageForNamespaces(getEvents(testSupport), @@ -906,17 +966,16 @@ public void withNamespaceLabelSelector_onReadExistingNamespaces_whenLabelRemoved } @Test - public void withNamespaceLabelSelector_onReadExistingNamespaces_whenNamespaceLabelRemoved_foundExpectedLogMessage() { + public void withNamespaceLabelSelector_onCreateReadNamespaces_whenNamespaceLabelRemoved_foundExpectedLogMessage() { logRecords.clear(); loggerControl.withLogLevel(Level.INFO).collectLogMessages(logRecords, MessageKeys.END_MANAGING_NAMESPACE); domainNamespaces.isStopping("NS3"); testSupport.defineResources(NAMESPACE_WEBLOGIC1, NAMESPACE_WEBLOGIC2, NAMESPACE_WEBLOGIC3, NAMESPACE_WEBLOGIC4, NAMESPACE_WEBLOGIC5); - defineSelectionStrategy(SelectionStrategy.LabelSelector); TuningParameters.getInstance().put("domainNamespaceLabelSelector", LABEL + "=" + VALUE); - testSupport.runSteps(createDomainRecheck().readExistingNamespaces()); + runCreateReadNamespacesStep(); assertThat(logRecords, containsInfo(MessageKeys.END_MANAGING_NAMESPACE, "NS3")); } @@ -926,10 +985,9 @@ public void withNamespaceRegExp_onCreateStartNamespacesStep_nsWatchStartedEventC defineSelectionStrategy(SelectionStrategy.RegExp); testSupport.defineResources(NAMESPACE_WEBLOGIC1, NAMESPACE_WEBLOGIC2, NAMESPACE_WEBLOGIC3, NAMESPACE_WEBLOGIC4, NAMESPACE_WEBLOGIC5); - TuningParameters.getInstance().put("domainNamespaceRegExp", REGEXP); - List namespaces = Arrays.asList(NS_WEBLOGIC1, NS_WEBLOGIC2, NS_WEBLOGIC3); + testSupport.runSteps( createDomainRecheck().createStartNamespacesStep(namespaces)); @@ -956,14 +1014,14 @@ public void withNamespaceRegExp_onCreateStartNamespacesStep_startManagingNSEvent } @Test - public void withNamespaceRegExp_onReadExistingNamespaces_whenNamespaceLabelRemoved_nsWatchStoppedEventCreated() { + public void withNamespaceRegExp_onCreateReadNamespaces_whenNamespaceLabelRemoved_nsWatchStoppedEventCreated() { domainNamespaces.isStopping("NS3"); testSupport.defineResources(NAMESPACE_WEBLOGIC1, NAMESPACE_WEBLOGIC2, NAMESPACE_WEBLOGIC3, NAMESPACE_WEBLOGIC4, NAMESPACE_WEBLOGIC5); defineSelectionStrategy(SelectionStrategy.RegExp); TuningParameters.getInstance().put("domainNamespaceRegExp", REGEXP); - testSupport.runSteps(createDomainRecheck().readExistingNamespaces()); + runCreateReadNamespacesStep(); assertThat("Found NAMESPACE_WATCHING_STOPPED event with expected message", containsEventWithMessageForNamespaces(getEvents(testSupport), @@ -971,14 +1029,14 @@ public void withNamespaceRegExp_onReadExistingNamespaces_whenNamespaceLabelRemov } @Test - public void withNamespaceRegExp_onReadExistingNamespaces_whenNamespaceLabelRemoved_stopManagingNSEventCreated() { + public void withNamespaceRegExp_onRCreateReadNamespaces_whenNamespaceLabelRemoved_stopManagingNSEventCreated() { domainNamespaces.isStopping("NS3"); testSupport.defineResources(NAMESPACE_WEBLOGIC1, NAMESPACE_WEBLOGIC2, NAMESPACE_WEBLOGIC3, NAMESPACE_WEBLOGIC4, NAMESPACE_WEBLOGIC5); defineSelectionStrategy(SelectionStrategy.RegExp); TuningParameters.getInstance().put("domainNamespaceRegExp", REGEXP); - testSupport.runSteps(createDomainRecheck().readExistingNamespaces()); + runCreateReadNamespacesStep(); assertThat("Found NAMESPACE_WATCHING_STOPPED event with expected message", containsEventWithMessageForNamespaces(getEvents(testSupport), @@ -986,7 +1044,7 @@ public void withNamespaceRegExp_onReadExistingNamespaces_whenNamespaceLabelRemov } @Test - public void withNamespaceRegExp_onReadExistingNamespaces_whenNamespaceLabelRemoved_foundExpectedLogMessage() { + public void withNamespaceRegExp_onCreateReadNamespaces_whenNamespaceLabelRemoved_foundExpectedLogMessage() { logRecords.clear(); loggerControl.withLogLevel(Level.INFO).collectLogMessages(logRecords, MessageKeys.END_MANAGING_NAMESPACE); domainNamespaces.isStopping("NS3"); @@ -995,38 +1053,98 @@ public void withNamespaceRegExp_onReadExistingNamespaces_whenNamespaceLabelRemov defineSelectionStrategy(SelectionStrategy.RegExp); TuningParameters.getInstance().put("domainNamespaceRegExp", REGEXP); - testSupport.runSteps(createDomainRecheck().readExistingNamespaces()); + runCreateReadNamespacesStep(); assertThat(logRecords, containsInfo(MessageKeys.END_MANAGING_NAMESPACE, "NS3")); } @Test - public void withNamespaceDedicated_onCreateStartNamespacesStep_nsWatchStartedEventCreatedWithExpectedMessage() { + public void withNamespaceDedicated_onCreateReadNamespaces_nsWatchStartedEventCreatedWithExpectedMessage() { defineSelectionStrategy(SelectionStrategy.Dedicated); - - List namespaces = Collections.singletonList(OP_NS); - testSupport.runSteps( - createDomainRecheck().createStartNamespacesStep(namespaces)); + runCreateReadNamespacesStep(); assertThat("Found NAMESPACE_WATCHING_STARTED event with expected message for all namespaces", containsEventWithMessageForNamespaces(getEvents(testSupport), - NAMESPACE_WATCHING_STARTED, namespaces), is(true)); + NAMESPACE_WATCHING_STARTED, Collections.singletonList(OP_NS)), is(true)); assertThat("Found NAMESPACE_WATCHING_STARTED event with expected message for all namespaces", containsEventWithMessageForNamespaces(getEvents(testSupport), - START_MANAGING_NAMESPACE, namespaces), is(true)); + START_MANAGING_NAMESPACE, Collections.singletonList(OP_NS)), is(true)); } @Test - public void withNamespaceDedicated_onCreateStartNamespacesStep_startManagingNSEventCreatedWithExpectedMessage() { + public void withNamespaceDedicated_onCreateReadNamespaces_startManagingNSEventCreatedWithExpectedMessage() { defineSelectionStrategy(SelectionStrategy.Dedicated); - - List namespaces = Arrays.asList(OP_NS); - testSupport.runSteps( - createDomainRecheck().createStartNamespacesStep(namespaces)); + runCreateReadNamespacesStep(); assertThat("Found START_MANAGING_NAMESPACE event with expected message for all namespaces", containsEventWithMessageForNamespaces(getEvents(testSupport), - START_MANAGING_NAMESPACE, namespaces), is(true)); + START_MANAGING_NAMESPACE, Collections.singletonList(OP_NS)), is(true)); + } + + @Test + public void withNamespaceDedicated_changeToList_onCreateReadNamespaces_nsWatchStoppedEventCreatedInOpNS() { + defineSelectionStrategy(SelectionStrategy.Dedicated); + runCreateReadNamespacesStep(); + + defineNamespaceListStrategy("NS1"); + runCreateReadNamespacesStep(); + + assertThat("Found NAMESPACE_WATCHING_STOPPED event with expected message", + containsEventWithMessageForNamespaces(getEvents(testSupport), + NAMESPACE_WATCHING_STOPPED, Collections.singletonList(OP_NS)), is(true)); + } + + @Test + public void withNamespaceDedicated_changeToList_onCreateReadNamespaces_opNSEventMapIsEmpty() { + defineSelectionStrategy(SelectionStrategy.Dedicated); + runCreateReadNamespacesStep(); + + assertThat("Confirm that the event maps for the namespace are not empty before change the strategy", + eventMapsEmpty(OP_NS), is(false)); + + defineNamespaceListStrategy("NS1"); + runCreateReadNamespacesStep(); + + assertThat("Confirm that the event maps for the namespace are empty", + eventMapsEmpty(OP_NS), is(true)); + } + + private boolean eventMapsEmpty(String ns) { + return isNSEventMapEmpty(ns) && isDomainEventMapEmpty(ns); + } + + private boolean isNSEventMapEmpty(String ns) { + return nsEventObjects.get(ns) == null || nsEventObjects.get(ns).size() == 0; + } + + private boolean isDomainEventMapEmpty(String ns) { + return domainEventObjects.get(ns) == null || domainEventObjects.get(ns).size() == 0; + } + + @Test + public void withNamespaceDedicated_changeToList_onCreateReadNamespaces_nsWatchStartedEventCreated() { + defineSelectionStrategy(SelectionStrategy.Dedicated); + runCreateReadNamespacesStep(); + + defineNamespaceListStrategy("NS1"); + runCreateReadNamespacesStep(); + + assertThat("Found NAMESPACE_WATCHING_STARTED event with expected message", + containsEventWithMessageForNamespaces(getEvents(testSupport), + NAMESPACE_WATCHING_STARTED, Collections.singletonList("NS1")), is(true)); + } + + @Test + public void withNamespaceDedicated_changeToList_onCreateReadNamespaces_StartManagingNSEventCreated() { + defineSelectionStrategy(SelectionStrategy.Dedicated); + runCreateReadNamespacesStep(); + + defineNamespaceListStrategy("NS1"); + runCreateReadNamespacesStep(); + + assertThat("Found START_MANAGING_NAMESPACE event with expected message", + containsEventWithMessageForNamespaces(getEvents(testSupport), + START_MANAGING_NAMESPACE, Collections.singletonList("NS1")), is(true)); } abstract static class MainDelegateStub implements MainDelegate {