Skip to content

Commit

Permalink
Issue #1043: Moved EmptyTopicPath from ImmutableTopicPathBuilder
Browse files Browse the repository at this point in the history
…to package level. Added a unit test.

Signed-off-by: Juergen Fickel <juergen.fickel@bosch.io>
  • Loading branch information
Juergen Fickel committed Apr 29, 2021
1 parent bb47f2d commit a5ef80e
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.protocoladapter;

import java.util.Optional;

import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

/**
* Implementation of an empty {@link TopicPath}.
*/
@Immutable
final class EmptyTopicPath implements TopicPath {

private EmptyTopicPath() {
super();
}

/**
* Returns an instance of {@code EmptyTopicPath}.
*
* @return the instance.
*/
static EmptyTopicPath getInstance() {
return new EmptyTopicPath();
}

/**
* @return always {@code null}.
*/
@Override
@Nullable
public String getNamespace() {
return null;
}

/**
* @return always {@code null}.
*/
@Override
@Nullable
public String getEntityName() {
return null;
}

/**
* @return always {@code null}.
*/
@Override
@Nullable
public Group getGroup() {
return null;
}

/**
* @return always {@code null}.
*/
@Override
@Nullable
public Criterion getCriterion() {
return null;
}

/**
* @return always {@code null}.
*/
@Override
@Nullable
public Channel getChannel() {
return null;
}

/**
* @return always an empty Optional.
*/
@Override
public Optional<Action> getAction() {
return Optional.empty();
}

/**
* @return always an empty Optional.
*/
@Override
public Optional<SearchAction> getSearchAction() {
return Optional.empty();
}

/**
* @return always an empty Optional.
*/
@Override
public Optional<String> getSubject() {
return Optional.empty();
}

/**
* @return always an empty String.
*/
@Override
public String getPath() {
return "";
}

@Override
public boolean equals(@Nullable final Object o) {
return this == o || !(o == null || getClass() != o.getClass());
}

@Override
public int hashCode() {
return EmptyTopicPath.class.hashCode();
}

@Override
public String toString() {
return getClass().getSimpleName() + "[]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import static java.util.Objects.requireNonNull;
import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull;

import java.util.Optional;

import javax.annotation.concurrent.NotThreadSafe;

import org.eclipse.ditto.model.base.entity.id.NamespacedEntityId;
Expand Down Expand Up @@ -45,15 +43,6 @@ private ImmutableTopicPathBuilder(final String namespace, final String name) {
this.name = name;
}

/**
* Returns an empty {@code TopicPath}.
*
* @return the topic path.
*/
public static TopicPath empty() {
return EmptyTopicPath.newInstance();
}

/**
* Returns a new TopicPathBuilder for the specified {@code entityId}. The {@code namespace} and {@code id} part of
* the {@code TopicPath} will pe parsed from the {@code entityId} and set in the builder.
Expand Down Expand Up @@ -294,73 +283,4 @@ public TopicPath build() {
}
}

/**
* Implementation of {@link TopicPath} with an empty path.
*/
private static class EmptyTopicPath implements TopicPath {

private EmptyTopicPath() {
// no-op
}

static EmptyTopicPath newInstance() {
return new EmptyTopicPath();
}

@Override
public String getNamespace() {
return null;
}

@Override
public String getEntityName() {
return null;
}

@Override
public Group getGroup() {
return null;
}

@Override
public Criterion getCriterion() {
return null;
}

@Override
public Channel getChannel() {
return null;
}

@Override
public Optional<Action> getAction() {
return Optional.empty();
}

@Override
public Optional<SearchAction> getSearchAction() {
return Optional.empty();
}

@Override
public Optional<String> getSubject() {
return Optional.empty();
}

@Override
public String getPath() {
return "";
}

@Override
public boolean equals(final Object o) {
return this == o || !(o == null || getClass() != o.getClass());
}

@Override
public int hashCode() {
return super.hashCode();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static Adaptable setExtra(final Adaptable existingAdaptable, final JsonOb
* @return the topic path.
*/
public static TopicPath emptyTopicPath() {
return ImmutableTopicPathBuilder.empty();
return EmptyTopicPath.getInstance();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.protocoladapter;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertInstancesOf;
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable;

import org.junit.Before;
import org.junit.Test;

import nl.jqno.equalsverifier.EqualsVerifier;

/**
* Unit test for {@link EmptyTopicPathTest}.
*/
public final class EmptyTopicPathTest {

private EmptyTopicPath underTest;

@Before
public void setUp() {
underTest = EmptyTopicPath.getInstance();
}

@Test
public void assertImmutability() {
assertInstancesOf(EmptyTopicPath.class, areImmutable());
}

@Test
public void testHashCodeAndEquals() {
EqualsVerifier.forClass(EmptyTopicPath.class)
.usingGetClass()
.verify();
}

@Test
public void getInstanceReturnsNotNull() {
assertThat(EmptyTopicPath.getInstance()).isNotNull();
}

@Test
public void getNamespaceReturnsNull() {
assertThat(underTest.getNamespace()).isNull();
}

@Test
public void getEntityNameReturnsNull() {
assertThat(underTest.getEntityName()).isNull();
}

@Test
public void getGroupReturnsNull() {
assertThat(underTest.getGroup()).isNull();
}

@Test
public void getCriterionReturnsNull() {
assertThat(underTest.getCriterion()).isNull();
}

@Test
public void getChannelReturnsNull() {
assertThat(underTest.getChannel()).isNull();
}

@Test
public void getActionReturnsEmptyOptional() {
assertThat(underTest.getAction()).isEmpty();
}

@Test
public void getSearchActionReturnsEmptyOptional() {
assertThat(underTest.getSearchAction()).isEmpty();
}

@Test
public void getSubjectReturnsEmptyOptional() {
assertThat(underTest.getSubject()).isEmpty();
}

@Test
public void getPathReturnsEmptyString() {
assertThat(underTest.getPath()).isEmpty();
}

}

0 comments on commit a5ef80e

Please sign in to comment.