/
AuditControllerTest.java
139 lines (127 loc) · 5.46 KB
/
AuditControllerTest.java
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
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 com.example.audit;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.UUID;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.testcontainers.junit.jupiter.Testcontainers;
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
@Testcontainers
@AutoConfigureMockMvc
public class AuditControllerTest {
@Autowired
private MockMvc mockMvc;
String mockBody;
@BeforeEach
public void setup() throws JSONException {
JSONObject message =
new JSONObject()
.put("quote", "test quote")
.put("author", "anonymous")
.put("book", "new book")
.put("randomId", UUID.randomUUID())
.put("attributes", new JSONObject());
mockBody = new JSONObject().put("message", message).toString();
}
@Disabled
@Test
@DisplayName("This is a successful test. Note diff between platforms!")
public void goodTest() throws Exception {
mockMvc
.perform(
post("/")
.contentType(MediaType.APPLICATION_JSON)
.content(mockBody)
.header("ce-id", "test id")
.header("ce-source", "test source")
.header("ce-type", "test type")
.header("ce-specversion", "test specversion")
.header("ce-subject", "test subject"))
// Linux and Mac Intel - different response from emulator
// .andExpect(status().isOk());
// Mac Apple Silicon throws this error
.andExpect((status().is4xxClientError()));
}
@Test
@DisplayName("Test with empty request body")
public void addEmptyBody() throws Exception {
mockMvc.perform(post("/")).andExpect(status().isBadRequest());
}
@Test
@DisplayName("Test with no message added to teh POST request")
public void addNoMessage() throws Exception {
mockMvc
.perform(post("/").contentType(MediaType.APPLICATION_JSON).content("{}"))
.andExpect(status().isBadRequest());
}
@Test
@DisplayName("Test with an invalid Mime type")
public void addInvalidMimetype() throws Exception {
mockMvc
.perform(post("/").contentType(MediaType.TEXT_HTML).content(mockBody))
.andExpect(status().isUnsupportedMediaType());
}
@Test
@DisplayName("Test with required headers added")
public void addRequiredHeaders() throws Exception {
mockMvc
.perform(
post("/")
.contentType(MediaType.APPLICATION_JSON)
.content(mockBody)
.header("ce-id", "test")
.header("ce-source", "test")
.header("ce-type", "test")
.header("ce-specversion", "test"))
.andExpect(status().is4xxClientError());
}
@Test
@DisplayName("Test with missing required headers")
public void missingRequiredHeaders() throws Exception {
mockMvc
.perform(
post("/")
.contentType(MediaType.APPLICATION_JSON)
.content(mockBody)
.header("ce-source", "test")
.header("ce-type", "test")
.header("ce-specversion", "test")
.header("ce-subject", "test"))
.andExpect(status().isBadRequest());
mockMvc
.perform(
post("/")
.contentType(MediaType.APPLICATION_JSON)
.content(mockBody)
.header("ce-id", "test")
.header("ce-source", "test")
.header("ce-type", "test")
.header("ce-specversion", "test"))
.andExpect(status().isBadRequest());
}
}