Search in sources :

Example 1 with Consumer

use of com.ctrip.framework.apollo.openapi.entity.Consumer in project apollo by ctripcorp.

the class ConsumerService method createConsumer.

public Consumer createConsumer(Consumer consumer) {
    String appId = consumer.getAppId();
    Consumer managedConsumer = consumerRepository.findByAppId(appId);
    if (managedConsumer != null) {
        throw new BadRequestException("Consumer already exist");
    }
    String ownerName = consumer.getOwnerName();
    UserInfo owner = userService.findByUserId(ownerName);
    if (owner == null) {
        throw new BadRequestException(String.format("User does not exist. UserId = %s", ownerName));
    }
    consumer.setOwnerEmail(owner.getEmail());
    String operator = userInfoHolder.getUser().getUserId();
    consumer.setDataChangeCreatedBy(operator);
    consumer.setDataChangeLastModifiedBy(operator);
    return consumerRepository.save(consumer);
}
Also used : Consumer(com.ctrip.framework.apollo.openapi.entity.Consumer) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) UserInfo(com.ctrip.framework.apollo.portal.entity.bo.UserInfo)

Example 2 with Consumer

use of com.ctrip.framework.apollo.openapi.entity.Consumer in project apollo by ctripcorp.

the class ConsumerServiceTest method testGetConsumerByConsumerId.

@Test
public void testGetConsumerByConsumerId() throws Exception {
    long someConsumerId = 1;
    Consumer someConsumer = mock(Consumer.class);
    when(consumerRepository.findOne(someConsumerId)).thenReturn(someConsumer);
    assertEquals(someConsumer, consumerService.getConsumerByConsumerId(someConsumerId));
    verify(consumerRepository, times(1)).findOne(someConsumerId);
}
Also used : Consumer(com.ctrip.framework.apollo.openapi.entity.Consumer) Test(org.junit.Test) AbstractUnitTest(com.ctrip.framework.apollo.portal.AbstractUnitTest)

Example 3 with Consumer

use of com.ctrip.framework.apollo.openapi.entity.Consumer in project apollo by ctripcorp.

the class ConsumerServiceTest method testCreateConsumer.

@Test
public void testCreateConsumer() {
    Consumer consumer = createConsumer(testConsumerName, testAppId, testOwner);
    UserInfo owner = createUser(testOwner);
    when(consumerRepository.findByAppId(testAppId)).thenReturn(null);
    when(userService.findByUserId(testOwner)).thenReturn(owner);
    when(userInfoHolder.getUser()).thenReturn(owner);
    consumerService.createConsumer(consumer);
    verify(consumerRepository).save(consumer);
}
Also used : Consumer(com.ctrip.framework.apollo.openapi.entity.Consumer) UserInfo(com.ctrip.framework.apollo.portal.entity.bo.UserInfo) Test(org.junit.Test) AbstractUnitTest(com.ctrip.framework.apollo.portal.AbstractUnitTest)

Example 4 with Consumer

use of com.ctrip.framework.apollo.openapi.entity.Consumer in project apollo by ctripcorp.

the class ConsumerServiceTest method testGenerateAndEnrichConsumerToken.

@Test
public void testGenerateAndEnrichConsumerToken() throws Exception {
    String someConsumerAppId = "someAppId";
    long someConsumerId = 1;
    String someToken = "someToken";
    Date generationTime = new Date();
    Consumer consumer = mock(Consumer.class);
    when(consumerRepository.findOne(someConsumerId)).thenReturn(consumer);
    when(consumer.getAppId()).thenReturn(someConsumerAppId);
    when(consumerService.generateToken(someConsumerAppId, generationTime, someTokenSalt)).thenReturn(someToken);
    ConsumerToken consumerToken = new ConsumerToken();
    consumerToken.setConsumerId(someConsumerId);
    consumerToken.setDataChangeCreatedTime(generationTime);
    consumerService.generateAndEnrichToken(consumer, consumerToken);
    assertEquals(someToken, consumerToken.getToken());
}
Also used : Consumer(com.ctrip.framework.apollo.openapi.entity.Consumer) Matchers.anyString(org.mockito.Matchers.anyString) Date(java.util.Date) ConsumerToken(com.ctrip.framework.apollo.openapi.entity.ConsumerToken) Test(org.junit.Test) AbstractUnitTest(com.ctrip.framework.apollo.portal.AbstractUnitTest)

Example 5 with Consumer

use of com.ctrip.framework.apollo.openapi.entity.Consumer in project apollo by ctripcorp.

the class ConsumerServiceTest method createConsumer.

private Consumer createConsumer(String name, String appId, String ownerName) {
    Consumer consumer = new Consumer();
    consumer.setName(name);
    consumer.setAppId(appId);
    consumer.setOwnerName(ownerName);
    return consumer;
}
Also used : Consumer(com.ctrip.framework.apollo.openapi.entity.Consumer)

Aggregations

Consumer (com.ctrip.framework.apollo.openapi.entity.Consumer)5 AbstractUnitTest (com.ctrip.framework.apollo.portal.AbstractUnitTest)3 Test (org.junit.Test)3 UserInfo (com.ctrip.framework.apollo.portal.entity.bo.UserInfo)2 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)1 ConsumerToken (com.ctrip.framework.apollo.openapi.entity.ConsumerToken)1 Date (java.util.Date)1 Matchers.anyString (org.mockito.Matchers.anyString)1