Search in sources :

Example 1 with GenericGatewayParameter

use of org.hisp.dhis.sms.config.GenericGatewayParameter in project dhis2-core by dhis2.

the class GenericSmsGatewayTest method setUp.

@BeforeEach
public void setUp() {
    subject = new SimplisticHttpGetGateWay(restTemplate, pbeStringEncryptor);
    gatewayConfig = new GenericHttpGatewayConfig();
    gatewayConfig.setUseGet(false);
    gatewayConfig.setName("generic");
    gatewayConfig.setUrlTemplate(GATEWAY_URL);
    gatewayConfig.setDefault(true);
    gatewayConfig.setUid(UID);
    username = new GenericGatewayParameter();
    username.setKey("user");
    username.setValue("user_uio");
    username.setEncode(false);
    username.setHeader(true);
    username.setConfidential(false);
    password = new GenericGatewayParameter();
    password.setKey("password");
    password.setValue("abc123");
    password.setEncode(false);
    password.setHeader(true);
    password.setConfidential(true);
    valueStore.put(SmsGateway.KEY_TEXT, SmsUtils.encode(TEXT));
    valueStore.put(SmsGateway.KEY_RECIPIENT, StringUtils.join(RECIPIENTS, ","));
}
Also used : GenericGatewayParameter(org.hisp.dhis.sms.config.GenericGatewayParameter) SimplisticHttpGetGateWay(org.hisp.dhis.sms.config.SimplisticHttpGetGateWay) GenericHttpGatewayConfig(org.hisp.dhis.sms.config.GenericHttpGatewayConfig) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with GenericGatewayParameter

use of org.hisp.dhis.sms.config.GenericGatewayParameter in project dhis2-core by dhis2.

the class GenericSmsGatewayTest method testSendSms_Json.

@Test
void testSendSms_Json() {
    strSubstitutor = new StringSubstitutor(valueStore);
    body = strSubstitutor.replace(CONFIG_TEMPLATE_JSON);
    gatewayConfig.getParameters().clear();
    gatewayConfig.setParameters(Arrays.asList(username, password));
    gatewayConfig.setContentType(ContentType.APPLICATION_JSON);
    gatewayConfig.setConfigurationTemplate(CONFIG_TEMPLATE_JSON);
    ResponseEntity<String> responseEntity = new ResponseEntity<>("success", HttpStatus.OK);
    when(restTemplate.exchange(any(URI.class), any(HttpMethod.class), any(HttpEntity.class), eq(String.class))).thenReturn(responseEntity);
    assertThat(subject.send(SUBJECT, TEXT, RECIPIENTS, gatewayConfig).isOk(), is(true));
    verify(restTemplate).exchange(any(URI.class), httpMethodArgumentCaptor.capture(), httpEntityArgumentCaptor.capture(), eq(String.class));
    assertNotNull(httpEntityArgumentCaptor.getValue());
    assertNotNull(httpMethodArgumentCaptor.getValue());
    HttpMethod httpMethod = httpMethodArgumentCaptor.getValue();
    assertEquals(HttpMethod.POST, httpMethod);
    HttpEntity<String> requestEntity = httpEntityArgumentCaptor.getValue();
    assertEquals(body, requestEntity.getBody());
    HttpHeaders httpHeaders = requestEntity.getHeaders();
    assertTrue(httpHeaders.get("Content-type").contains(gatewayConfig.getContentType().getValue()));
    List<GenericGatewayParameter> parameters = gatewayConfig.getParameters();
    parameters.stream().filter(p -> p.isEncode() && p.isConfidential() && p.isHeader()).forEach(p -> {
        assertTrue(httpHeaders.containsKey(p.getKey()));
        assertEquals(" Basic ZGVjcnlwdGVkVGV4dA==", httpHeaders.get(p.getKey()).get(0));
    });
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) SmsGateway(org.hisp.dhis.sms.config.SmsGateway) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Mock(org.mockito.Mock) HashMap(java.util.HashMap) GenericGatewayParameter(org.hisp.dhis.sms.config.GenericGatewayParameter) StringUtils(org.apache.commons.lang3.StringUtils) Captor(org.mockito.Captor) StringSubstitutor(org.apache.commons.text.StringSubstitutor) GenericHttpGatewayConfig(org.hisp.dhis.sms.config.GenericHttpGatewayConfig) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) PBEStringEncryptor(org.jasypt.encryption.pbe.PBEStringEncryptor) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) URI(java.net.URI) SmsUtils(org.hisp.dhis.system.util.SmsUtils) RestTemplate(org.springframework.web.client.RestTemplate) SimplisticHttpGetGateWay(org.hisp.dhis.sms.config.SimplisticHttpGetGateWay) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) HttpHeaders(org.springframework.http.HttpHeaders) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) Mockito.when(org.mockito.Mockito.when) Sets(com.google.common.collect.Sets) Mockito.verify(org.mockito.Mockito.verify) ContentType(org.hisp.dhis.sms.config.ContentType) Test(org.junit.jupiter.api.Test) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ResponseEntity(org.springframework.http.ResponseEntity) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpEntity(org.springframework.http.HttpEntity) GenericGatewayParameter(org.hisp.dhis.sms.config.GenericGatewayParameter) StringSubstitutor(org.apache.commons.text.StringSubstitutor) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) URI(java.net.URI) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.jupiter.api.Test)

Aggregations

GenericGatewayParameter (org.hisp.dhis.sms.config.GenericGatewayParameter)2 GenericHttpGatewayConfig (org.hisp.dhis.sms.config.GenericHttpGatewayConfig)2 SimplisticHttpGetGateWay (org.hisp.dhis.sms.config.SimplisticHttpGetGateWay)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Sets (com.google.common.collect.Sets)1 URI (java.net.URI)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 StringUtils (org.apache.commons.lang3.StringUtils)1 StringSubstitutor (org.apache.commons.text.StringSubstitutor)1 CoreMatchers.is (org.hamcrest.CoreMatchers.is)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 ContentType (org.hisp.dhis.sms.config.ContentType)1 SmsGateway (org.hisp.dhis.sms.config.SmsGateway)1 SmsUtils (org.hisp.dhis.system.util.SmsUtils)1 PBEStringEncryptor (org.jasypt.encryption.pbe.PBEStringEncryptor)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1