Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

<!-- DEPENDENCIES VERSION -->
<jdk.version>1.8</jdk.version>
<cds.services.version>1.25.0</cds.services.version>
<cds.services.version>1.26.0</cds.services.version>
<spring.boot.version>2.6.8</spring.boot.version>
<cloud.sdk.version>3.68.0</cloud.sdk.version>
<xsuaa.version>2.12.1</xsuaa.version>
<xsuaa.version>2.12.2</xsuaa.version>
<cf-java-logging-support.version>3.5.7</cf-java-logging-support.version>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.sap.cloud.sdk.cloudplatform.connectivity.DefaultDestinationLoader;
import com.sap.cloud.sdk.cloudplatform.connectivity.DefaultHttpDestination;
import com.sap.cloud.sdk.cloudplatform.connectivity.DestinationAccessor;
import com.sap.cloud.sdk.cloudplatform.security.BasicCredentials;

@Component
@Profile("mocked")
Expand All @@ -25,6 +26,7 @@ void applicationReady(ApplicationReadyEvent ready) {
if(port != null && destinationName != null) {
DefaultHttpDestination httpDestination = DefaultHttpDestination
.builder("http://localhost:" + port)
.basicCredentials(new BasicCredentials("authenticated", ""))
.name(destinationName).build();

DestinationAccessor.prependDestinationLoader(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void afterClose(boolean completed) {
ABusinessPartnerAddress address = ABusinessPartnerAddress.create();
address.setHouseNumber("17");

client.patch().uri(String.format(remoteAddressURI, "10401010", "100"))
client.patch().uri(String.format(remoteAddressURI, "10401010", "100")).headers(this::authenticatedCredentials)
.header("Content-Type", "application/json")
.bodyValue(address.toJson())
.exchange()
Expand All @@ -108,4 +108,7 @@ private void adminCredentials(HttpHeaders headers) {
headers.setBasicAuth("admin", "admin");
}

private void authenticatedCredentials(HttpHeaders headers) {
headers.setBasicAuth("authenticated", "");
}
}
9 changes: 6 additions & 3 deletions srv/src/test/java/my/bookshop/CatalogServiceITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class CatalogServiceITest {

private static final String USER_USER_STRING = "user";
private static final String ADMIN_USER_STRING = "admin";
private static final String AUTHENTICATED_USER_STRING = "authenticated"; // given by default

@Autowired
private MockMvc mockMvc;
Expand All @@ -47,13 +48,15 @@ public void cleanup() {
}

@Test
@WithMockUser(AUTHENTICATED_USER_STRING)
public void testDiscountApplied() throws Exception {
mockMvc.perform(get(booksURI + "?$filter=stock gt 200&top=1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.value[0].title").value(containsString("11% discount")));
}

@Test
@WithMockUser(AUTHENTICATED_USER_STRING)
public void testDiscountNotApplied() throws Exception {
mockMvc.perform(get(booksURI + "?$filter=stock lt 100&top=1"))
.andExpect(status().isOk())
Expand All @@ -64,11 +67,11 @@ public void testDiscountNotApplied() throws Exception {
public void testCreateReviewNotAuthenticated() throws Exception {
String payload = createTestReview().toJson();
mockMvc.perform(post(addReviewURI).contentType(MediaType.APPLICATION_JSON).content(payload))
.andExpect(status().isForbidden());
.andExpect(status().isUnauthorized());
}

@Test
@WithMockUser(username = USER_USER_STRING)
@WithMockUser(USER_USER_STRING)
public void testCreateReviewByUser() throws Exception {
String payload = createTestReview().toJson();
mockMvc.perform(post(addReviewURI).contentType(MediaType.APPLICATION_JSON).content(payload))
Expand All @@ -77,7 +80,7 @@ public void testCreateReviewByUser() throws Exception {
}

@Test
@WithMockUser(username = ADMIN_USER_STRING)
@WithMockUser(ADMIN_USER_STRING)
public void testCreateReviewByAdmin() throws Exception {
String payload = createTestReview().toJson();
mockMvc.perform(post(addReviewURI).contentType(MediaType.APPLICATION_JSON).content(payload))
Expand Down
26 changes: 17 additions & 9 deletions srv/src/test/java/my/bookshop/NotesServiceITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.http.HttpHeaders;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.reactive.server.WebTestClient;
Expand All @@ -23,7 +24,7 @@ public class NotesServiceITest {

@Test
public void testGetNotes() throws Exception {
client.get().uri(notesURI).exchange()
client.get().uri(notesURI).headers(this::authenticatedCredentials).exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.['@context']").isEqualTo("$metadata#Notes")
Expand All @@ -43,7 +44,7 @@ public void testGetNotes() throws Exception {

@Test
public void testGetAddresses() throws Exception {
client.get().uri(addressesURI + "?$filter=businessPartner eq '10401010'").exchange()
client.get().uri(addressesURI + "?$filter=businessPartner eq '10401010'").headers(this::authenticatedCredentials).exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.['@context']").isEqualTo("$metadata#Addresses")
Expand All @@ -57,7 +58,7 @@ public void testGetAddresses() throws Exception {

@Test
public void testGetNoteWithAddress() throws Exception {
client.get().uri(notesURI + "?$expand=address").exchange()
client.get().uri(notesURI + "?$expand=address").headers(this::authenticatedCredentials).exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.['@context']").isEqualTo("$metadata#Notes(address())")
Expand Down Expand Up @@ -86,7 +87,7 @@ public void testGetNoteWithAddress() throws Exception {

@Test
public void testGetSuppliersWithNotes() throws Exception {
client.get().uri(addressesURI + "?$expand=notes($orderby=ID)&$filter=businessPartner eq '10401010'").exchange()
client.get().uri(addressesURI + "?$expand=notes($orderby=ID)&$filter=businessPartner eq '10401010'").headers(this::authenticatedCredentials).exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.['@context']").isEqualTo("$metadata#Addresses(notes())")
Expand All @@ -111,7 +112,7 @@ public void testGetSuppliersWithNotes() throws Exception {

@Test
public void testGetNotesToSupplier() throws Exception {
client.get().uri(notesURI + "(ID=5efc842c-c70d-4ee2-af1d-81c7d257aff7,IsActiveEntity=true)/address").exchange()
client.get().uri(notesURI + "(ID=5efc842c-c70d-4ee2-af1d-81c7d257aff7,IsActiveEntity=true)/address").headers(this::authenticatedCredentials).exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.['@context']").isEqualTo("$metadata#Addresses/$entity")
Expand All @@ -122,7 +123,7 @@ public void testGetNotesToSupplier() throws Exception {

@Test
public void testGetSupplierToNotes() throws Exception {
client.get().uri(addressesURI + "(businessPartner='10401010',ID='100')/notes").exchange()
client.get().uri(addressesURI + "(businessPartner='10401010',ID='100')/notes").headers(this::authenticatedCredentials).exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.value[0].ID").isEqualTo("83e2643b-aecc-47d3-9f85-a8ba14eff07d")
Expand All @@ -138,7 +139,9 @@ public void testGetSupplierToNotes() throws Exception {

@Test
public void testGetSupplierToSpecificNote() throws Exception {
client.get().uri(addressesURI + "(businessPartner='10401010',ID='100')/notes(ID=83e2643b-aecc-47d3-9f85-a8ba14eff07d,IsActiveEntity=true)").exchange()
client.get().uri(addressesURI + "(businessPartner='10401010',ID='100')/notes(ID=83e2643b-aecc-47d3-9f85-a8ba14eff07d,IsActiveEntity=true)")
.headers(this::authenticatedCredentials)
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.ID").isEqualTo("83e2643b-aecc-47d3-9f85-a8ba14eff07d")
Expand All @@ -149,7 +152,7 @@ public void testGetSupplierToSpecificNote() throws Exception {

@Test
public void testGetNotesWithNestedExpands() throws Exception {
client.get().uri(notesURI + "?$select=note&$expand=address($select=postalCode;$expand=notes($select=note))&$top=1").exchange()
client.get().uri(notesURI + "?$select=note&$expand=address($select=postalCode;$expand=notes($select=note))&$top=1").headers(this::authenticatedCredentials).exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.value[0].ID").isEqualTo("5efc842c-c70d-4ee2-af1d-81c7d257aff7")
Expand All @@ -165,7 +168,9 @@ public void testGetNotesWithNestedExpands() throws Exception {

@Test
public void testGetAddressesWithNestedExpands() throws Exception {
client.get().uri(addressesURI + "?$select=postalCode&$expand=notes($select=note;$expand=address($select=postalCode))&$filter=businessPartner eq '1000020'").exchange()
client.get().uri(addressesURI + "?$select=postalCode&$expand=notes($select=note;$expand=address($select=postalCode))&$filter=businessPartner eq '1000020'")
.headers(this::authenticatedCredentials)
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.value[0].businessPartner").isEqualTo("1000020")
Expand All @@ -184,4 +189,7 @@ public void testGetAddressesWithNestedExpands() throws Exception {
.jsonPath("$.value[2]").doesNotExist();
}

private void authenticatedCredentials(HttpHeaders headers) {
headers.setBasicAuth("authenticated", "");
}
}