diff --git a/README.md b/README.md deleted file mode 100644 index 62e98da..0000000 --- a/README.md +++ /dev/null @@ -1,239 +0,0 @@ -# Bean Flavored Classroom-OOP Lab -* **Objective** - to register a `Classroom` bean which _mediates_ a _composite_ `Students` and `Instructors` bean reference. -* **Purpose** - to demonstrate the use of - * Bean registration - * Dependency Injection - * IOC Container - * `AnnotationConfigApplicationContext` - * Annotations - * `@Bean` - * `@DependsOn` - * `@Autowired` - * `@PostConstruct` - * `@Config` - * `@SpringBootTest` - * `@Qualifier` - - -## Developmental Notes -* Do NOT clone this repository until completing `Part 0.0` -* You may structure this project and the packaging how you please, however keep in mind that `@Configuration` scans from current directory down. -* Until `Part 10`, this project is nearly identical to the `Classroom-OOP` completed in the past. - -### Part 0.0 - Generating Project -* Navigate to [start.spring.io](https://start.spring.io/) -* In the `Search for Dependencies` input box search for - * `DevTools` - * `Web` -* Select `Generate Project` -* After the project has completed downloading, navigate to the download directory and unzip the project folder. -* At the root directory of the project, clone this repository. -* Move all files from the `demo` project generated by `start.spring.io`, into the newly cloned `learnerlab` folder. -* Open the project via its `pom.xml` from IntelliJ > File > Open - * Be sure to `Open as Project` when prompted - -### Part 1.0 - Create `Person` Class -* Create a `Person` class. - * The class should declare a `final` field named `id` of type `long`. - * The class should declare a field named `name` of type `String`. - * `Person` constructor should have a parameter of type `Long id, String name` which sets each of the fields to their respective value. - * The class should define a `getId()` method which returns the `Person` object's `id` field. - * The class should define a `getName()` method which returns the `Person` object's `name` field. - * The class should define a `setName()` method which sets the `Person` object's `name` field. - -- -### Part 2.0 - Create `Learner` Interface -* Create a `Learner` interface. - * `Learner` should declare one method signature: - * Method name: `learn` - * Method parameters: `double numberOfHours` - * Method return-type: `void` - -- -### Part 3.0 - Create `Student` Class -* Create a `Student` class such that: - * `Student` is a subclass of `Person` - * `Student` implements the `Learner` interface - * `Student` should have an instance variable `totalStudyTime` of type `double` - * `Student` should have a concrete implementation of the `learn` method which increments the `totalStudyTime` variable by the specified `numberOfHours` argument. - * `Student` should have a `getTotalStudyTime()` method which returns the `totalStudyTime` instance variable. - - -- -### Part 4.0 - Create `Teacher` Interface -* Create a `Teacher` interface. - * `Teacher` should declare a `teach` method signature: - * Method name: `teach` - * Method parameters: - * `Learner learner` - * `double numberOfHours` - * Method return-type: `void` - - * `Teacher` should declare a `lecture` method signature: - * Method name: `lecture` - * Method parameters: - * `Iterable learners` - * `double numberOfHours` - * Method return-type: `void` - - -- -### Part 5.0 - Create `Instructor` Class -* Create an `Instructor` class such that: - * `Instructor` is a subclass of `Person` - * `Instructor` implements the `Teacher` interface - * `Instructor` should have a concrete implementation of the `teach` method which invokes the `learn` method on the specified `Learner` object. - * `Instructor` should have a concrete implementation of the `lecture` method which invokes the `learn` method on each of the elements in the specified array of `Learner` objects. - * `numberOfHours` should be evenly split amongst the learners. - * `double numberOfHoursPerLearner = numberOfHours / learners.length;` - -### Part 6.0 - Create `People` -* Create an abstract `People` class - * The class signature should be parameterized with `PersonType` such that `PersonType` is a sub class of `Person`. - * The class should implement `Iterable` of type `PersonType`. - * Upon construction `People` should consume a `List` of `PersonType` and set it to a respective `personList` field. - * The class should define a constructor which consumes a variable number of `PersonType` objects and sets the `personList` field respectively. - * The class should define a method named `add` which adds a `PersonType` to the `personList`. - * The class should define a method named `remove` which removes a `PersonType` from the `personList`. - * The class should define a method named `size` which returns the size of `personList`. - * The class should define a method named `clear` which clears our `personList` field. - * The class should define a method named `addAll` which adds an `Iterable` of `PersonType` objects to the composite `personList`. - * This method will **NOT** take an argument of `Collection` - * The class should define a method named `findById` which makes use of a `long id` parameter to return a `PersonType` object with the respective `id` field. - * The class should define a method named `findAll` which returns the composite `personList`. - - -- -### Part 7.0 - Create `Students` -* Create a `Students` class. - * The class should be a subclass of `People` of parameterized type `Student`. - * The class should consume a variable number of `Student` objects upon construction and pass them to the super constructor. - -- -### Part 8.0 - Create `Instructors` -* Create a `Instructors` class. - * The class should be a subclass of `People` of parameterized type `Instructor`. - * The class should consume a variable number of `Instructor` objects upon construction and pass them to the super constructor. - - - -- -### Part 9.0 - Create `Classroom` -* Create a `Classroom` class. - * The class should consume and set composite reference to an `Instructors` and `Students` object upon construction - * The class should define a method `hostLecture` which makes use of a `Teacher teacher, double numberOfHours` parameter to host a `lecture` to the composite `personList` field in the `students` reference. - - -- -## Part 10.0 - Creating `Configuration` classes -* Each of the following `Config` classes should have a class-signature annotation of `@Configuration` - * this annotation tells spring to scan for `@Bean` definitions within the scope of the class, and register them to the [IOC Container](https://www.tutorialspoint.com/spring/spring_ioc_containers.htm) for `Inject` and `Autowire` use later. -* Classroom should define getters for each of its fields. - -### Developmental Notes -* `@Autowired` - * injects bean by type - * can be used alone. - * If is used alone, it will be wired by type - * If more than one bean of same type are declared in the container `@Autowired` does not know which beans to use for injection. - - -* `@Qualifier` - * injects bean by specified name - * supports fields, setter, constructors and multi-argument methods injection - * together with `@Autowired`, clarifies which beans to be wired by specifying the bean name (wired by name) - - - - -- -### Part 10.1 - Create `StudentConfig` -* **Note:** The creation of this class will demonstrate an implementation of _bean registration_ in Spring. -* The class should define a method named `currentStudents()` which returns a `Students` representative of the current cohort of students. - * the method should be annotated with `@Bean(name = "students")` - * this ensures the Spring container registers the bean with the respective name. - * a `@Bean` whose `name` attribute is not specified defaults to the name of the method it is annotating. -* The class should define a bean named `previousStudents()` which returns a `Students` representative of the previous cohort of students. - -- -### Part 10.2 - Create `InstructorsConfig` -* The class should define a bean named `delTechTuesdayInstructors()` which returns an `Instructors` representative of the DelTech Tuesday instructors. -* The class should define a bean named `delTechThursdayInstructors()` which returns an `Instructors` representative of the DelTech Thursday instructors. -* The class should define a bean named `instructors` which returns all `Instructors` employed at DelTech - * annotate this bean with `@Primary` - * this ensures Spring will inject this bean in the case that an `Autowire` annotation is not supplied with a `Qualifier` annotation - - - -- -### Part 10.3 - Create `ClassroomConfig` -* The class should define a bean named `currentCohort()` which returns a `Classroom` object whose dependencies are `instructors` and `students` -* The class should define a bean named `previousCohort()` which returns an `Classroom` object whose dependencies are `instructors` and `previousStudents` -* **Note:** [it is sometimes useful](https://www.boraji.com/spring-dependson-annotation-example) (although not always necessary) to use the `@DependsOn` annotation to help the Spring framework and other readers of the code to understand what order beans should be executed. - * `@DependsOn({"instructors", "students"})` - - - - -- -## Part 11.0 - Test `Config` classes -* Each of the following `Test` classes should be annotated with - * `@RunWith(SpringRunner.class)` - * enforces which strategy `junit` should use to run tests - * `@SpringBootTest` - * indicates that this class is a Spring Boot test class - * provides support to scan for a `ContextConfiguration` that tells the test class how to load the `ApplicationContext`. - * If no `ContextConfiguration` classes are specified as a parameter to the `@SpringBootTest` annotation, the default behavior is to load the `ApplicationContext` by scanning for a `@SpringBootConfiguration` annotation on a class in the package root. -* Each bean can be injected into the class scope using `@Autowired` along with `@Qualifier(name = "beanname")` - - - -- -### Part 11.1 - Test `StudentConfig` Class -* Create a `TestStudentConfig` class in the `test` package. -* The class should ensure that each `Bean` in the `StudentConfig` class is configured as expected. -* **Tip:** You can use the `toString` method to get a representation of the aggregate state of any `People` object. - - -- -### Part 11.2 - Test `InstructorConfig` Class -* Create a `TestInstructorConfig` class in the `test` package. -* The class should ensure that each `Bean` in the `TestInstructorConfig` class is configured as expected. - - -- -### Part 11.3 - Test `ClassroomConfig` Class -* Create a `TestClassroomConfig` class in the `test` package. -* The class should ensure that each `Bean` in the `TestClassroomConfig` class is configured as expected. - - - - - - - -## Part 11.0 - Using `@Component` -* Annotating a class signature with `@Component` allows Spring to register the class as a `Bean` implicitly. - - -- -### Part 11.1 - Create `Alumni` Class -* Create an `Alumni` component which autowires `Students` of the previous cohort and `Instructors` -* Create an `executeBootcamp` method which teaches each `Student` in the composite `Students` a `totalNumberOfHours` of `1200`. - * Annotate this method with `@PostConstruct` - * denotes that this method must be executed before the class is put into an IoC container -* Create a getter for each of the fields. - -- -### Part 11.2 - Test `Alumni` Class -* Write a test class which ensures that each `Student` in the `Alumni` class has been taught `1200` hours upon injection of the `Alumni` dependency. -* Ensure the `numberOfHoursTaught` has been evenly distributed amongst each of the instructors. - -* **Tip:** How to derive `numberOfHoursTaught` dynamically -```java -int numberOfInstructors = instructors.size(); -int numberOfStudents = students.size(); -double numberOfHoursToTeachEachStudent = 1200; -double numberOfHoursToTeach = numberOfHoursToTeachEachStudent * numberOfStudents; -double numberOfHoursPerInstructor = numberOfHoursToTeach / numberOfInstructors; -``` diff --git a/src/main/java/com/classroom/Alumni.java b/src/main/java/com/classroom/Alumni.java new file mode 100644 index 0000000..c84fe19 --- /dev/null +++ b/src/main/java/com/classroom/Alumni.java @@ -0,0 +1,39 @@ +package com.classroom; + +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; + +@Component +public class Alumni { + + final + Classroom prevCohort; + + + public Alumni(@Qualifier("previousCohort") Classroom prevCohort) { + this.prevCohort = prevCohort; + } + + @PostConstruct + public void executeBootcamp(){ + int numberOfInstructors = prevCohort.getInstructors().size(); + int numberOfStudents = prevCohort.getStudents().size(); + + double numberOfHoursToTeachEachStudent = 1200; + double numberOfHoursToTeach = numberOfHoursToTeachEachStudent * numberOfStudents; //1200*4 = 4800 + double numberOfHoursPerInstructor = numberOfHoursToTeach / numberOfInstructors; //4800/2 = 2400 + + for(Instructor teach: prevCohort.getInstructors()){ + for(Student stud: prevCohort.getStudents()){ + teach.teach(stud, numberOfHoursPerInstructor); + } + } + } + + public Classroom getPrevCohort() { + return prevCohort; + } + +} diff --git a/src/main/java/com/classroom/Classroom.java b/src/main/java/com/classroom/Classroom.java new file mode 100644 index 0000000..a3e569a --- /dev/null +++ b/src/main/java/com/classroom/Classroom.java @@ -0,0 +1,26 @@ +package com.classroom; + +import java.util.List; + +public class Classroom { + + private List Instructors; + private List Students; + + public Classroom(List persons, List students){ + this.Instructors = persons; + this.Students = students; + } + + public void hostLecture(Teacher teacher, double numberOfHours){ + teacher.lecture(Students, numberOfHours); + } + + public List getInstructors() { + return Instructors; + } + + public List getStudents() { + return Students; + } +} diff --git a/src/main/java/com/classroom/ClassroomApplication.java b/src/main/java/com/classroom/ClassroomApplication.java new file mode 100644 index 0000000..9796f5f --- /dev/null +++ b/src/main/java/com/classroom/ClassroomApplication.java @@ -0,0 +1,13 @@ +package com.classroom; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ClassroomApplication { + + public static void main(String[] args) { + SpringApplication.run(ClassroomApplication.class, args); + } + +} diff --git a/src/main/java/com/classroom/ClassroomConfig.java b/src/main/java/com/classroom/ClassroomConfig.java new file mode 100644 index 0000000..5a4fbb4 --- /dev/null +++ b/src/main/java/com/classroom/ClassroomConfig.java @@ -0,0 +1,45 @@ +package com.classroom; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.ArrayList; +import java.util.List; + +@Configuration +public class ClassroomConfig { + + @Bean(name = "currentCohort") + public Classroom currentCohort(){ + + List instructors = new ArrayList<>(); + List students = new ArrayList<>(); + + instructors.add(new Instructor(100l, "Zan")); + instructors.add(new Instructor(101l, "Brian")); + + students.add(new Student(1l, "Cedric")); + students.add(new Student(2l, "Lolu")); + students.add(new Student(3l, "Apoorva")); + students.add(new Student(4l, "Stephen")); + + return new Classroom(instructors, students); + } + + @Bean(name = "previousCohort") + public Classroom previousCohort(){ + + List instructors = new ArrayList<>(); + List students = new ArrayList<>(); + + instructors.add(new Instructor(102l, "Kaleb")); + instructors.add(new Instructor(103l, "Tutor")); + + students.add(new Student(5l, "Charu")); + students.add(new Student(6l, "Robert")); + students.add(new Student(7l, "user iphone 83")); + students.add(new Student(8l, "Jessica")); + + return new Classroom(instructors, students); + } +} diff --git a/src/main/java/com/classroom/Instructor.java b/src/main/java/com/classroom/Instructor.java new file mode 100644 index 0000000..82ce24b --- /dev/null +++ b/src/main/java/com/classroom/Instructor.java @@ -0,0 +1,29 @@ +package com.classroom; + +public class Instructor extends Person implements Teacher { + + public Instructor(){ + this(null, null); + } + + public Instructor(Long ID, String name){ + super(ID, name); + } + + @Override + public void teach(Learner learner, double numberOfHours) { + learner.learn(numberOfHours); + } + + @Override + public void lecture(Iterable learners, double numberOfHours) { + Integer numOfStudents =0; + for(Learner s:learners){ + numOfStudents += 1; + } + + for(Learner s:learners){ + s.learn(numberOfHours/numOfStudents); + } + } +} diff --git a/src/main/java/com/classroom/Instructors.java b/src/main/java/com/classroom/Instructors.java new file mode 100644 index 0000000..f13ce23 --- /dev/null +++ b/src/main/java/com/classroom/Instructors.java @@ -0,0 +1,11 @@ +package com.classroom; + +import java.util.List; + +public class Instructors extends People { + + public Instructors(List persons) { + super(persons); + } + +} diff --git a/src/main/java/com/classroom/InstructorsConfig.java b/src/main/java/com/classroom/InstructorsConfig.java new file mode 100644 index 0000000..382c115 --- /dev/null +++ b/src/main/java/com/classroom/InstructorsConfig.java @@ -0,0 +1,47 @@ +package com.classroom; + + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; + +import java.util.ArrayList; +import java.util.List; + +@Configuration +public class InstructorsConfig { + + @Bean(name = "tuesdayInstructors") + public Instructors delTechTuesdayInstructors(){ + List tuesInstruc= new ArrayList<>(); + + tuesInstruc.add(new Instructor(100l, "Zan")); + tuesInstruc.add(new Instructor(101l, "Brian")); + + return new Instructors(tuesInstruc); + } + + @Bean(name = "thursdayInstructors") + public Instructors delTechThursdayInstructors(){ + List thursInstruc= new ArrayList<>(); + + thursInstruc.add(new Instructor(102l, "Kaleb")); + thursInstruc.add(new Instructor(103l, "Tutor")); + + return new Instructors(thursInstruc); + } + + @Primary + @Bean(name ="allInstructors") + public Instructors instructors(){ + Instructors instructors = new Instructors(new ArrayList<>()); + + instructors.add(new Instructor(100l, "Zan")); + instructors.add(new Instructor(101l, "Brian")); + instructors.add(new Instructor(102l, "Kaleb")); + instructors.add(new Instructor(103l, "Tutor")); + + + return instructors; + } +} diff --git a/src/main/java/com/classroom/Learner.java b/src/main/java/com/classroom/Learner.java new file mode 100644 index 0000000..830e491 --- /dev/null +++ b/src/main/java/com/classroom/Learner.java @@ -0,0 +1,6 @@ +package com.classroom; + +public interface Learner { + + public void learn(double numberOfHours); +} diff --git a/src/main/java/com/classroom/People.java b/src/main/java/com/classroom/People.java new file mode 100644 index 0000000..68b9220 --- /dev/null +++ b/src/main/java/com/classroom/People.java @@ -0,0 +1,54 @@ +package com.classroom; + +import java.util.Iterator; +import java.util.List; + +public abstract class People implements Iterable{ + + public List personList; + + public People(List persons){ + this.personList = persons; + } + + public void add(PersonType person){ + this.personList.add(person); + } + + public void remove(PersonType person){ + this.personList.remove(person); + } + + public int size(){ + return this.personList.size(); + } + + public void clear(){ + this.personList.clear(); + } + + public void addAll(){ + for(PersonType p:this){ + personList.add(p); + } + } + + public Person findById(long id){ + Person person = new Person(); + for(PersonType p:personList){ + if(p.getID() == id){ + return p; + } + } + return person; + } + + public List findAll(){ + return personList; + } + + @Override + public Iterator iterator() { + return personList.iterator(); + } +} diff --git a/src/main/java/com/classroom/Person.java b/src/main/java/com/classroom/Person.java new file mode 100644 index 0000000..469f9fc --- /dev/null +++ b/src/main/java/com/classroom/Person.java @@ -0,0 +1,28 @@ +package com.classroom; + +public class Person { + + private final Long ID; + private String name; + + public Person(){ + this(null, null); + } + + public Person(Long id, String name){ + ID = id; + this.name = name; + } + + public Long getID() { + return ID; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/main/java/com/classroom/Student.java b/src/main/java/com/classroom/Student.java new file mode 100644 index 0000000..ea0cda8 --- /dev/null +++ b/src/main/java/com/classroom/Student.java @@ -0,0 +1,23 @@ +package com.classroom; + +public class Student extends Person implements Learner{ + + public double totalStudyTime = 0; + + public Student(){ + this(null, null); + } + + public Student(Long id, String name){ + super(id, name); + } + + @Override + public void learn(double numberOfHours) { + totalStudyTime += numberOfHours; + } + + public double getTotalStudyTime() { + return totalStudyTime; + } +} diff --git a/src/main/java/com/classroom/StudentConfig.java b/src/main/java/com/classroom/StudentConfig.java new file mode 100644 index 0000000..5446fcd --- /dev/null +++ b/src/main/java/com/classroom/StudentConfig.java @@ -0,0 +1,37 @@ +package com.classroom; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.ArrayList; +import java.util.List; + +@Configuration +public class StudentConfig { + + @Bean(name = "students") + public Students currentStudents(){ + + List students= new ArrayList<>(); + + students.add(new Student(1l, "Cedric")); + students.add(new Student(2l, "Lolu")); + students.add(new Student(3l, "Apoorva")); + students.add(new Student(4l, "Stephen")); + + return new Students(students); + } + + @Bean(name = "previousStudents") + public Students previousStudents(){ + List students= new ArrayList<>(); + + students.add(new Student(5l, "Charu")); + students.add(new Student(6l, "Robert")); + students.add(new Student(7l, "user iphone 83")); + students.add(new Student(8l, "Jessica")); + + return new Students(students); + } + +} diff --git a/src/main/java/com/classroom/Students.java b/src/main/java/com/classroom/Students.java new file mode 100644 index 0000000..0feb9c4 --- /dev/null +++ b/src/main/java/com/classroom/Students.java @@ -0,0 +1,10 @@ +package com.classroom; + +import java.util.List; + +public class Students extends People{ + + public Students(List persons) { + super(persons); + } +} diff --git a/src/main/java/com/classroom/Teacher.java b/src/main/java/com/classroom/Teacher.java new file mode 100644 index 0000000..0398fd7 --- /dev/null +++ b/src/main/java/com/classroom/Teacher.java @@ -0,0 +1,7 @@ +package com.classroom; + +public interface Teacher { + + public void teach(Learner learner, double numberOfHours); + public void lecture(Iterable learners, double numberOfHours); +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/src/test/java/com/classroom/ClassroomApplicationTests.java b/src/test/java/com/classroom/ClassroomApplicationTests.java new file mode 100644 index 0000000..d9eefdb --- /dev/null +++ b/src/test/java/com/classroom/ClassroomApplicationTests.java @@ -0,0 +1,13 @@ +package com.classroom; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class ClassroomApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/src/test/java/com/classroom/ClassroomConfigTest.java b/src/test/java/com/classroom/ClassroomConfigTest.java new file mode 100644 index 0000000..63fd8dd --- /dev/null +++ b/src/test/java/com/classroom/ClassroomConfigTest.java @@ -0,0 +1,53 @@ +package com.classroom; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ClassroomConfigTest { + + @Autowired + ClassroomConfig config; + + @Autowired + @Qualifier("currentCohort") + Classroom curCohort; + + @Autowired + @Qualifier("previousCohort") + Classroom preCohort; + + @Test + public void testCurrentCohort(){ + + Assert.assertEquals("Zan", curCohort.getInstructors().get(0).getName()); + Assert.assertEquals("Brian", curCohort.getInstructors().get(1).getName()); + + Assert.assertEquals("Cedric", curCohort.getStudents().get(0).getName()); + Assert.assertEquals("Lolu", curCohort.getStudents().get(1).getName()); + Assert.assertEquals("Apoorva", curCohort.getStudents().get(2).getName()); + Assert.assertEquals("Stephen", curCohort.getStudents().get(3).getName()); + } + + @Test + public void testPreviousCohort(){ + + Assert.assertEquals("Kaleb", preCohort.getInstructors().get(0).getName()); + Assert.assertEquals("Tutor", preCohort.getInstructors().get(1).getName()); + + Assert.assertEquals("Charu", preCohort.getStudents().get(0).getName()); + Assert.assertEquals("Robert", preCohort.getStudents().get(1).getName()); + Assert.assertEquals("user iphone 83", preCohort.getStudents().get(2).getName()); + Assert.assertEquals("Jessica", preCohort.getStudents().get(3).getName()); + + Assert.assertEquals("102", preCohort.getInstructors().get(0).getID().toString()); + Assert.assertEquals("5", preCohort.getStudents().get(0).getID().toString()); + } + +} diff --git a/src/test/java/com/classroom/InstructorConfigTest.java b/src/test/java/com/classroom/InstructorConfigTest.java new file mode 100644 index 0000000..ee0baf8 --- /dev/null +++ b/src/test/java/com/classroom/InstructorConfigTest.java @@ -0,0 +1,50 @@ +package com.classroom; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class InstructorConfigTest { + + @Autowired + InstructorsConfig config; + + @Autowired + @Qualifier("tuesdayInstructors") + Instructors TInstructors; + + @Autowired + @Qualifier("thursdayInstructors") + Instructors TRInstructors; + + @Autowired + @Qualifier("allInstructors") + Instructors allInstructors; + + @Test + public void testTuesdayInstructors(){ + Assert.assertEquals("Zan", TInstructors.findById(100).getName()); + Assert.assertEquals("Brian", TInstructors.findById(101).getName()); + } + + @Test + public void testThursdayInstructors(){ + Assert.assertEquals("Kaleb", TRInstructors.findById(102).getName()); + Assert.assertEquals("Tutor", TRInstructors.findById(103).getName()); + } + + @Test + public void testAllInstructors(){ + Assert.assertEquals("Zan", allInstructors.findById(100).getName()); + Assert.assertEquals("Brian", allInstructors.findById(101).getName()); + Assert.assertEquals("Kaleb", allInstructors.findById(102).getName()); + Assert.assertEquals("Tutor", allInstructors.findById(103).getName()); + } + +} diff --git a/src/test/java/com/classroom/PersonTest.java b/src/test/java/com/classroom/PersonTest.java new file mode 100644 index 0000000..a47ff39 --- /dev/null +++ b/src/test/java/com/classroom/PersonTest.java @@ -0,0 +1,18 @@ +package com.classroom; + +import junit.framework.Assert; + +import org.junit.jupiter.api.Test; + + +public class PersonTest { + + @Test + public void createPersonTest(){ + Person ced = new Person(1L,"Cedric"); + + long expectedID = ced.getID(); + Assert.assertEquals(1L, expectedID); + Assert.assertEquals("Cedric", ced.getName()); + } +} diff --git a/src/test/java/com/classroom/StudentConfigTest.java b/src/test/java/com/classroom/StudentConfigTest.java new file mode 100644 index 0000000..1e2648e --- /dev/null +++ b/src/test/java/com/classroom/StudentConfigTest.java @@ -0,0 +1,43 @@ +package com.classroom; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.test.context.SpringBootTest; + +import org.springframework.test.context.junit4.SpringRunner; + + +@RunWith(SpringRunner.class) +@SpringBootTest +public class StudentConfigTest { + + @Autowired + StudentConfig config; + + @Autowired + @Qualifier("students") + Students currentStudents; + + @Autowired + @Qualifier("previousStudents") + Students previousStudents; + + @Test + public void testCurrentStudents(){ + Assert.assertEquals("Cedric", currentStudents.findById(1l).getName()); + Assert.assertEquals("Lolu", currentStudents.findById(2l).getName()); + Assert.assertEquals("Apoorva", currentStudents.findById(3l).getName()); + Assert.assertEquals("Stephen", currentStudents.findById(4l).getName()); + } + + @Test + public void testPastStudents(){ + Assert.assertEquals("Charu", previousStudents.findById(5l).getName()); + Assert.assertEquals("Robert", previousStudents.findById(6l).getName()); + Assert.assertEquals("user iphone 83", previousStudents.findById(7l).getName()); + Assert.assertEquals("Jessica", previousStudents.findById(8l).getName()); + } +} diff --git a/src/test/java/com/classroom/alumniTest.java b/src/test/java/com/classroom/alumniTest.java new file mode 100644 index 0000000..069a14d --- /dev/null +++ b/src/test/java/com/classroom/alumniTest.java @@ -0,0 +1,37 @@ +package com.classroom; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class alumniTest { + + @Autowired + Alumni clazz; + + @Test + public void teachAlumTest(){ + //All students should have 4800 of hours + for(Student stud:clazz.getPrevCohort().getStudents()){ + Assert.assertEquals(4800, (int)stud.getTotalStudyTime()); + } + } + + @Test + public void classStudentSizeTest(){ + int expectedSize = clazz.getPrevCohort().getStudents().size(); + Assert.assertEquals(4, expectedSize); + } + + @Test + public void classInstructorSizeTest(){ + int expectedSize = clazz.getPrevCohort().getInstructors().size(); + Assert.assertEquals(2, expectedSize); + } + +} diff --git a/src/test/java/com/classroom/classRoomTests.java b/src/test/java/com/classroom/classRoomTests.java new file mode 100644 index 0000000..14ea48a --- /dev/null +++ b/src/test/java/com/classroom/classRoomTests.java @@ -0,0 +1,37 @@ +package com.classroom; + +import junit.framework.Assert; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; +/* +This test encompasses all major classes, by testing class room you are +testing Person, Instrucor, Student, Learner interface, Teacher itnerface, +and classroom + */ + +public class classRoomTests { + + @Test + public void teachAllTest(){ + List students = new ArrayList(); + List teacher = new ArrayList(); + + Instructor zan = new Instructor(0L, "Zan"); + teacher.add(zan); + + Student ced = new Student(1l, "Cedric"); + Student lol = new Student(2L, "Lolo"); + students.add(ced); + students.add(lol); + + Classroom clazz = new Classroom(teacher, students); + + clazz.hostLecture(zan, 2); + + Assert.assertEquals(1.0, ced.getTotalStudyTime()); + Assert.assertEquals(1.0, lol.getTotalStudyTime()); + } +}