Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Jan Smejkal
Pa165 Secret Archive
Commits
4390dc9b
Commit
4390dc9b
authored
Apr 24, 2022
by
Milan Mozolák
Browse files
Added mission service method to get missions by agent
parent
467c88cf
Pipeline
#131888
waiting for manual action with stage
Changes
4
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
secret-archive-dao/src/main/java/cz/fi/muni/pa165/seminar4/group7/entity/Mission.java
View file @
4390dc9b
...
...
@@ -8,6 +8,7 @@ import lombok.experimental.Accessors;
import
javax.persistence.*
;
import
java.sql.Date
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
...
...
@@ -39,7 +40,7 @@ public class Mission extends BaseEntity {
@OneToMany
(
mappedBy
=
"mission"
)
@Setter
(
AccessLevel
.
NONE
)
private
List
<
Resource
>
resources
;
private
List
<
Resource
>
resources
=
new
ArrayList
<>()
;
@ManyToOne
(
cascade
=
CascadeType
.
ALL
)
@JoinColumn
(
name
=
"country_id"
)
...
...
@@ -48,7 +49,7 @@ public class Mission extends BaseEntity {
@OneToMany
(
mappedBy
=
"mission"
)
@Setter
(
AccessLevel
.
NONE
)
private
List
<
AgentAssignment
>
agentAssignments
;
private
List
<
AgentAssignment
>
agentAssignments
=
new
ArrayList
<>()
;
public
void
addResource
(
Resource
resource
)
{
resources
.
add
(
resource
);
...
...
secret-archive-service/src/main/java/cz/fi/muni/pa165/seminar4/group7/MissionService.java
View file @
4390dc9b
package
cz.fi.muni.pa165.seminar4.group7
;
import
cz.fi.muni.pa165.seminar4.group7.entity.Agent
;
import
cz.fi.muni.pa165.seminar4.group7.entity.Mission
;
import
java.util.Optional
;
...
...
@@ -45,4 +46,12 @@ public interface MissionService {
* @param mission updated mission.
*/
void
update
(
Mission
mission
);
/**
* Find all missions where agent is assigned
*
* @param agent to find by
* @return list of missions
*/
Iterable
<
Mission
>
findAllByAgent
(
Agent
agent
);
}
secret-archive-service/src/main/java/cz/fi/muni/pa165/seminar4/group7/MissionServiceImpl.java
View file @
4390dc9b
package
cz.fi.muni.pa165.seminar4.group7
;
import
cz.fi.muni.pa165.seminar4.group7.dao.MissionDao
;
import
cz.fi.muni.pa165.seminar4.group7.entity.Agent
;
import
cz.fi.muni.pa165.seminar4.group7.entity.AgentAssignment
;
import
cz.fi.muni.pa165.seminar4.group7.entity.Mission
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.util.ArrayList
;
import
java.util.Optional
;
import
java.util.stream.Collectors
;
/**
* @author Milan Mozolak
...
...
@@ -53,4 +57,18 @@ public class MissionServiceImpl implements MissionService {
}
missionDao
.
save
(
mission
);
}
@Override
public
Iterable
<
Mission
>
findAllByAgent
(
Agent
agent
)
{
var
toReturn
=
new
ArrayList
<
Mission
>();
for
(
var
mission
:
missionDao
.
findAll
())
{
if
(
mission
.
getAgentAssignments
().
stream
()
.
map
(
AgentAssignment:
:
getAgent
)
.
collect
(
Collectors
.
toList
())
.
contains
(
agent
))
{
toReturn
.
add
(
mission
);
}
}
return
toReturn
;
}
}
secret-archive-service/src/test/java/cz/fi/muni/pa165/seminar4/group7/MissionServiceTest.java
View file @
4390dc9b
package
cz.fi.muni.pa165.seminar4.group7
;
import
cz.fi.muni.pa165.seminar4.group7.dao.MissionDao
;
import
cz.fi.muni.pa165.seminar4.group7.entity.Agent
;
import
cz.fi.muni.pa165.seminar4.group7.entity.AgentAssignment
;
import
cz.fi.muni.pa165.seminar4.group7.entity.Mission
;
import
org.junit.jupiter.api.Test
;
import
org.mockito.InjectMocks
;
...
...
@@ -94,4 +96,21 @@ public class MissionServiceTest {
assertThatExceptionOfType
(
IllegalArgumentException
.
class
).
isThrownBy
(()
->
missionService
.
update
(
mission1
));
verify
(
missionDao
,
never
()).
save
(
mission1
);
}
@Test
void
findByAgent
()
{
var
agent
=
new
Agent
();
var
assignment
=
new
AgentAssignment
();
assignment
.
setAgent
(
agent
);
mission1
.
addAgentAssignment
(
assignment
);
when
(
missionDao
.
findAll
()).
thenReturn
(
List
.
of
(
mission1
,
mission2
));
assertThat
(
missionService
.
findAllByAgent
(
agent
)).
containsExactly
(
mission1
);
}
@Test
void
findByAgentNonExisting
()
{
var
agent
=
new
Agent
();
when
(
missionDao
.
findAll
()).
thenReturn
(
List
.
of
(
mission1
,
mission2
));
assertThat
(
missionService
.
findAllByAgent
(
agent
)).
isEmpty
();
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment