Skip to content
Open
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
30 changes: 30 additions & 0 deletions src/test/java/pwa/BotServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package pwa;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.benmanes.caffeine.cache.Cache;
import com.uci.utils.BotService;
import com.uci.utils.dto.BotServiceParams;
import io.fusionauth.client.FusionAuthClient;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

public class BotServiceTest extends BotService {

public BotServiceTest(WebClient webClient, FusionAuthClient fusionAuthClient, Cache<Object, Object> cache, BotServiceParams botServiceParams) {
super(webClient, fusionAuthClient, cache, botServiceParams);
}

public Mono<JsonNode> getBotNodeFromId(String botId) {
String cacheKey = "bot-node-by-id:" + botId;
ObjectMapper mapper = new ObjectMapper();
JsonNode root = null;
try {
root = mapper.readTree("{\"apiId\":\"api.bot.5768d2b7-15c5-4ab2-bab1-e7ba2c638ec2\",\"path\":\"/admin/bot/5768d2b7-15c5-4ab2-bab1-e7ba2c638ec2\",\"apiVersion\":\"v1\",\"msgid\":\"90216f57-700c-4c45-82ef-fb0d2ef532aa\",\"result\":{\"id\":\"5768d2b7-15c5-4ab2-bab1-e7ba2c638ec2\",\"createdAt\":\"2023-04-28T10:53:41.394Z\",\"updatedAt\":\"2023-04-28T10:53:41.395Z\",\"name\":\"UCI Demo\",\"startingMessage\":\"Hi UCI\",\"ownerID\":\"8f7ee860-0163-4229-9d2a-01cef53145ba\",\"ownerOrgID\":\"org01\",\"purpose\":\"For Internal Demo\",\"description\":\"For Internal Demo\",\"startDate\":\"2023-04-27T00:00:00.000Z\",\"endDate\":\"2025-12-01T00:00:00.000Z\",\"status\":\"ENABLED\",\"tags\":[],\"botImage\":null,\"users\":[],\"logicIDs\":[{\"id\":\"44fefff9-8719-4055-997e-be1225cf4731\",\"name\":\"UCI Demo\",\"createdAt\":\"2023-04-28T10:51:34.518Z\",\"updatedAt\":\"2023-04-28T10:51:34.518Z\",\"description\":null,\"adapterId\":\"44a9df72-3d7a-4ece-94c5-98cf26307324\",\"transformers\":[{\"id\":\"9c46aa04-b72e-423a-aab7-a1a05842d99b\",\"createdAt\":\"2023-04-28T10:51:34.501Z\",\"updatedAt\":\"2023-04-28T10:51:34.520Z\",\"meta\":{\"form\":\"https://hosted.my.form.here.com\",\"formID\":\"UCI-demo-1\"},\"transformerId\":\"bbf56981-b8c9-40e9-8067-468c2c753659\",\"conversationLogicId\":\"44fefff9-8719-4055-997e-be1225cf4731\"}],\"adapter\":{\"id\":\"44a9df72-3d7a-4ece-94c5-98cf26307324\",\"createdAt\":\"2023-04-28T06:02:41.824Z\",\"updatedAt\":\"2023-04-28T06:02:41.824Z\",\"channel\":\"WhatsApp\",\"provider\":\"gupshup\",\"config\":{\"2WAY\":\"2000193034\",\"phone\":\"9999999999\",\"HSM_ID\":\"2000193032\",\"credentials\":{\"vault\":\"samagra\",\"variable\":\"gupshupSamagraProd\"}},\"name\":\"SamagraProd\"}}]},\"startTime\":\"2023-09-28T13:01:52.055Z\",\"method\":\"GET\",\"endTime\":\"2023-09-28T13:01:52.080Z\"}");
return Mono.just(root.path("result"));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
57 changes: 57 additions & 0 deletions src/test/java/pwa/PwaWebControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package pwa;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.uci.adapter.cdn.FileCdnFactory;
import com.uci.adapter.pwa.PwaWebPortalAdapter;
import com.uci.adapter.pwa.web.inbound.PwaWebMessage;
import com.uci.dao.repository.XMessageRepository;
import com.uci.inbound.pwa.web.PwaWebController;
import com.uci.utils.cache.service.RedisCacheService;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;

import javax.xml.bind.JAXBException;

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

@SpringBootTest(classes = {PwaWebController.class, UtilsTestConfig.class})
//@AutoConfigureMockMvc
public class PwaWebControllerTest {
private PwaWebMessage pwaWebMessage;
@Autowired
private PwaWebController pwaWebController;
@MockBean
public XMessageRepository xMessageRepository;
@MockBean
public RedisCacheService redisCacheService;
@MockBean
public FileCdnFactory fileCdnFactory;
@MockBean
public PwaWebPortalAdapter pwaWebPortalAdapter;



@BeforeEach
public void setup() {
MockitoAnnotations.initMocks(this);
pwaWebMessage = new PwaWebMessage();
pwaWebMessage.setMessageId("f3af59b6-57c3-4238-93d4-06bad3d79398");
pwaWebMessage.setText("Hi UCI");
pwaWebMessage.setUserId("67630c26-e29e-46ae-acb6-756704362d84");
pwaWebMessage.setAppId("chatbot");
pwaWebMessage.setFrom("ucipwa:9999999999");
pwaWebMessage.setTo("5768d2b7-15c5-4ab2-bab1-e7ba2c638ec2");
}

@Test
public void testPwaWebController() throws JAXBException, JsonProcessingException {
pwaWebController.dikshaWeb(pwaWebMessage);
assertNotNull(SimpleProducerTest.kafkaDataMap.get("${KAFKA_INBOUND_PROCESSED_TOPIC}"));
assertEquals(SimpleProducerTest.kafkaData, SimpleProducerTest.kafkaDataMap.get("${KAFKA_INBOUND_PROCESSED_TOPIC}"));
}
}
53 changes: 53 additions & 0 deletions src/test/java/pwa/SimpleProducerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package pwa;

import com.uci.utils.kafka.SimpleProducer;
import org.springframework.kafka.core.KafkaTemplate;

import java.util.HashMap;
import java.util.Map;

public class SimpleProducerTest extends SimpleProducer {
public static String kafkaData = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
"<xMessage>\n" +
" <adapterId>44a9df72-3d7a-4ece-94c5-98cf26307324</adapterId>\n" +
" <app>UCI Demo</app>\n" +
" <botId>5768d2b7-15c5-4ab2-bab1-e7ba2c638ec2</botId>\n" +
" <channel>web</channel>\n" +
" <channelURI>web</channelURI>\n" +
" <from>\n" +
" <bot>false</bot>\n" +
" <broadcast>false</broadcast>\n" +
" <deviceType>PHONE_PWA</deviceType>\n" +
" <userID>ucipwa:9999999999</userID>\n" +
" </from>\n" +
" <messageId>\n" +
" <channelMessageId>f3af59b6-57c3-4238-93d4-06bad3d79398</channelMessageId>\n" +
" <replyId>ucipwa:9999999999</replyId>\n" +
" </messageId>\n" +
" <messageState>REPLIED</messageState>\n" +
" <messageType>TEXT</messageType>\n" +
" <ownerId>8f7ee860-0163-4229-9d2a-01cef53145ba</ownerId>\n" +
" <ownerOrgId>org01</ownerOrgId>\n" +
" <payload>\n" +
" <text>Hi UCI</text>\n" +
" </payload>\n" +
" <provider>pwa</provider>\n" +
" <providerURI>pwa</providerURI>\n" +
" <sessionId>eb566186-b2d1-4c9d-92b1-8d845f1708ab</sessionId>\n" +
" <timestamp>1695981278680</timestamp>\n" +
" <to>\n" +
" <bot>false</bot>\n" +
" <broadcast>false</broadcast>\n" +
" <userID>admin</userID>\n" +
" </to>\n" +
"</xMessage>";
public static Map<String, String> kafkaDataMap = new HashMap<>();

public SimpleProducerTest(KafkaTemplate<String, String> simpleProducer1) {
super(simpleProducer1);
}

public void send(String topic, String message) {
kafkaDataMap.put("inbound-processed", kafkaData);
}
}
83 changes: 83 additions & 0 deletions src/test/java/pwa/UtilsTestConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package pwa;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.uci.utils.BotService;
import com.uci.utils.UtilHealthService;
import com.uci.utils.dto.BotServiceParams;
import com.uci.utils.kafka.KafkaConfig;
import com.uci.utils.service.UserService;
import io.fusionauth.client.FusionAuthClient;
import org.apache.kafka.clients.admin.AdminClient;
import org.mockito.Mock;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.reactive.function.client.WebClient;


public class UtilsTestConfig {

@Mock
WebClient webClient;

@MockBean
FusionAuthClient fusionAuthClient;

@MockBean
BotServiceParams botServiceParams;

@MockBean
AdminClient adminClient;
@MockBean
Cache<Object, Object> cache;
@Mock
KafkaTemplate<String, String> simpleProducer;

@Bean
public UserService getUserService() {
return new UserService();
}

@Bean
public UtilHealthService getUtilHealthService() {
return new UtilHealthService();
}

@Bean
public KafkaConfig getKafkaConfig() {
return new KafkaConfig();
}

@Bean
public BotService getBotService() {
return new BotServiceTest(webClient, fusionAuthClient, cache, botServiceParams);
}

@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper();
}

// @Bean
// public Cache<Object, Object> caffeineCacheBuilder() {
// return Caffeine.newBuilder()
// .recordStats()
// .build();
// }
public Caffeine<Object, Object> caffeineCacheBuilder() {
return Caffeine.newBuilder()
.recordStats();
}

@Bean
public Cache<Object, Object> cache() {
return caffeineCacheBuilder().build();
}

@Bean
public SimpleProducerTest simpleProducer() {
return new SimpleProducerTest(simpleProducer);
}
}
8 changes: 8 additions & 0 deletions src/test/java/pwa/XMessageRepositoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pwa;

import com.uci.dao.repository.XMessageRepository;


public interface XMessageRepositoryTest extends XMessageRepository {

}