Skip to content

Commit

Permalink
Merge pull request #29 from chinmoy12c/hotfix/fix_failing_tests
Browse files Browse the repository at this point in the history
Fixed failing tests in NetcoreServiceTest and SunbirdWebPortalAdapterTest
  • Loading branch information
pankajjangid05 authored Apr 28, 2023
2 parents f06e140 + a7f458b commit 57d2c28
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.uci.utils.BotService;
import io.fusionauth.client.FusionAuthClient;

import okhttp3.OkHttpClient;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
Expand Down Expand Up @@ -86,4 +87,9 @@ public BotService getBotService() {

return new BotService(webClient, getFAClient(), cache);
}

@Bean
public OkHttpClient getOkHttpClient() {
return new OkHttpClient().newBuilder().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,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(new NWCredentials(System.getenv("NETCORE_WHATSAPP_AUTH_TOKEN"))).
byte[] inputBytes = NewNetcoreService.getInstance().
getMediaFile(id).readAllBytes();

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

return NewNetcoreService.getInstance(new NWCredentials(System.getenv("NETCORE_WHATSAPP_AUTH_TOKEN"))).
return NewNetcoreService.getInstance().
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 +618,7 @@ private void optOutUser(String phoneNo) {
* @param message
*/
private void optInOutUser(String type, SingleOptInOutMessage message) {
NewNetcoreService.getInstance(new NWCredentials(System.getenv("NETCORE_WHATSAPP_AUTH_TOKEN"))).
NewNetcoreService.getInstance().
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 @@ -11,44 +11,46 @@
import com.uci.adapter.netcore.whatsapp.outbound.OutboundOptInOutMessage;
import com.uci.adapter.netcore.whatsapp.outbound.SendMessageResponse;
import okhttp3.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;

@Service
public class NewNetcoreService {

private final WebClient webClient;

@Autowired
private OkHttpClient client;
private MediaType mediaType;
private String baseURL;
private NWCredentials credentials;

private static NewNetcoreService newNetcoreService = null;

public NewNetcoreService(NWCredentials credentials) {
this.client = new OkHttpClient().newBuilder().build();
public NewNetcoreService() {
this.mediaType = MediaType.parse("application/json");
String url = System.getenv("NETCORE_WHATSAPP_URI");
url = url != null && !url.isEmpty() ? url : "https://waapi.pepipost.com/api/v2/";
this.baseURL = url;
this.credentials = credentials;
this.credentials = new NWCredentials(System.getenv("NETCORE_WHATSAPP_AUTH_TOKEN"));
webClient = WebClient.builder()
.baseUrl(url)
.defaultHeader("Content-Type", "application/json")
.defaultHeader("Authorization", "Bearer " + credentials.getToken())
.build();
}

public static NewNetcoreService getInstance(NWCredentials credentials) {
if (newNetcoreService == null) {
return new NewNetcoreService(credentials);
} else {
return newNetcoreService;
}
public static NewNetcoreService getInstance() {
return Objects.requireNonNullElseGet(newNetcoreService, NewNetcoreService::new);
}

public ManageUserResponse manageUser(ManageUserRequestMessage message) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.uci.adapter.netcore.whatsapp.outbound;

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonAlias;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -10,11 +12,11 @@
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class SendMessageResponse {
public class SendMessageResponse implements Serializable {

@Getter
@Setter
public class Data {
public class Data implements Serializable {

@JsonAlias({"id"})
private String identifier;
Expand All @@ -27,7 +29,7 @@ public class Data {

@Getter
@Setter
public class Error {
public class Error implements Serializable {

@JsonAlias({"code"})
private String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class SunbirdWebPortalAdapterTest {
@BeforeEach
public void init() {
objectMapper = new ObjectMapper();
simplePayload = "{\"Body\":\"1\",\"userId\":\"2da3ad1ac0422d59ef004fdb173706ed\",\"appId\":\"prod.diksha.portal\",\"channel\":\"ORG_001\",\"From\":\"2da3ad1ac0422d59ef004fdb173706ed\",\"context\":null}";
simplePayload = "{\"body\":\"1\",\"userId\":\"2da3ad1ac0422d59ef004fdb173706ed\",\"appId\":\"prod.diksha.portal\",\"channel\":\"ORG_001\",\"From\":\"2da3ad1ac0422d59ef004fdb173706ed\",\"context\":null}";
adapter = SunbirdWebPortalAdapter.builder()
.build();
}
Expand Down
62 changes: 22 additions & 40 deletions src/test/java/com/uci/adapter/AdapterTestConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.uci.adapter;

import com.github.benmanes.caffeine.cache.Cache;
import com.uci.adapter.netcore.whatsapp.NewNetcoreService;
import okhttp3.OkHttpClient;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
Expand All @@ -11,6 +13,16 @@
import io.fusionauth.client.FusionAuthClient;

public class AdapterTestConfiguration {

@Autowired
private WebClient webClient;

@Autowired
private FusionAuthClient fusionAuthClient;

@Mock
private Cache<Object, Object> cache;

@Bean
public WebClient getWebClient() {
return WebClient.builder().baseUrl("CAMPAIGN_URL").defaultHeader("admin-token", "admin-token").build();
Expand All @@ -21,48 +33,18 @@ public FusionAuthClient getFusionAuthClient() {
return new FusionAuthClient("fa-auth-key", "fa-auth-url");
}

@Autowired
private WebClient webClient;

@Autowired
private FusionAuthClient fusionAuthClient;

@Mock
private Cache cache;

@Bean
public BotService botService() {
return new BotService(webClient, fusionAuthClient, cache);
}

// @Bean
// public CampaignService campaignService() {
// return new CampaignService(getWebClient(), getFusionAuthClient());
// }
//
// @Bean
// Map<String, Object> kafkaProducerConfiguration() {
// Map<String, Object> configuration = new HashMap<>();
// configuration.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, BOOTSTRAP_SERVERS);
// configuration.put(ProducerConfig.CLIENT_ID_CONFIG, "sample-producer");
// configuration.put(ProducerConfig.ACKS_CONFIG, "all");
// configuration.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, org.springframework.kafka.support.serializer.JsonSerializer.class);
// configuration.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, org.springframework.kafka.support.serializer.JsonSerializer.class);
// return configuration;
// }
//
// @Bean
// public ProducerFactory<String, String> producerFactory() {
// return new DefaultKafkaProducerFactory(kafkaProducerConfiguration());
// }
//
// @Bean
// public KafkaTemplate<String, String> kafkaTemplate() {
// return new KafkaTemplate<>(producerFactory());
// }
//
// @Bean
// SimpleProducer kafkaSimpleProducer() {
// return new SimpleProducer(kafkaTemplate());
// }

@Bean
public NewNetcoreService newNetcoreService() {
return new NewNetcoreService();
}

@Bean
public OkHttpClient okHttpClient() {
return new OkHttpClient().newBuilder().build();
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
package com.uci.adapter.netcore.whatsapp;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.Test;
import java.io.IOException;

import com.uci.adapter.netcore.whatsapp.outbound.SingleMessage;
import com.uci.adapter.netcore.whatsapp.outbound.Text;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.uci.adapter.AdapterTestConfiguration;
import com.uci.adapter.netcore.whatsapp.outbound.OutboundMessage;
import com.uci.adapter.netcore.whatsapp.outbound.SendMessageResponse;
import com.uci.adapter.netcore.whatsapp.outbound.SingleMessage;
import com.uci.adapter.netcore.whatsapp.outbound.Text;
import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;

import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringBootTest(classes = AdapterTestConfiguration.class)
public class NetcoreServiceTest {

@Mock
private OkHttpClient client;

@InjectMocks
private NewNetcoreService netcoreService;

@Test
public void sendTextTest() {
// Use your netcore token to send message to netcore
String token = "token";
NWCredentials credentials = NWCredentials.builder().build();
credentials.setToken(token);

NewNetcoreService nns = new NewNetcoreService(credentials);

public void sendTextTest() throws IOException {
Text text = Text.builder().content("Hello").previewURL("false").build();
Text[] texts = {text};

SingleMessage msg = SingleMessage
.builder()
.from("461089f9-1000-4211-b182-c7f0291f3d45")
Expand All @@ -33,17 +48,34 @@ public void sendTextTest() {
.text(texts)
.build();
SingleMessage[] messages = {msg};

OutboundMessage message = OutboundMessage.builder().message(messages).build();

/* Send test message to number from UCI Netcore chatbot */
SendMessageResponse response = nns.sendText(message);

// System.out.println(response);
assertNotNull(response);
assertEquals(response.getStatus(), "success");
assertNotNull(response.getMessage());
assertNotNull(response.getData());
assertNotNull(response.getData().getIdentifier());

String mockResponseBody =
"{"
+ "\"status\":\"success\","
+ "\"message\":\"Message sent successfully\","
+ "\"data\":{"
+ "\"identifier\":\"1234\""
+ "},"
+ "\"error\":null"
+ "}";

try (Response mockResponse = Mockito.mock(Response.class)) {
Call callMock = Mockito.mock(Call.class);
Mockito.when(mockResponse.body()).thenReturn(ResponseBody.create(mockResponseBody.getBytes(), MediaType.parse("application/json")));
Mockito.when(callMock.execute()).thenReturn(mockResponse);
Mockito.when(client.newCall(Mockito.any(Request.class))).thenReturn(callMock);

/* Send test message to number from UCI Netcore chatbot */
SendMessageResponse response = netcoreService.sendText(message);
Mockito.verify(callMock).execute();
assertNotNull(response);
assertEquals(new ObjectMapper().writeValueAsString(response), mockResponseBody);
assertEquals(response.getStatus(), "success");
assertNotNull(response.getMessage());
assertNotNull(response.getData());
assertNotNull(response.getData().getIdentifier());
}
}
}

0 comments on commit 57d2c28

Please sign in to comment.