-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [CO-1357] emit user status changed event on message broker (#558)
- Publish UserStatusChanged message using message-broker-sdk on account status changed callback - Add UT Refs: CO-1357
- Loading branch information
1 parent
4f36ac5
commit 75b9b53
Showing
8 changed files
with
244 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,9 @@ | |
{ | ||
"carbonio-mailbox/": { | ||
"policy": "read" | ||
}, | ||
"carbonio-message-broker/": { | ||
"policy": "read" | ||
} | ||
} | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
store/src/main/java/com/zextras/mailbox/client/ServiceDiscoverHttpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com> | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
package com.zextras.mailbox.client; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.zextras.carbonio.files.exceptions.InternalServerError; | ||
import com.zextras.carbonio.files.exceptions.UnAuthorized; | ||
import io.vavr.control.Try; | ||
import org.apache.commons.codec.binary.Base64; | ||
import org.apache.commons.io.IOUtils; | ||
import org.apache.http.HttpStatus; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class ServiceDiscoverHttpClient { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(ServiceDiscoverHttpClient.class); | ||
|
||
private final String serviceDiscoverURL; | ||
private String token; | ||
|
||
ServiceDiscoverHttpClient(String serviceDiscoverURL) { | ||
this.serviceDiscoverURL = serviceDiscoverURL; | ||
this.token = System.getenv("CONSUL_HTTP_TOKEN"); // default: get from env | ||
} | ||
|
||
public static ServiceDiscoverHttpClient atURL( | ||
String url, | ||
String serviceName | ||
) { | ||
return new ServiceDiscoverHttpClient(url + "/v1/kv/" + serviceName + "/"); | ||
} | ||
|
||
public static ServiceDiscoverHttpClient defaultURL(String serviceName) { | ||
return new ServiceDiscoverHttpClient("http://localhost:8500/v1/kv/" + serviceName + "/"); | ||
} | ||
|
||
public ServiceDiscoverHttpClient withToken(String token) { | ||
this.token = token; | ||
return this; | ||
} | ||
|
||
public Try<String> getConfig(String configKey) { | ||
try (CloseableHttpClient httpClient = HttpClients.createMinimal()) { | ||
HttpGet request = new HttpGet(serviceDiscoverURL + configKey); | ||
request.setHeader("X-Consul-Token", token); | ||
try(CloseableHttpResponse response = httpClient.execute(request)) { | ||
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { | ||
String bodyResponse = IOUtils.toString( | ||
response.getEntity().getContent(), | ||
StandardCharsets.UTF_8 | ||
); | ||
|
||
String value = new ObjectMapper().readTree(bodyResponse).get(0).get("Value").asText(); | ||
String valueDecoded = new String(Base64.decodeBase64(value), StandardCharsets.UTF_8).trim(); | ||
|
||
return Try.success(valueDecoded); | ||
} | ||
|
||
logger.error("Service discover didn't respond with 200 when requesting a config (received {})", | ||
response.getStatusLine().getStatusCode()); | ||
return Try.failure(new UnAuthorized()); | ||
} | ||
} catch (IOException exception) { | ||
logger.error("Exception trying to get config from service discover: ", exception); | ||
return Try.failure(new InternalServerError(exception)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
store/src/test/java/com/zextras/mailbox/callbacks/AccountStatusChangedCallbackTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com> | ||
// | ||
// SPDX-License-Identifier: GPL-2.0-only | ||
|
||
package com.zextras.mailbox.callbacks; | ||
|
||
import com.zextras.carbonio.message_broker.MessageBrokerClient; | ||
import com.zextras.carbonio.message_broker.config.enums.Service; | ||
import com.zextras.carbonio.message_broker.events.services.mailbox.UserStatusChanged; | ||
import com.zextras.mailbox.client.ServiceDiscoverHttpClient; | ||
import com.zextras.mailbox.util.MailboxTestUtil; | ||
import com.zimbra.cs.account.Account; | ||
import com.zimbra.cs.account.Provisioning; | ||
import com.zimbra.cs.account.callback.AccountStatus; | ||
import com.zimbra.cs.account.callback.CallbackContext; | ||
import io.vavr.control.Try; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.Mockito; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
|
||
class AccountStatusChangedCallbackTest { | ||
|
||
AccountStatus accountStatus; | ||
|
||
@BeforeEach | ||
void setUp() throws Exception { | ||
MailboxTestUtil.setUp(); | ||
accountStatus = new AccountStatus(); | ||
} | ||
|
||
/** | ||
* A lot of calls are mocked since they are external calls to service discover or message broker, this | ||
* just tests that if calls are successful no other exceptions are thrown. | ||
*/ | ||
@Test | ||
void shouldNotFailWhenExecutingUserStatusChangedCallback(){ | ||
CallbackContext context = Mockito.mock(CallbackContext.class); | ||
String attrName = "fake"; | ||
Account entry = Mockito.mock(Account.class); | ||
|
||
ServiceDiscoverHttpClient serviceDiscoverHttpClient = Mockito.mock(ServiceDiscoverHttpClient.class); | ||
MessageBrokerClient messageBrokerClient = Mockito.mock(MessageBrokerClient.class); | ||
|
||
Mockito.when(context.isDoneAndSetIfNot(AccountStatus.class)).thenReturn(false); | ||
Mockito.when(context.isCreate()).thenReturn(false); | ||
Mockito.when(entry.getAccountStatus(any(Provisioning.class))).thenReturn("active"); | ||
Mockito.when(entry.getId()).thenReturn("fake-account-id"); | ||
|
||
try(MockedStatic<Files> mockedFiles = Mockito.mockStatic(Files.class); | ||
MockedStatic<ServiceDiscoverHttpClient> mockedServiceDiscoverStatic = Mockito.mockStatic(ServiceDiscoverHttpClient.class); | ||
MockedStatic<MessageBrokerClient> mockedMessageBrokerClientStatic = Mockito.mockStatic(MessageBrokerClient.class)) { | ||
|
||
mockedFiles.when(() -> Files.readString(any(Path.class))).thenReturn("fake-token"); | ||
mockedServiceDiscoverStatic.when(() -> ServiceDiscoverHttpClient.defaultURL("carbonio-message-broker")) | ||
.thenReturn(serviceDiscoverHttpClient); | ||
Mockito.when(serviceDiscoverHttpClient.withToken("fake-token")).thenReturn(serviceDiscoverHttpClient); | ||
|
||
Mockito.when(serviceDiscoverHttpClient.getConfig("default/username")).thenReturn(Try.success("fake-username")); | ||
Mockito.when(serviceDiscoverHttpClient.getConfig("default/password")).thenReturn(Try.success("fake-password")); | ||
|
||
mockedMessageBrokerClientStatic.when(() -> MessageBrokerClient.fromConfig( | ||
"127.78.0.7", | ||
20005, | ||
"fake-username", | ||
"fake-password" | ||
)).thenReturn(messageBrokerClient); | ||
|
||
Mockito.when(messageBrokerClient.withCurrentService(Service.MAILBOX)).thenReturn(messageBrokerClient); | ||
Mockito.when(messageBrokerClient.publish(any(UserStatusChanged.class))).thenReturn(true); | ||
|
||
accountStatus.postModify(context, attrName, entry); | ||
|
||
assertTrue(true); | ||
}catch(Exception e) { | ||
e.printStackTrace(); | ||
fail(); | ||
} | ||
} | ||
} |