Skip to content

Commit b60ffbc

Browse files
committed
chore: fix unit tests
1 parent 559d9ea commit b60ffbc

File tree

1 file changed

+8
-21
lines changed

1 file changed

+8
-21
lines changed

src/test/java/com/aws/greengrass/tes/TokenExchangeServiceTest.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.amazon.aws.iot.greengrass.component.common.ComponentType;
99
import com.aws.greengrass.authorization.AuthorizationHandler;
1010
import com.aws.greengrass.authorization.exceptions.AuthorizationException;
11+
import com.aws.greengrass.config.ChildChanged;
1112
import com.aws.greengrass.config.Configuration;
1213
import com.aws.greengrass.config.Subscriber;
1314
import com.aws.greengrass.config.Topic;
@@ -131,14 +132,7 @@ void cleanup() throws Exception {
131132
@ParameterizedTest
132133
@ValueSource(ints = {0, 3000})
133134
void GIVEN_token_exchange_service_WHEN_started_THEN_correct_env_set(int port) throws Exception {
134-
Topic portTopic = mock(Topic.class);
135-
when(portTopic.dflt(anyInt())).thenReturn(portTopic);
136-
when(portTopic.subscribe(any())).thenAnswer((a) -> {
137-
((Subscriber) a.getArgument(0)).published(WhatHappened.initialized, portTopic);
138-
return null;
139-
});
140-
when(portTopic.getOnce()).thenReturn(port);
141-
135+
Topic portTopic = Topic.of(new Context(), PORT_TOPIC, port);
142136
Topic roleTopic = mock(Topic.class);
143137
when(roleTopic.subscribe(any())).thenAnswer((a) -> {
144138
((Subscriber) a.getArgument(0)).published(WhatHappened.initialized, roleTopic);
@@ -157,6 +151,11 @@ void GIVEN_token_exchange_service_WHEN_started_THEN_correct_env_set(int port) th
157151
when(configuration.lookup(SERVICES_NAMESPACE_TOPIC, DEFAULT_NUCLEUS_COMPONENT_NAME, CONFIGURATION_CONFIG_KEY,
158152
IOT_ROLE_ALIAS_TOPIC)).thenReturn(roleTopic);
159153

154+
when(config.subscribe(any())).thenAnswer((a) -> {
155+
((ChildChanged) a.getArgument(0)).childChanged(WhatHappened.initialized, portTopic);
156+
return null;
157+
});
158+
160159
TokenExchangeService tes = new TokenExchangeService(config,
161160
mockCredentialHandler,
162161
mockAuthZHandler, deviceConfigurationWithRoleAlias(MOCK_ROLE_ALIAS));
@@ -180,7 +179,6 @@ void GIVEN_token_exchange_service_WHEN_started_THEN_correct_env_set(int port) th
180179
verify(mockAuthZHandler).registerComponent(stringArgumentCaptor.capture(), operationsCaptor.capture());
181180
assertEquals(TOKEN_EXCHANGE_SERVICE_TOPICS, stringArgumentCaptor.getValue());
182181
assertTrue(operationsCaptor.getValue().contains(TokenExchangeService.AUTHZ_TES_OPERATION));
183-
184182
}
185183

186184
@ParameterizedTest
@@ -202,13 +200,7 @@ void GIVEN_token_exchange_service_WHEN_started_with_empty_role_alias_THEN_server
202200
// set mock for port topic
203201
Topic portTopic = mock(Topic.class);
204202
when(portTopic.dflt(anyInt())).thenReturn(portTopic);
205-
when(portTopic.subscribe(any())).thenAnswer((a) -> {
206-
((Subscriber) a.getArgument(0)).published(WhatHappened.initialized, portTopic);
207-
return null;
208-
});
209-
when(portTopic.getOnce()).thenReturn(8080);
210-
211-
203+
212204
when(config.lookup(CONFIGURATION_CONFIG_KEY, PORT_TOPIC)).thenReturn(portTopic);
213205
when(configuration.lookup(SERVICES_NAMESPACE_TOPIC, DEFAULT_NUCLEUS_COMPONENT_NAME, CONFIGURATION_CONFIG_KEY,
214206
IOT_ROLE_ALIAS_TOPIC)).thenReturn(roleTopic);
@@ -240,11 +232,6 @@ void GIVEN_token_exchange_service_WHEN_auth_errors_THEN_server_errors_out(Extens
240232
// set mock for port topic
241233
Topic portTopic = mock(Topic.class);
242234
when(portTopic.dflt(anyInt())).thenReturn(portTopic);
243-
when(portTopic.subscribe(any())).thenAnswer((a) -> {
244-
((Subscriber) a.getArgument(0)).published(WhatHappened.initialized, portTopic);
245-
return null;
246-
});
247-
when(portTopic.getOnce()).thenReturn(8080);
248235
when(config.lookup(CONFIGURATION_CONFIG_KEY, PORT_TOPIC)).thenReturn(portTopic);
249236
when(configuration.lookup(SERVICES_NAMESPACE_TOPIC, DEFAULT_NUCLEUS_COMPONENT_NAME, CONFIGURATION_CONFIG_KEY,
250237
IOT_ROLE_ALIAS_TOPIC)).thenReturn(roleTopic);

0 commit comments

Comments
 (0)