diff --git a/client/src/app/notes.service.spec.ts b/client/src/app/notes.service.spec.ts index a39fc39..9787124 100644 --- a/client/src/app/notes.service.spec.ts +++ b/client/src/app/notes.service.spec.ts @@ -9,14 +9,17 @@ describe('Note service:', () => { const testNotes: Note[] = [ { _id: 'first_id', + user_id: null, body: 'This is the first note' }, { _id: 'second_id', + user_id: null, body: 'This is the second note' }, { _id: 'third_id', + user_id: null, body: 'This is the third note' }, ]; diff --git a/client/src/app/user-service.spec.ts b/client/src/app/user-service.spec.ts new file mode 100644 index 0000000..f2751b6 --- /dev/null +++ b/client/src/app/user-service.spec.ts @@ -0,0 +1,66 @@ +import { HttpClient } from '@angular/common/http'; +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { TestBed } from '@angular/core/testing'; +import { User } from './user'; +import { NotesService } from './notes.service'; +import { UserService } from './user-service'; + +describe('Note service:', () => { + // pulled these from https://github.com/UMM-CSci-3601-S20/it-1-knights-who-say-ni + const testOwners: User[] = [ + { + _id: 'testman_id', + name: 'Test Man', + officeID: '3168', + email: 'tman@gmail.com', + building: 'Testing' + }, + { + _id: 'spaceman_id', + name: 'Space Man', + officeID: '9999', + email: 'moonrocks@hotmail.com', + building: 'The moon' + }, + { + _id: 'cowtipper_id', + name: 'Cow Tipper', + officeID: '4512', + email: 'fiendofthebovine@gmail.com', + building: 'Some pasture' + }, + { + _id: 'alien_id', + name: 'Alien', + officeID: '9999', + email: 'definitelyfriendly@outerspace.com', + building: 'The moon' + }, + ]; + + let userService: UserService; + // These are used to mock the HTTP requests so that we (a) don't have to + // have the server running and (b) we can check exactly which HTTP + // requests were made to ensure that we're making the correct requests. + let httpClient: HttpClient; + let httpTestingController: HttpTestingController; + + beforeEach(() => { + // Set up the mock handling of the HTTP requests + TestBed.configureTestingModule({ + imports: [HttpClientTestingModule] + }); + httpClient = TestBed.inject(HttpClient); + httpTestingController = TestBed.inject(HttpTestingController); + // Construct an instance of the service with the mock HTTP client + userService = new UserService(httpClient); + }); + + afterEach(() => { + // After every test, make sure we don't have pending requests + httpTestingController.verify(); + }); + + // Insert tests here // + +}); diff --git a/client/src/app/user-service.ts b/client/src/app/user-service.ts new file mode 100644 index 0000000..a8fff96 --- /dev/null +++ b/client/src/app/user-service.ts @@ -0,0 +1,35 @@ +import { Injectable } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; +import { environment } from '../environments/environment'; +import { User } from './user'; +import { Observable } from 'rxjs'; +import { map } from 'rxjs/operators'; + +@Injectable({ + providedIn: 'root' +}) + +export class UserService { + + readonly userUrl: string = environment.API_URL + 'users'; + + constructor(private httpClient: HttpClient) {} + + getUsers() { + return this.httpClient.get(this.userUrl); + } + + // adding stub + addUser(newUser: User): Observable { + return null; + } + // deletion stub + // this may end being complicated if you have to also delete all the user's messages as well + deleteUser(id: string): Observable { + return null; + } + // editing stub + editUser(editUser: User, id: string): Observable { + return null; + } +} diff --git a/server/src/test/java/umm3601/user/UserControllerSpec.java b/server/src/test/java/umm3601/user/UserControllerSpec.java new file mode 100644 index 0000000..2cd339f --- /dev/null +++ b/server/src/test/java/umm3601/user/UserControllerSpec.java @@ -0,0 +1,139 @@ +package umm3601.user; + +import static com.mongodb.client.model.Filters.eq; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.common.collect.ImmutableMap; +import com.mockrunner.mock.web.MockHttpServletRequest; +import com.mockrunner.mock.web.MockHttpServletResponse; +import com.mongodb.client.MongoClient; +import com.mongodb.BasicDBObject; +import com.mongodb.MongoClientSettings; +import com.mongodb.ServerAddress; +import com.mongodb.client.MongoClients; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; + +import static com.mongodb.client.model.Filters.eq; + +import org.bson.Document; +import org.bson.types.ObjectId; +import org.eclipse.jetty.security.UserDataConstraint; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; + +import io.javalin.http.BadRequestResponse; +import io.javalin.http.Context; +import io.javalin.http.util.ContextUtil; +import io.javalin.plugin.json.JavalinJson; +import io.javalin.http.NotFoundResponse; + +import umm3601.users.User; +import umm3601.users.UserController; + + +public class UserControllerSpec { + + MockHttpServletRequest mockReq = new MockHttpServletRequest(); + MockHttpServletResponse mockRes = new MockHttpServletResponse(); + + private UserController userController; + + static MongoClient mongoClient; + static MongoDatabase db; + + static ObjectMapper jsonMapper = new ObjectMapper(); + + @BeforeAll + public static void setupAll() { + String mongoAddr = System.getenv().getOrDefault("Mongo_ADDR", "localhost"); + + mongoClient = MongoClients.create(MongoClientSettings.builder().applyToClusterSettings + (builder -> builder.hosts(Arrays.asList(new ServerAddress(mongoAddr)))).build()); + + db = mongoClient.getDatabase("test"); + } + + @BeforeEach + public void setupEach() throws IOException { + + // Reset our mock request and response objects + mockReq.resetAll(); + mockRes.resetAll(); + + // Setting up the database + MongoCollection userDocuments = db.getCollection("users"); + userDocuments.drop(); + List testUsers = new ArrayList<>(); + // Copy pasted test users from https://github.com/UMM-CSci-3601-S20/it-1-knights-who-say-ni + testUsers.add(Document.parse("{\n" + + " name: \"Rachael Johnson\",\n" + + " officeID: \"1310\",\n" + + " building: \"Science\",\n" + + " email: \"rmjohns@morris.umn.edu\"\n" + + " }")); + testUsers.add(Document.parse("{\n" + + " name: \"Robert Denton\",\n" + + " officeID: \"1523\",\n" + + " building: \"Science\",\n" + + " email: \"rdenton@morris.umn.edu\"\n" + + " }")); + testUsers.add(Document.parse("{\n" + + " name: \"Emily Bruce\",\n" + + " officeID: \"1600\",\n" + + " building: \"Camden\",\n" + + " email: \"bruce088@morris.umn.edu\"\n" + + " }")); + + userDocuments.insertMany(testUsers); + + userController = new UserController(db); + } + + @AfterAll + public static void teardown() { + db.drop(); + mongoClient.close(); + } + + // UPDATE WHEN THE STUBS ARE COMPLETED + // List of tests (stubs) that we should have for our newly created user controller + // everything after GetAllUsers() is not something we promised, so we shouldn't worry about them unless we have the time (assuming we even end up implementing them) + + + // we want to make sure that we are actually getting all of the users for our user directory + @Test + public void GetAllUsers() throws IOException { + + } + + // if we end up allowing the addition of users, we'll want some testing for it + @Test + public void addUser() throws IOException { + + } + + // same deal here. If we end up implementing user deletion, we'll want this test + @Test + public void deleteUser() throws IOException { + + } + + // and again. If this ends up happening, we want to test it + @Test + public void editUser() throws IOException { + + } +}