Search in sources :

Example 6 with Subscriber

use of com.aws.greengrass.config.Subscriber in project aws-greengrass-nucleus by aws-greengrass.

the class CredentialRequestHandlerTest method GIVEN_credential_handler_WHEN_called_handle_THEN_returns_creds.

@Test
@SuppressWarnings("PMD.CloseResource")
void GIVEN_credential_handler_WHEN_called_handle_THEN_returns_creds() throws Exception {
    ArgumentCaptor<String> pathCaptor = ArgumentCaptor.forClass(String.class);
    when(mockCloudHelper.sendHttpRequest(any(), any(), pathCaptor.capture(), any(), any())).thenReturn(CLOUD_RESPONSE);
    when(mockAuthNHandler.doAuthentication(anyString())).thenReturn("ServiceA");
    when(mockAuthZHandler.isAuthorized(any(), any())).thenReturn(true);
    ArgumentCaptor<Subscriber> subscriberArgumentCaptor = ArgumentCaptor.forClass(Subscriber.class);
    when(mockDeviceConfig.getIotRoleAlias().subscribe(subscriberArgumentCaptor.capture())).thenReturn(null);
    CredentialRequestHandler handler = new CredentialRequestHandler(mockCloudHelper, mockConnectionManager, mockAuthNHandler, mockAuthZHandler, mockDeviceConfig);
    handler.setIotCredentialsPath(ROLE_ALIAS);
    when(mockAuthNHandler.doAuthentication(anyString())).thenReturn("ServiceA");
    Headers mockHeaders = mock(Headers.class);
    when(mockHeaders.getFirst(any())).thenReturn(AUTHN_TOKEN);
    when(mockExchange.getResponseBody()).thenReturn(mockStream);
    when(mockExchange.getRequestHeaders()).thenReturn(mockHeaders);
    when(mockExchange.getRequestURI()).thenReturn(TES_URI);
    when(mockExchange.getRequestMethod()).thenReturn(REQUEST_METHOD);
    handler.handle(mockExchange);
    int expectedStatus = 200;
    byte[] serializedResponse = getExpectedResponse();
    int expectedResponseLength = serializedResponse.length;
    verify(mockExchange, times(1)).sendResponseHeaders(expectedStatus, expectedResponseLength);
    verify(mockStream, times(1)).write(serializedResponse);
    mockStream.close();
    subscriberArgumentCaptor.getValue().published(WhatHappened.childChanged, Topic.of(mock(Context.class), "role", "role"));
    handler.getAwsCredentialsBypassCache();
    assertThat(pathCaptor.getValue(), containsString("role"));
}
Also used : Subscriber(com.aws.greengrass.config.Subscriber) Headers(com.sun.net.httpserver.Headers) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with Subscriber

use of com.aws.greengrass.config.Subscriber in project aws-greengrass-nucleus by aws-greengrass.

the class TokenExchangeServiceTest method GIVEN_token_exchange_service_WHEN_started_THEN_correct_env_set.

@ParameterizedTest
@ValueSource(ints = { 0, 3000 })
void GIVEN_token_exchange_service_WHEN_started_THEN_correct_env_set(int port) throws Exception {
    Topic portTopic = mock(Topic.class);
    when(portTopic.dflt(anyInt())).thenReturn(portTopic);
    when(portTopic.subscribe(any())).thenAnswer((a) -> {
        ((Subscriber) a.getArgument(0)).published(WhatHappened.initialized, portTopic);
        return null;
    });
    when(portTopic.getOnce()).thenReturn(port);
    Topic roleTopic = mock(Topic.class);
    when(roleTopic.subscribe(any())).thenAnswer((a) -> {
        ((Subscriber) a.getArgument(0)).published(WhatHappened.initialized, roleTopic);
        return null;
    });
    when(roleTopic.getOnce()).thenReturn(MOCK_ROLE_ALIAS);
    when(roleTopic.withValue(anyString())).thenReturn(roleTopic);
    when(roleTopic.dflt(anyString())).thenReturn(roleTopic);
    Topic mockUriTopic = mock(Topic.class);
    Topics mockConfig = mock(Topics.class);
    when(config.getRoot()).thenReturn(mockConfig);
    when(config.lookup(CONFIGURATION_CONFIG_KEY, PORT_TOPIC)).thenReturn(portTopic);
    when(config.lookup(CONFIGURATION_CONFIG_KEY, ACTIVE_PORT_TOPIC)).thenReturn(portTopic);
    when(mockConfig.lookup(SETENV_CONFIG_NAMESPACE, TES_URI_ENV_VARIABLE_NAME)).thenReturn(mockUriTopic);
    when(configuration.lookup(SERVICES_NAMESPACE_TOPIC, DEFAULT_NUCLEUS_COMPONENT_NAME, CONFIGURATION_CONFIG_KEY, IOT_ROLE_ALIAS_TOPIC)).thenReturn(roleTopic);
    TokenExchangeService tes = new TokenExchangeService(config, mockCredentialHandler, mockAuthZHandler, executorService, deviceConfigurationWithRoleAlias(MOCK_ROLE_ALIAS));
    tes.postInject();
    tes.startup();
    Thread.sleep(5000L);
    tes.shutdown();
    verify(mockUriTopic).overrideValue(stringArgumentCaptor.capture());
    String tesUrl = stringArgumentCaptor.getValue();
    URI uri = new URI(tesUrl);
    assertEquals("localhost", uri.getHost());
    assertEquals("/2016-11-01/credentialprovider/", uri.getPath());
    if (port == 0) {
        // If port is 0, then service should url should be set to random port
        assertTrue(uri.getPort() > 0);
    } else {
        assertEquals(port, uri.getPort());
    }
    verify(mockAuthZHandler).registerComponent(stringArgumentCaptor.capture(), operationsCaptor.capture());
    assertEquals(TOKEN_EXCHANGE_SERVICE_TOPICS, stringArgumentCaptor.getValue());
    assertTrue(operationsCaptor.getValue().contains(TokenExchangeService.AUTHZ_TES_OPERATION));
}
Also used : Topics(com.aws.greengrass.config.Topics) Subscriber(com.aws.greengrass.config.Subscriber) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Topic(com.aws.greengrass.config.Topic) URI(java.net.URI) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Subscriber (com.aws.greengrass.config.Subscriber)7 Topic (com.aws.greengrass.config.Topic)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 Test (org.junit.jupiter.api.Test)3 WhatHappened (com.aws.greengrass.config.WhatHappened)2 State (com.aws.greengrass.dependency.State)2 ValueSource (org.junit.jupiter.params.provider.ValueSource)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Topics (com.aws.greengrass.config.Topics)1 GenericExternalService (com.aws.greengrass.lifecyclemanager.GenericExternalService)1 InputValidationException (com.aws.greengrass.lifecyclemanager.exceptions.InputValidationException)1 Headers (com.sun.net.httpserver.Headers)1 URI (java.net.URI)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 NullAndEmptySource (org.junit.jupiter.params.provider.NullAndEmptySource)1