Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
v-yussupov committed Oct 28, 2023
1 parent b1e0743 commit 2cf945c
Show file tree
Hide file tree
Showing 23 changed files with 2,678 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
import java.util.Optional;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.faastener.core.model.entities.ClassificationFrameworkEntity;
import org.faastener.core.model.common.TechnologyType;
import org.faastener.core.model.domain.ClassificationFramework;
import org.faastener.core.model.domain.ClassificationFramework.CriteriaGrouping;
import org.faastener.core.model.domain.ClassificationFramework.CriterionType;
import org.faastener.core.model.domain.ClassificationFramework.FrameworkView;
import org.faastener.core.services.ClassificationFrameworkService;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
Expand All @@ -33,7 +37,8 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@ExtendWith(SpringExtension.class)
@WebMvcTest(ClassificationFrameworkController.class)
@SpringBootTest
@AutoConfigureMockMvc
@TestPropertySource(properties = "mongock.enabled=false")
class ClassificationFrameworkControllerTest {
@MockBean
Expand All @@ -46,7 +51,7 @@ class ClassificationFrameworkControllerTest {
@DisplayName("GET /api/frameworks/{id} - Found")
void testGetFrameworkByIdFound() throws Exception {
ObjectMapper mapper = new ObjectMapper();
ClassificationFrameworkEntity mockFramework = mapper.readValue(new ClassPathResource("data/cu1-frameworks.json").getFile(), ClassificationFrameworkEntity.class);
ClassificationFramework mockFramework = mapper.readValue(new ClassPathResource("data/cu1-frameworks.json").getFile(), ClassificationFramework.class);
doReturn(Optional.of(mockFramework)).when(frameworkService).findById("fwFaaS");

mockMvc.perform(get("/api/frameworks/{id}", "fwFaaS"))
Expand Down Expand Up @@ -78,12 +83,12 @@ void testGetFrameworkByIdNotFound() throws Exception {
@Test
@DisplayName("POST /api/frameworks - Success")
void testCreateFramework() throws Exception {
List<ClassificationFrameworkEntity.CriterionTypeEntity> mockCriteria = new ArrayList<>();
mockCriteria.add(new ClassificationFrameworkEntity.CriterionTypeEntity("criterion1", "criterion type 1", "criterion type 1 description", Collections.emptyList()));
mockCriteria.add(new ClassificationFrameworkEntity.CriterionTypeEntity("criterion1", "criterion type 2", "criterion type 2 description", Collections.emptyList()));
ClassificationFrameworkEntity.CriteriaGroupingEntity mockGrouping = new ClassificationFrameworkEntity.CriteriaGroupingEntity("groupingId", "test grouping", "grouping description", Collections.emptyList(), mockCriteria);
ClassificationFrameworkEntity.FrameworkViewEntity mockView = new ClassificationFrameworkEntity.FrameworkViewEntity("viewId", "test view", "view description", Collections.singletonList(mockGrouping));
ClassificationFrameworkEntity mockFramework = new ClassificationFrameworkEntity("frameworkId", "test", TechnologyType.FAAS, "1.0", "some description", Collections.singletonList(mockView));
List<CriterionType> mockCriteria = new ArrayList<>();
mockCriteria.add(new CriterionType("criterion1", "criterion type 1", "criterion type 1 description", Collections.emptyList()));
mockCriteria.add(new CriterionType("criterion1", "criterion type 2", "criterion type 2 description", Collections.emptyList()));
CriteriaGrouping mockGrouping = new CriteriaGrouping("groupingId", "test grouping", "grouping description", Collections.emptyList(), mockCriteria);
FrameworkView mockView = new FrameworkView("viewId", "test view", "view description", Collections.singletonList(mockGrouping));
ClassificationFramework mockFramework = new ClassificationFramework("frameworkId", "test", TechnologyType.FAAS, "1.0", "some description", Collections.singletonList(mockView));

doReturn(mockFramework).when(frameworkService).save(any());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.faastener.core.model.domain.FilterConfiguration;
import org.faastener.core.model.entities.FilterConfigurationEntity;
import org.faastener.core.services.FilterConfigurationService;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
Expand All @@ -28,7 +31,8 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@ExtendWith(SpringExtension.class)
@WebMvcTest(FilterConfigurationController.class)
@SpringBootTest
@AutoConfigureMockMvc
@TestPropertySource(properties = "mongock.enabled=false")
class FilterConfigurationControllerTest {
@MockBean
Expand All @@ -42,7 +46,7 @@ class FilterConfigurationControllerTest {
void testGetFilterConfigurationByIdFound() throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS);
FilterConfigurationEntity mockFilterConfiguration = mapper.readValue(new ClassPathResource("data/cu3-filters.json").getFile(), FilterConfigurationEntity.class);
FilterConfiguration mockFilterConfiguration = mapper.readValue(new ClassPathResource("data/cu3-filters.json").getFile(), FilterConfiguration.class);
doReturn(Optional.of(mockFilterConfiguration)).when(filterConfigurationService).findById("faas-filter");

mockMvc.perform(get("/api/filters/{id}", "faas-filter"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
Expand All @@ -33,8 +35,8 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@TestPropertySource(properties = "mongock.enabled=false")
@ExtendWith(SpringExtension.class)
@WebMvcTest(TechnologyController.class)
@SpringBootTest
@AutoConfigureMockMvc
class TechnologyControllerTest {
@MockBean
private TechnologyService technologiesService;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package org.faastener.core.repositories;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import org.faastener.core.model.common.TechnologyType;
import org.faastener.core.model.entities.ClassificationFrameworkEntity;
import org.faastener.core.model.entities.ClassificationFrameworkEntity.CriteriaGroupingEntity;
import org.faastener.core.model.entities.ClassificationFrameworkEntity.CriterionTypeEntity;
import org.faastener.core.model.entities.ClassificationFrameworkEntity.FrameworkViewEntity;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.context.TestPropertySource;

@DataMongoTest
@ExtendWith(MongoRepositoryTestExtension.class)
@TestPropertySource(properties = "mongock.enabled=false")
class ClassificationFrameworkRepositoryTest {
@Autowired
private MongoTemplate mongoTemplate;

@Autowired
private ClassificationFrameworkRepository repository;


public MongoTemplate getMongoTemplate() {
return mongoTemplate;
}

@Test
@MongoDataFile(value = "framework.json", classType = ClassificationFrameworkEntity.class, collectionName = "frameworks")
void testSave() {
ClassificationFrameworkEntity framework1 = initTestFramework(2, 3, 3);
ClassificationFrameworkEntity savedFramework1 = repository.save(framework1);

// Retrieve the review
Optional<ClassificationFrameworkEntity> loadedFramework1 = repository.findById(savedFramework1.getId());

// Validations
Assertions.assertTrue(loadedFramework1.isPresent());

loadedFramework1.ifPresent(f1 -> {
Assertions.assertEquals(framework1.getId(), f1.getId());
Assertions.assertEquals(framework1.getName(), f1.getName());
Assertions.assertEquals(framework1.getDescription(), f1.getDescription());
Assertions.assertEquals(framework1.getVersion(), f1.getVersion());
Assertions.assertEquals(2, f1.getFrameworkViews().size(), "There should be 2 views");
});
}

@Test
@MongoDataFile(value = "framework.json", classType = ClassificationFrameworkEntity.class, collectionName = "frameworks")
void testSaveAll() {
ClassificationFrameworkEntity framework1 = initTestFramework(2, 3, 3);
ClassificationFrameworkEntity framework2 = initTestFramework(1, 2, 4);

List<ClassificationFrameworkEntity> savedFrameworks = repository.saveAll(List.of(framework1, framework2));

// Retrieve the review
List<ClassificationFrameworkEntity> loadedFrameworks = repository.findAll();

// Validations
Assertions.assertEquals(2, loadedFrameworks.size());
}

@Test
@MongoDataFile(value = "framework.json", classType = ClassificationFrameworkEntity.class, collectionName = "frameworks")
void testFindByIdSuccess() {
Optional<ClassificationFrameworkEntity> framework = repository.findById("fwFaaS");
Assertions.assertTrue(framework.isPresent(), "Framework with the given ID should be present");
framework.ifPresent(fw -> {
Assertions.assertEquals("fwFaaS", fw.getId(), "Framework ID should be fwFaaS");
Assertions.assertEquals(TechnologyType.FAAS, fw.getTechnologyType(), "Framework type should be FAAS");
Assertions.assertEquals(2, fw.getFrameworkViews().size(), "Framework with ID fw-faas should have 2 views");
});
}

@Test
@MongoDataFile(value = "framework.json", classType = ClassificationFrameworkEntity.class, collectionName = "frameworks")
void testFindByIdFailure() {
Optional<ClassificationFrameworkEntity> framework = repository.findById("absentID");
Assertions.assertFalse(framework.isPresent(), "No framework with ID absentID should exist");
}

private ClassificationFrameworkEntity initTestFramework(int numViews, int numGroupings, int criteriaTypesPerGrouping) {
ClassificationFrameworkEntity framework = new ClassificationFrameworkEntity();
framework.setId("test-fw-id");
framework.setName("test-fw-name");
framework.setDescription("test-desc");
framework.setVersion("1.0.0");

framework.setFrameworkViews(initTestViews(numViews, numGroupings, criteriaTypesPerGrouping));

return framework;
}

private List<FrameworkViewEntity> initTestViews(int numViews, int groupingsPerView, int criteriaTypesPerGrouping) {
List<FrameworkViewEntity> views = new ArrayList<>();

for (int view = 1; view <= numViews; view++) {
FrameworkViewEntity v = new FrameworkViewEntity();
v.setId("test-view-id-" + view);
v.setName("test-view-" + view);
v.setDescription("test-view-desc-" + view);
v.setCriteriaGroupings(initTestGroupings(groupingsPerView, criteriaTypesPerGrouping));

views.add(v);
}

return views;
}

private List<CriteriaGroupingEntity> initTestGroupings(int numGroupings, int criteriaTypesPerGrouping) {
List<CriteriaGroupingEntity> groupings = new ArrayList<>();

for (int grouping = 1; grouping <= numGroupings; grouping++) {
CriteriaGroupingEntity g = new CriteriaGroupingEntity();
g.setId("test-grouping-id-" + grouping);
g.setName("test-grouping-name-" + grouping);
g.setLocator("test-grouping-locator-" + grouping);
g.setCriteria(initTestCriteriaTypes(criteriaTypesPerGrouping));

groupings.add(g);
}

return groupings;
}

private List<CriterionTypeEntity> initTestCriteriaTypes(int numCrtieriaTypes) {
List<CriterionTypeEntity> criteriaTypes = new ArrayList<>();

for (int type = 1; type <= numCrtieriaTypes; type++) {
CriterionTypeEntity ct = new CriterionTypeEntity();
ct.setId("test-criterion-type-id-" + type);
ct.setName("test-criterion-type-name-id-" + type);
ct.setDescription("test-criterion-type-desc-id-" + type);
ct.setExampleValues(Collections.singletonList("single example"));

criteriaTypes.add(ct);
}

return criteriaTypes;
}
}




34 changes: 34 additions & 0 deletions src/test/java/org/faastener/core/repositories/MongoDataFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.faastener.core.repositories;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* An annotation that provides information about the test MongoDB JSON file, collection name, and type of objects stored in the test file
*/
@Target( {ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MongoDataFile {
/**
* The name of the MongoDB JSON test file.
*
* @return The name of the MongoDB JSON test file.
*/
String value();

/**
* The class of objects stored in the MongoDB test file.
*
* @return The class of objects stored in the MongoDB test file.
*/
Class classType();

/**
* The name of the MongoDB collection hosting the test objects.
*
* @return The name of the MongoDB collection hosting the test objects.
*/
String collectionName();
}
Loading

0 comments on commit 2cf945c

Please sign in to comment.