forked from eclipse-tractusx/puris
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eclipse-tractusx#97 from FraunhoferISST/feat/test-…
…examples Feat: Introduce tests to backend
- Loading branch information
Showing
7 changed files
with
443 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
backend/src/test/java/org/eclipse/tractusx/puris/backend/common/security/ApiKeyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright (c) 2023 Volkswagen AG | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.eclipse.tractusx.puris.backend.common.security; | ||
|
||
import org.eclipse.tractusx.puris.backend.common.security.annotation.WithMockApiKey; | ||
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.test.web.servlet.MockMvc; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
public class ApiKeyTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Test | ||
void StockViewController_MaterialsRequestWithoutAuthHeader_ShouldReturn403() throws Exception { | ||
this.mockMvc.perform( | ||
get("/stockView/materials")) | ||
.andDo(print()) | ||
.andExpect(status().is(403)); | ||
} | ||
|
||
@Test | ||
void StockViewController_MaterialsRequestWithAuthHeader_ShouldReturn200() throws Exception { | ||
this.mockMvc.perform( | ||
get("/stockView/materials") | ||
.header("X-API-KEY", "test") | ||
) | ||
.andExpect(status().is(200)); | ||
} | ||
|
||
@Test | ||
@WithMockApiKey(apiKey = "test2") | ||
void StockViewController_MaterialsRequestWithWrongAnnotationAuth_ShouldReturn403() throws Exception { | ||
this.mockMvc.perform( | ||
get("/stockView/materials") | ||
) | ||
.andExpect(status().is(403)); | ||
} | ||
|
||
@Test | ||
@WithMockApiKey | ||
void StockViewController_MaterialsRequestWithCorrectAnnotationAuth_ShouldReturn200() throws Exception { | ||
this.mockMvc.perform( | ||
get("/stockView/materials") | ||
) | ||
.andExpect(status().is(200)); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...st/java/org/eclipse/tractusx/puris/backend/common/security/annotation/WithMockApiKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2023 Volkswagen AG | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.eclipse.tractusx.puris.backend.common.security.annotation; | ||
|
||
import org.springframework.security.test.context.support.WithSecurityContext; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@WithSecurityContext(factory = WithMockApiKeySecurityContextFactory.class) | ||
public @interface WithMockApiKey { | ||
|
||
String apiKey() default "test"; | ||
} |
47 changes: 47 additions & 0 deletions
47
...actusx/puris/backend/common/security/annotation/WithMockApiKeySecurityContextFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (c) 2023 Volkswagen AG | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.eclipse.tractusx.puris.backend.common.security.annotation; | ||
|
||
import org.eclipse.tractusx.puris.backend.common.security.domain.ApiKeyAuthentication; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.security.core.context.SecurityContext; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.test.context.support.WithSecurityContextFactory; | ||
|
||
public class WithMockApiKeySecurityContextFactory implements WithSecurityContextFactory<WithMockApiKey> { | ||
|
||
@Value("${puris.api.key}") | ||
String apiKeyConfig; | ||
|
||
@Override | ||
public SecurityContext createSecurityContext(WithMockApiKey annotation) { | ||
SecurityContext context = SecurityContextHolder.createEmptyContext(); | ||
|
||
if (this.apiKeyConfig.equals((annotation.apiKey()))){ | ||
ApiKeyAuthentication auth = new ApiKeyAuthentication(annotation.apiKey(), true); | ||
context.setAuthentication(auth); | ||
} else { | ||
ApiKeyAuthentication auth = new ApiKeyAuthentication(annotation.apiKey(), false); | ||
context.setAuthentication(auth); | ||
} | ||
|
||
return context; | ||
} | ||
} |
140 changes: 140 additions & 0 deletions
140
...est/java/org/eclipse/tractusx/puris/backend/stock/controller/StockViewControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/* | ||
* Copyright (c) 2023 Volkswagen AG | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.eclipse.tractusx.puris.backend.stock.controller; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.eclipse.tractusx.puris.backend.common.security.SecurityConfig; | ||
import org.eclipse.tractusx.puris.backend.common.security.annotation.WithMockApiKey; | ||
import org.eclipse.tractusx.puris.backend.common.security.logic.ApiKeyAuthenticationProvider; | ||
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material; | ||
import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialPartnerRelationService; | ||
import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialService; | ||
import org.eclipse.tractusx.puris.backend.masterdata.logic.service.PartnerService; | ||
import org.eclipse.tractusx.puris.backend.stock.logic.dto.FrontendMaterialDto; | ||
import org.eclipse.tractusx.puris.backend.stock.logic.service.MaterialStockService; | ||
import org.eclipse.tractusx.puris.backend.stock.logic.service.PartnerProductStockService; | ||
import org.eclipse.tractusx.puris.backend.stock.logic.service.ProductStockRequestApiService; | ||
import org.eclipse.tractusx.puris.backend.stock.logic.service.ProductStockService; | ||
import org.junit.jupiter.api.Test; | ||
import org.modelmapper.ModelMapper; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.when; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@WebMvcTest(StockViewController.class) | ||
@Import({ SecurityConfig.class, ApiKeyAuthenticationProvider.class }) | ||
class StockViewControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@MockBean | ||
private MaterialService materialService; | ||
|
||
@MockBean | ||
private ProductStockService productStockService; | ||
|
||
@MockBean | ||
private MaterialStockService materialStockService; | ||
|
||
@MockBean | ||
private PartnerProductStockService partnerProductStockService; | ||
|
||
@MockBean | ||
private ProductStockRequestApiService productStockRequestApiService; | ||
|
||
@MockBean | ||
private PartnerService partnerService; | ||
|
||
@MockBean | ||
private MaterialPartnerRelationService mprService; | ||
|
||
@MockBean | ||
private ModelMapper modelMapper; | ||
|
||
@Test | ||
@WithMockApiKey | ||
void getMaterials_GivenTwoMaterials_ReturnsListOfMaterials() throws Exception{ | ||
|
||
// given | ||
Material material1 = Material.builder() | ||
.ownMaterialNumber("MNR-4711") | ||
.materialFlag(true) | ||
.name("Test Material 1") | ||
.materialNumberCx("urn:uuid:ccfffbba-cfa0-49c4-bc9c-4e13d7a4ac7a") | ||
.build(); | ||
Material material2 = Material.builder() | ||
.ownMaterialNumber("MNR-4712") | ||
.materialFlag(true) | ||
.name("Test Material 2") | ||
.build(); | ||
List<Material> allMaterials = new ArrayList<>(); | ||
allMaterials.add(material1); | ||
allMaterials.add(material2); | ||
when(materialService.findAllMaterials()).thenReturn(allMaterials); | ||
|
||
this.mockMvc.perform( | ||
get("/stockView/materials") | ||
) | ||
.andDo(print()) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) | ||
.andDo(result -> { | ||
String jsonResponse = result.getResponse().getContentAsString(); | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
List<FrontendMaterialDto> returnedMaterials = objectMapper.readValue(jsonResponse, new TypeReference<>() { | ||
}); | ||
|
||
assertAll( | ||
() -> assertNotNull(returnedMaterials), | ||
() -> assertEquals(2, returnedMaterials.size()) | ||
); | ||
|
||
assertAll( | ||
() -> assertNotNull(returnedMaterials), | ||
() -> assertEquals(2, returnedMaterials.size()) | ||
); | ||
|
||
FrontendMaterialDto returnedMaterial = returnedMaterials.stream().filter( | ||
frontendMaterialDto -> frontendMaterialDto.getOwnMaterialNumber().equals("MNR-4711") | ||
).findFirst().get(); | ||
assertAll( | ||
() -> assertEquals("MNR-4711", returnedMaterial.getOwnMaterialNumber()), | ||
() -> assertEquals("Test Material 1", returnedMaterial.getDescription()) | ||
); | ||
}); | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
...va/org/eclipse/tractusx/puris/backend/stock/masterdata/domain/MaterialRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright (c) 2023 Volkswagen AG | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.eclipse.tractusx.puris.backend.stock.masterdata.domain; | ||
|
||
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material; | ||
import org.eclipse.tractusx.puris.backend.masterdata.domain.repository.MaterialRepository; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@DataJpaTest | ||
public class MaterialRepositoryTest { | ||
|
||
@Autowired | ||
private MaterialRepository materialRepository; | ||
|
||
@Test | ||
void findAllByMaterialFlagTrue_ReturnsListOfMaterials() { | ||
// Given | ||
Material material1 = new Material(true, false, "MNR-123", "uuid-value", "Test Material 1", new HashSet<>()); | ||
|
||
Material material2 = new Material(true, false, "MNR-234", "uuid-value-2", "Test Material 2", new HashSet<>()); | ||
|
||
// would be more realistic with relationship, but didn't add it here as we just want to test the MaterialRepo | ||
Material product = new Material(false, true, "MNR-456", "uuid-value-2", "Test Product 1", new HashSet<>()); | ||
|
||
materialRepository.save(material1); | ||
materialRepository.save(material2); | ||
materialRepository.save(product); | ||
|
||
// When | ||
List<Material> materials = materialRepository.findAllByMaterialFlagTrue(); | ||
|
||
// Then | ||
assertNotNull(materials); | ||
assertEquals(2, materials.size()); | ||
assertTrue(materials.contains(material1)); | ||
assertTrue(materials.contains(material2)); | ||
} | ||
} |
Oops, something went wrong.