Skip to content

Commit

Permalink
Create spec files for usercontroller and the newly created user service
Browse files Browse the repository at this point in the history
These spec files are not currently testing anything, but they should have most of the necessary structure to support expected testing. (we should basically be able to jump right into writing tests)

We still need to create the actual user-doorboard component files as well as e2e tests for all the new features. 

Progresses issue #9
  • Loading branch information
Burge337 committed Mar 26, 2020
1 parent 05a2dc3 commit 3b8003c
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 0 deletions.
3 changes: 3 additions & 0 deletions client/src/app/notes.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
},
];
Expand Down
66 changes: 66 additions & 0 deletions client/src/app/user-service.spec.ts
Original file line number Diff line number Diff line change
@@ -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 //

});
35 changes: 35 additions & 0 deletions client/src/app/user-service.ts
Original file line number Diff line number Diff line change
@@ -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<User[]>(this.userUrl);
}

// adding stub
addUser(newUser: User): Observable<string> {
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<boolean> {
return null;
}
// editing stub
editUser(editUser: User, id: string): Observable<string> {
return null;
}
}
139 changes: 139 additions & 0 deletions server/src/test/java/umm3601/user/UserControllerSpec.java
Original file line number Diff line number Diff line change
@@ -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<Document> userDocuments = db.getCollection("users");
userDocuments.drop();
List<Document> 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 {

}
}

0 comments on commit 3b8003c

Please sign in to comment.