-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHookExecutableWrapperTest.java
More file actions
156 lines (129 loc) · 6.64 KB
/
HookExecutableWrapperTest.java
File metadata and controls
156 lines (129 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package software.amazon.cloudformation;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.verify;
import com.fasterxml.jackson.core.type.TypeReference;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.cloudformation.encryption.KMSCipher;
import software.amazon.cloudformation.injection.CredentialsProvider;
import software.amazon.cloudformation.loggers.CloudWatchLogPublisher;
import software.amazon.cloudformation.loggers.LogPublisher;
import software.amazon.cloudformation.metrics.MetricsPublisher;
import software.amazon.cloudformation.proxy.Credentials;
import software.amazon.cloudformation.proxy.OperationStatus;
import software.amazon.cloudformation.proxy.ProgressEvent;
import software.amazon.cloudformation.proxy.hook.HookHandlerRequest;
import software.amazon.cloudformation.proxy.hook.HookProgressEvent;
import software.amazon.cloudformation.proxy.hook.HookStatus;
import software.amazon.cloudformation.resource.SchemaValidator;
import software.amazon.cloudformation.resource.Serializer;
@ExtendWith(MockitoExtension.class)
public class HookExecutableWrapperTest {
private static final String TEST_DATA_BASE_PATH = "src/test/java/software/amazon/cloudformation/data/hook/%s";
@Mock
private CredentialsProvider providerLoggingCredentialsProvider;
@Mock
private MetricsPublisher providerMetricsPublisher;
@Mock
private CloudWatchLogPublisher providerEventsLogger;
@Mock
private LogPublisher platformEventsLogger;
@Mock
private SchemaValidator validator;
@Mock
private HookHandlerRequest hookHandlerRequest;
@Mock
private SdkHttpClient httpClient;
@Mock
private KMSCipher cipher;
private HookExecutableWrapperOverride wrapper;
@BeforeEach
public void initWrapper() {
wrapper = new HookExecutableWrapperOverride(providerLoggingCredentialsProvider, platformEventsLogger,
providerEventsLogger, providerMetricsPublisher, validator, httpClient,
cipher);
}
private static InputStream loadRequestStream(final String fileName) {
final File file = new File(String.format(TEST_DATA_BASE_PATH, fileName));
try {
return new FileInputStream(file);
} catch (final FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
private void verifyInitialiseRuntime() {
verify(providerLoggingCredentialsProvider).setCredentials(any(Credentials.class));
verify(providerMetricsPublisher).refreshClient();
}
private void verifyHandlerResponse(final OutputStream out, final HookProgressEvent<TestContext> expected) throws IOException {
final Serializer serializer = new Serializer();
final HookProgressEvent<TestContext> handlerResponse = serializer.deserialize(out.toString(),
new TypeReference<HookProgressEvent<TestContext>>() {
});
assertThat(handlerResponse.getClientRequestToken()).isEqualTo(expected.getClientRequestToken());
assertThat(handlerResponse.getHookStatus()).isEqualTo(expected.getHookStatus());
assertThat(handlerResponse.getErrorCode()).isEqualTo(expected.getErrorCode());
assertThat(handlerResponse.getMessage()).isEqualTo(expected.getMessage());
assertThat(handlerResponse.getResult()).isEqualTo(expected.getResult());
assertThat(handlerResponse.getCallbackContext()).isEqualTo(expected.getCallbackContext());
assertThat(handlerResponse.getCallbackDelaySeconds()).isEqualTo(expected.getCallbackDelaySeconds());
assertThat(handlerResponse.getAnnotations()).isEqualTo(expected.getAnnotations());
}
@ParameterizedTest
@CsvSource({ "preCreate.request.json,CREATE_PRE_PROVISION", "preUpdate.request.json,UPDATE_PRE_PROVISION",
"preDelete.request.json,DELETE_PRE_PROVISION" })
public void invokeHandler_CompleteSynchronously_returnsSuccess(final String requestDataPath,
final String invocationPointString)
throws IOException {
final HookInvocationPoint invocationPoint = HookInvocationPoint.valueOf(invocationPointString);
// if the handler responds Complete, this is treated as a successful synchronous
// completion
final ProgressEvent<TestModel,
TestContext> pe = ProgressEvent.<TestModel, TestContext>builder().status(OperationStatus.SUCCESS).build();
wrapper.setInvokeHandlerResponse(pe);
lenient().when(cipher.decryptCredentials(any())).thenReturn(new Credentials("123", "123", "123"));
wrapper.setTransformResponse(hookHandlerRequest);
try (final InputStream in = loadRequestStream(requestDataPath); final OutputStream out = new ByteArrayOutputStream()) {
wrapper.handleRequest(in, out);
// verify initialiseRuntime was called and initialised dependencies
verifyInitialiseRuntime();
// verify output response
verifyHandlerResponse(out,
HookProgressEvent.<TestContext>builder().clientRequestToken("123456").hookStatus(HookStatus.SUCCESS).build());
// assert handler receives correct injections
assertThat(wrapper.awsClientProxy).isNotNull();
assertThat(wrapper.getRequest()).isEqualTo(hookHandlerRequest);
assertThat(wrapper.invocationPoint).isEqualTo(invocationPoint);
assertThat(wrapper.callbackContext).isNull();
}
}
}