Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FFM-10366 - Delete events throwing ApiException exception #179

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/main/java/io/harness/cf/client/api/UpdateProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ protected Runnable processFlag(@NonNull final Message message) {

return () -> {
try {
final FeatureConfig config = connector.getFlag(message.getIdentifier());
if (config != null) {
if (message.getEvent().equals("create") || message.getEvent().equals("patch")) {
if (message.getEvent().equals("create") || message.getEvent().equals("patch")) {
final FeatureConfig config = connector.getFlag(message.getIdentifier());
if (config != null) {
repository.setFlag(message.getIdentifier(), config);
log.debug("Set new segment with key {} and value {}", message.getIdentifier(), config);
} else if (message.getEvent().equals("delete")) {
log.debug("Delete flag with key {}", message.getIdentifier());
repository.deleteFlag(message.getIdentifier());
}
} else if (message.getEvent().equals("delete")) {
log.debug("Delete flag with key {}", message.getIdentifier());
repository.deleteFlag(message.getIdentifier());
}
} catch (ConnectorException e) {
log.error(
Expand All @@ -121,15 +121,15 @@ protected Runnable processFlag(@NonNull final Message message) {
protected Runnable processSegment(@NonNull final Message message) {
return () -> {
try {
final Segment segment = connector.getSegment(message.getIdentifier());
if (segment != null) {
if (message.getEvent().equals("create") || message.getEvent().equals("patch")) {
if (message.getEvent().equals("create") || message.getEvent().equals("patch")) {
final Segment segment = connector.getSegment(message.getIdentifier());
if (segment != null) {
log.debug("Set new segment with key {} and value {}", message.getIdentifier(), segment);
repository.setSegment(message.getIdentifier(), segment);
} else if (message.getEvent().equals("delete")) {
log.debug("Delete segment with key {}", message.getIdentifier());
repository.deleteSegment(message.getIdentifier());
}
} else if (message.getEvent().equals("delete")) {
log.debug("Delete segment with key {}", message.getIdentifier());
repository.deleteSegment(message.getIdentifier());
}
} catch (ConnectorException e) {
log.error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ public ConnectorException(String message, int httpCode, String httpMsg) {
this.shouldRetry = true;
}

public ConnectorException(String message, int httpCode, String httpMsg, Exception e) {
super(message, e);
this.httpCode = httpCode;
this.httpReason = httpMsg;
this.shouldRetry = true;
}

public ConnectorException(String message, boolean shouldRetry, Exception e) {
super(message, e);
this.shouldRetry = shouldRetry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public List<FeatureConfig> getFlags() throws ConnectorException {
this.environmentUuid,
this.cluster,
e);
throw new ConnectorException(e.getMessage(), e.getCode(), e.getMessage());
throw new ConnectorException(e.getMessage(), e.getCode(), e.getMessage(), e);
} finally {
MDC.remove(REQUEST_ID_KEY);
}
Expand Down Expand Up @@ -283,7 +283,7 @@ public FeatureConfig getFlag(@NonNull final String identifier) throws ConnectorE
this.environmentUuid,
this.cluster,
e);
throw new ConnectorException(e.getMessage(), e.getCode(), e.getMessage());
throw new ConnectorException(e.getMessage(), e.getCode(), e.getMessage(), e);
} finally {
MDC.remove(REQUEST_ID_KEY);
}
Expand Down Expand Up @@ -314,7 +314,7 @@ public List<Segment> getSegments() throws ConnectorException {
e.getCode(),
e.getMessage(),
e);
throw new ConnectorException(e.getMessage(), e.getCode(), e.getMessage());
throw new ConnectorException(e.getMessage(), e.getCode(), e.getMessage(), e);
} finally {
MDC.remove(REQUEST_ID_KEY);
}
Expand Down Expand Up @@ -345,7 +345,7 @@ public Segment getSegment(@NonNull final String identifier) throws ConnectorExce
this.environmentUuid,
this.cluster,
e);
throw new ConnectorException(e.getMessage(), e.getCode(), e.getMessage());
throw new ConnectorException(e.getMessage(), e.getCode(), e.getMessage(), e);
} finally {
MDC.remove(REQUEST_ID_KEY);
}
Expand All @@ -369,7 +369,7 @@ public void postMetrics(@NonNull final Metrics metrics) throws ConnectorExceptio
this.environmentUuid,
this.cluster,
e);
throw new ConnectorException(e.getMessage(), e.getCode(), e.getMessage());
throw new ConnectorException(e.getMessage(), e.getCode(), e.getMessage(), e);
} finally {
MDC.remove(REQUEST_ID_KEY);
}
Expand Down
43 changes: 43 additions & 0 deletions src/test/java/io/harness/cf/client/api/UpdateProcessorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.harness.cf.client.api;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import io.harness.cf.client.connector.Connector;
import io.harness.cf.client.connector.ConnectorException;
import io.harness.cf.client.connector.Updater;
import io.harness.cf.client.dto.Message;
import org.junit.jupiter.api.Test;

public class UpdateProcessorTest {

@Test
public void shouldNotCallOutToServerOnDeleteTargetSegmentEvent() throws ConnectorException {
final Connector mockConnector = mock(Connector.class);
final Repository mockRepo = mock(Repository.class);
final Updater mockUpdater = mock(Updater.class);

final UpdateProcessor processor = new UpdateProcessor(mockConnector, mockRepo, mockUpdater);
final Message message = new Message("delete", "target-segment", "test", 1);
processor.processSegment(message).run();

verify(mockConnector, times(0)).getSegment(anyString());
verify(mockRepo, times(1)).deleteSegment(anyString());
}

@Test
public void shouldNotCallOutToServerOnDeleteFlagEvent() throws ConnectorException {
final Connector mockConnector = mock(Connector.class);
final Repository mockRepo = mock(Repository.class);
final Updater mockUpdater = mock(Updater.class);

final UpdateProcessor processor = new UpdateProcessor(mockConnector, mockRepo, mockUpdater);
final Message message = new Message("delete", "flag", "test", 1);
processor.processFlag(message).run();

verify(mockConnector, times(0)).getFlag(anyString());
verify(mockRepo, times(1)).deleteFlag(anyString());
}
}