Skip to content

Commit

Permalink
Integrated changes, fixed last test
Browse files Browse the repository at this point in the history
  • Loading branch information
cremertim committed Sep 23, 2024
1 parent aff8ad6 commit e80e7fd
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 92 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
import org.springframework.web.bind.annotation.RestController;

import de.tum.cit.aet.artemis.communication.domain.Faq;
import de.tum.cit.aet.artemis.communication.service.FaqService;
import de.tum.cit.aet.artemis.communication.repository.FaqRepository;
import de.tum.cit.aet.artemis.core.domain.Course;
import de.tum.cit.aet.artemis.core.exception.BadRequestAlertException;
import de.tum.cit.aet.artemis.core.exception.EntityNotFoundException;
import de.tum.cit.aet.artemis.core.repository.CourseRepository;
import de.tum.cit.aet.artemis.core.security.Role;
import de.tum.cit.aet.artemis.core.security.annotations.EnforceAtLeastInstructor;
Expand All @@ -47,17 +46,17 @@ public class FaqResource {
@Value("${jhipster.clientApp.name}")
private String applicationName;

private final FaqService faqService;

private final CourseRepository courseRepository;

private final FaqRepository faqRepository;

private final AuthorizationCheckService authCheckService;

public FaqResource(FaqService faqService, CourseRepository courseRepository, AuthorizationCheckService authCheckService) {
public FaqResource(CourseRepository courseRepository, AuthorizationCheckService authCheckService, FaqRepository faqRepository) {

this.faqService = faqService;
this.courseRepository = courseRepository;
this.authCheckService = authCheckService;
this.faqRepository = faqRepository;
}

/**
Expand All @@ -76,7 +75,7 @@ public ResponseEntity<Faq> createFaq(@RequestBody Faq faq) throws URISyntaxExcep
}
authCheckService.checkHasAtLeastRoleInCourseElseThrow(Role.INSTRUCTOR, faq.getCourse(), null);

Faq savedFaq = faqService.save(faq);
Faq savedFaq = faqRepository.save(faq);
return ResponseEntity.created(new URI("/api/faqs/" + savedFaq.getId())).body(savedFaq);
}

Expand All @@ -96,8 +95,8 @@ public ResponseEntity<Faq> updateFaq(@RequestBody Faq faq, @PathVariable Long fa
throw new BadRequestAlertException("Id of FAQ and path must match", ENTITY_NAME, "idNull");
}
authCheckService.checkHasAtLeastRoleInCourseElseThrow(Role.INSTRUCTOR, faq.getCourse(), null);
Faq existingFaq = faqService.findById(faqId).orElseThrow(() -> new EntityNotFoundException("FAQ not found", faqId));
Faq result = faqService.save(faq);
Faq existingFaq = faqRepository.findByIdElseThrow(faqId);
Faq result = faqRepository.save(faq);
return ResponseEntity.ok().body(result);
}

Expand All @@ -111,7 +110,7 @@ public ResponseEntity<Faq> updateFaq(@RequestBody Faq faq, @PathVariable Long fa
@EnforceAtLeastStudent
public ResponseEntity<Faq> getFaq(@PathVariable Long faqId) {
log.debug("REST request to get faq {}", faqId);
Faq faq = faqService.findById(faqId).orElseThrow(() -> new EntityNotFoundException("FAQ not found", faqId));
Faq faq = faqRepository.findByIdElseThrow(faqId);
authCheckService.checkHasAtLeastRoleInCourseElseThrow(Role.STUDENT, faq.getCourse(), null);
return ResponseEntity.ok(faq);
}
Expand All @@ -127,9 +126,9 @@ public ResponseEntity<Faq> getFaq(@PathVariable Long faqId) {
public ResponseEntity<Void> deleteFaq(@PathVariable Long faqId) {

log.debug("REST request to delete faq {}", faqId);
Faq faq = faqService.findById(faqId).orElseThrow(() -> new EntityNotFoundException("FAQ not found", faqId));
Faq faq = faqRepository.findByIdElseThrow(faqId);
authCheckService.checkHasAtLeastRoleInCourseElseThrow(Role.INSTRUCTOR, faq.getCourse(), null);
faqService.deleteById(faqId);
faqRepository.deleteById(faqId);
return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, faqId.toString())).build();
}

Expand All @@ -146,7 +145,7 @@ public ResponseEntity<Set<Faq>> getFaqForCourse(@PathVariable Long courseId) {

Course course = getCourseForRequest(courseId);
authCheckService.checkHasAtLeastRoleInCourseElseThrow(Role.STUDENT, course, null);
Set<Faq> faqs = faqService.findAllByCourseId(courseId);
Set<Faq> faqs = faqRepository.findAllByCourseId(courseId);
return ResponseEntity.ok().body(faqs);
}

Expand All @@ -163,7 +162,7 @@ public ResponseEntity<Set<String>> getFaqCategoriesForCourse(@PathVariable Long

Course course = getCourseForRequest(courseId);
authCheckService.checkHasAtLeastRoleInCourseElseThrow(Role.STUDENT, course, null);
Set<String> faqs = faqService.findAllCategoriesByCourseId(courseId);
Set<String> faqs = faqRepository.findAllCategoriesByCourseId(courseId);

return ResponseEntity.ok().body(faqs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class CourseUpdateComponent implements OnInit {
faExclamationTriangle = faExclamationTriangle;
faPen = faPen;

faqEnabled: boolean = true; //default value
faqEnabled: true; //default value
communicationEnabled = true;
messagingEnabled = true;
ltiEnabled = false;
Expand Down
4 changes: 2 additions & 2 deletions src/main/webapp/app/faq/faq.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class FAQService {

create(faq: FAQ): Observable<EntityResponseType> {
const copy = FAQService.convertFaqFromClient(faq);
faq.faqState = FAQState.ACCEPTED;
copy.faqState = FAQState.ACCEPTED;
return this.http.post<FAQ>(`api/faqs`, copy, { observe: 'response' }).pipe(
map((res: EntityResponseType) => {
return res;
Expand Down Expand Up @@ -59,7 +59,7 @@ export class FAQService {
* @param res the response
*/
static convertFaqCategoriesFromServer<ERT extends EntityResponseType>(res: ERT): ERT {
if (res.body && res.body.categories) {
if (res.body?.categories) {
FAQService.parseFaqCategories(res.body);
}
return res;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div>
<div class="faq-container">
<h2 class="markdown-preview" [innerHTML]="faq().questionTitle | htmlForMarkdown"></h2>
<h2 class="markdown-preview">{{faq().questionTitle}}</h2>

<div class="badge-container">
@for (category of faq().categories; track category){
Expand Down
3 changes: 0 additions & 3 deletions src/test/java/de/tum/cit/aet/artemis/FaqFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,4 @@ public static Set<String> generateFaqCategories() {
categories.add("this is also a category");
return categories;
}

private FaqFactory() {
}
}
3 changes: 2 additions & 1 deletion src/test/java/de/tum/cit/aet/artemis/FaqIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import de.tum.cit.aet.artemis.communication.domain.FaqState;
import de.tum.cit.aet.artemis.communication.repository.FaqRepository;
import de.tum.cit.aet.artemis.core.domain.Course;
import de.tum.cit.aet.artemis.core.exception.EntityNotFoundException;

class FaqIntegrationTest extends AbstractSpringIntegrationIndependentTest {

Expand Down Expand Up @@ -105,7 +106,7 @@ void updateFaq_correctRequestBody_shouldUpdateFaq() throws Exception {
@Test
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void testGetFaqCategoriesByCourseId() throws Exception {
Faq faq = faqRepository.findById(this.faq.getId()).orElseThrow();
Faq faq = faqRepository.findById(this.faq.getId()).orElseThrow(EntityNotFoundException::new);
Set<String> categories = faq.getCategories();
Set<String> returnedCategories = request.get("/api/courses/" + faq.getCourse().getId() + "/faq-categories", HttpStatus.OK, Set.class);
assertThat(categories).isEqualTo(returnedCategories);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ describe('FaqUpdateComponent', () => {
parent: {
data: of({ course: { id: 1 } }),
},
queryParams: of({
params: {},
}),
snapshot: {
paramMap: convertToParamMap({
courseId: '1',
Expand Down
25 changes: 10 additions & 15 deletions src/test/javascript/spec/component/faq/faq.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ describe('FaqComponent', () => {
parent: {
data: of({ course: { id: 1 } }),
},
queryParams: of({
params: {},
}),
snapshot: {
paramMap: convertToParamMap({
courseId: '1',
Expand Down Expand Up @@ -93,16 +90,17 @@ describe('FaqComponent', () => {
},
}),
],
}).compileComponents();
})
.compileComponents()
.then(() => {
global.ResizeObserver = jest.fn().mockImplementation((callback: ResizeObserverCallback) => {
return new MockResizeObserver(callback);
});
faqComponentFixture = TestBed.createComponent(FAQComponent);
faqComponent = faqComponentFixture.componentInstance;

global.ResizeObserver = jest.fn().mockImplementation((callback: ResizeObserverCallback) => {
return new MockResizeObserver(callback);
});
faqComponentFixture = TestBed.createComponent(FAQComponent);
faqComponent = faqComponentFixture.componentInstance;

faqService = TestBed.inject(FAQService);
faqComponentFixture.detectChanges();
faqService = TestBed.inject(FAQService);
});
});

afterEach(() => {
Expand All @@ -113,18 +111,15 @@ describe('FaqComponent', () => {
const findAllSpy = jest.spyOn(faqService, 'findAllByCourseId');

faqComponentFixture.detectChanges();
//is actually called when debugging, i dont get why it is 0. Need help
expect(findAllSpy).toHaveBeenCalledOnce();
expect(findAllSpy).toHaveBeenCalledWith(1);
expect(faqComponent.faqs).toHaveLength(3);
});

it('should delete faq', () => {
const deleteSpy = jest.spyOn(faqService, 'delete');

faqComponentFixture.detectChanges();
faqComponent.deleteFaq(faq1.id!);

expect(deleteSpy).toHaveBeenCalledOnce();
expect(deleteSpy).toHaveBeenCalledWith(faq1.id!);
expect(faqComponent.faqs).toHaveLength(2);
Expand Down

0 comments on commit e80e7fd

Please sign in to comment.