Skip to content

Commit

Permalink
Removes manual instance creation of Whatsapp netcore adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmoy12c committed May 1, 2023
1 parent 57d2c28 commit a25dca4
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class AppConfiguration1 {


@Bean
@Qualifier("rest")
@Qualifier("rest")
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import reactor.core.publisher.Mono;

Expand All @@ -51,23 +52,25 @@
@Slf4j
@Getter
@Setter
@Builder
@Component
public class NetcoreWhatsappAdapter extends AbstractProvider implements IProvider {

private final static String GUPSHUP_OUTBOUND = "https://media.smsgupshup.com/GatewayAPI/rest";

@Autowired
@Qualifier("rest")
@Qualifier("rest")
private RestTemplate restTemplate;

private BotService botservice;

@Autowired
private MediaSizeLimit mediaSizeLimit;

@Autowired
private FileCdnProvider fileCdnProvider;

@Autowired
private NewNetcoreService newNetcoreService;

/**
* Convert Inbound Netcore Message To XMessage
*/
Expand Down Expand Up @@ -263,7 +266,7 @@ private Map<String, Object> uploadInboundMediaFile(String messageId, String id,
if(!id.isEmpty() && !mime_type.isEmpty()) {
try {
log.info("Get netcore media by id:" + id);
byte[] inputBytes = NewNetcoreService.getInstance().
byte[] inputBytes = newNetcoreService.
getMediaFile(id).readAllBytes();

if (inputBytes != null) {
Expand Down Expand Up @@ -408,7 +411,7 @@ public Mono<XMessage> processOutBoundMessageF(XMessage xMsg) {
String phoneNo = "91" +xMsg.getTo().getUserID();
SingleMessage message = getOutboundSingleMessage(xMsg, phoneNo);

return NewNetcoreService.getInstance().
return newNetcoreService.
sendOutboundMessage(OutboundMessage.builder().message(new SingleMessage[]{message}).build()).map(new Function<SendMessageResponse, XMessage>() {
@Override
public XMessage apply(SendMessageResponse sendMessageResponse) {
Expand Down Expand Up @@ -618,7 +621,7 @@ private void optOutUser(String phoneNo) {
* @param message
*/
private void optInOutUser(String type, SingleOptInOutMessage message) {
NewNetcoreService.getInstance().
newNetcoreService.
sendOutboundOptInOutMessage(OutboundOptInOutMessage.builder().type(type).recipients(new SingleOptInOutMessage[]{message}).build()).map(new Function<SendMessageResponse, Boolean>() {
@Override
public Boolean apply(SendMessageResponse sendMessageResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ public NewNetcoreService() {
.build();
}

public static NewNetcoreService getInstance() {
return Objects.requireNonNullElseGet(newNetcoreService, NewNetcoreService::new);
}

public ManageUserResponse manageUser(ManageUserRequestMessage message) {
ObjectMapper mapper = new ObjectMapper();
RequestBody body = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.uci.adapter.provider.factory;

import com.uci.adapter.cdn.FileCdnFactory;
import com.uci.adapter.cdn.service.AzureBlobService;
import com.uci.adapter.cdn.service.MinioClientService;
import com.uci.adapter.firebase.web.FirebaseNotificationAdapter;
import com.uci.adapter.gs.whatsapp.GupShupWhatsappAdapter;
import com.uci.adapter.netcore.whatsapp.NetcoreWhatsappAdapter;
import com.uci.adapter.pwa.PwaWebPortalAdapter;
import com.uci.adapter.cdn.service.SunbirdCloudMediaService;
import com.uci.adapter.sunbird.web.SunbirdWebPortalAdapter;
import com.uci.adapter.utils.CommonUtils;
import com.uci.dao.repository.XMessageRepository;
Expand Down Expand Up @@ -41,6 +38,9 @@ public class ProviderFactory {
@Autowired
public CommonUtils commonUtils;

@Autowired
private NetcoreWhatsappAdapter netcoreWhatsappAdapter;

@Value("${fcm.notificationKeyEnable:#{'true'}}")
private String notificationKeyEnable;

Expand All @@ -67,11 +67,8 @@ public IProvider getProvider(String provider,String channel) {
.build();
return pwaAdapter;
} else if(provider.equalsIgnoreCase("Netcore") && channel.toLowerCase().equalsIgnoreCase("whatsapp")){
NetcoreWhatsappAdapter netcoreWhatsappAdapter = NetcoreWhatsappAdapter
.builder()
.botservice(botService)
.fileCdnProvider(fileCdnFactory.getFileCdnProvider())
.build();
netcoreWhatsappAdapter.setBotservice(botService);
netcoreWhatsappAdapter.setFileCdnProvider(fileCdnFactory.getFileCdnProvider());
return netcoreWhatsappAdapter;
} else if(provider.toLowerCase().equals("firebase") && channel.toLowerCase().equals("web")){
return FirebaseNotificationAdapter.builder().botService(botService).notificationKeyEnable(notificationKeyEnable).build();
Expand Down
7 changes: 3 additions & 4 deletions src/test/java/com/uci/adapter/AdapterTestConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

import com.github.benmanes.caffeine.cache.Cache;
import com.uci.adapter.netcore.whatsapp.NewNetcoreService;
import com.uci.utils.BotService;
import io.fusionauth.client.FusionAuthClient;
import okhttp3.OkHttpClient;
import org.mockito.Mock;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.function.client.WebClient;

import com.uci.utils.BotService;

import io.fusionauth.client.FusionAuthClient;

public class AdapterTestConfiguration {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,22 @@
import lombok.extern.slf4j.Slf4j;
import messagerosa.core.model.SenderReceiverInfo;
import messagerosa.core.model.XMessage;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.notNull;

@Slf4j
//@ExtendWith(MockitoExtension.class)
@SpringBootTest(classes=AdapterTestConfiguration.class)
class NetcoreWhatsappAdapterTest {

NetcoreWhatsappAdapter adapter;
ObjectMapper objectMapper;

Expand All @@ -39,10 +37,8 @@ class NetcoreWhatsappAdapterTest {
public void init() {

objectMapper = new ObjectMapper();
adapter = NetcoreWhatsappAdapter
.builder()
.botservice(botService)
.build();
adapter = new NetcoreWhatsappAdapter();
adapter.setBotservice(botService);
}

@Test
Expand Down

0 comments on commit a25dca4

Please sign in to comment.