Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Eclipse Projects
Eclipse openK User Modules
org.eclipse.openk-usermodules.gridFailureInformation.backend
Commits
1cc6e89c
Commit
1cc6e89c
authored
Mar 19, 2020
by
Ina Curdt
Browse files
SI-283: Testklassen erstellt
parent
a9984fea
Changes
14
Hide whitespace changes
Inline
Side-by-side
testImportGridFailures/src/main/java/org/eclipse/openk/gridfailureinformation/jobs/importgridfailures/config/EventProducerConfig.java
View file @
1cc6e89c
package
org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.config
;
import
lombok.extern.log4j.Log4j2
;
import
org.springframework.amqp.core.DirectExchange
;
import
org.springframework.amqp.core.Exchange
;
import
org.springframework.amqp.core.*
;
import
org.springframework.amqp.rabbit.core.RabbitTemplate
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
...
...
@@ -19,16 +18,19 @@ import org.springframework.messaging.MessageHandler;
@Log4j2
public
class
EventProducerConfig
{
@Value
(
"${rabbitmq
config
.routing
-
key}"
)
@Value
(
"${
spring.
rabbitmq.routingkey}"
)
public
String
routingKey
;
@Value
(
"${rabbitmq
config
.exchange
-
name}"
)
@Value
(
"${
spring.
rabbitmq.exchangename}"
)
public
String
exchangeName
;
@Value
(
"${spring.rabbitmq.queuename}"
)
public
String
queueName
;
@Autowired
RabbitTemplate
rabbitTemplate
;
@Bean
public
Exchange
e
ventE
xchange
()
{
public
Direct
Exchange
exchange
()
{
return
new
DirectExchange
(
exchangeName
);
}
...
...
@@ -37,9 +39,17 @@ public class EventProducerConfig {
return
new
DirectChannel
();
}
@Bean
public
Queue
messageImportQueue
()
{
return
new
Queue
(
queueName
);
}
@Bean
public
Binding
binding
()
{
return
BindingBuilder
.
bind
(
messageImportQueue
()).
to
(
exchange
()).
with
(
routingKey
);
}
@Bean
@ServiceActivator
(
inputChannel
=
"messageImportChannel"
)
public
MessageHandler
messageHandler
()
{
return
message
->
rabbitTemplate
.
convertAndSend
(
exchangeName
,
routingKey
,
message
.
getPayload
(),
message1
->
{
message1
.
getMessageProperties
().
getHeaders
().
put
(
"metaId"
,
message
.
getHeaders
().
get
(
"metaId"
));
message1
.
getMessageProperties
().
getHeaders
().
put
(
"description"
,
message
.
getHeaders
().
get
(
"description"
));
...
...
testImportGridFailures/src/main/java/org/eclipse/openk/gridfailureinformation/jobs/importgridfailures/controller/ImportController.java
View file @
1cc6e89c
...
...
@@ -29,32 +29,17 @@ import org.springframework.web.bind.annotation.*;
@RequestMapping
(
"/import"
)
public
class
ImportController
{
@Autowired
private
ImportService
importService
;
@PostMapping
@ApiOperation
(
value
=
"Import einer externen Störungsinformation"
)
@ApiResponses
(
value
=
{
@ApiResponse
(
code
=
201
,
message
=
"Störungsinformation erfolgreich importiert"
),
@ApiResponse
(
code
=
500
,
message
=
"Konnte nicht durchgeführt werden"
)
})
public
void
pushForeignFailureInfo
(
// public ResponseEntity<ForeignFailureDto> pushForeignFailureInfo(
@Validated
@RequestBody
ForeignFailureDto
foreignFailureDto
)
{
ForeignFailureDto
savedgfDto
=
importService
.
pushForeignFailure
(
foreignFailureDto
);
// URI location = ServletUriComponentsBuilder
// .fromCurrentRequestUri()
// .path("/{uuid}")
// .buildAndExpand(foreignFailureDto.getUuid())
// .toUri();
// return ResponseEntity.created(location).body(savedgfDto);
}
public
void
pushForeignFailureInfo
(
@Validated
@RequestBody
ForeignFailureDto
foreignFailureDto
)
{
importService
.
pushForeignFailure
(
foreignFailureDto
);
}
}
testImportGridFailures/src/main/java/org/eclipse/openk/gridfailureinformation/jobs/importgridfailures/exceptions/BadRequestException.java
0 → 100644
View file @
1cc6e89c
/*
*******************************************************************************
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
*/
package
org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.exceptions
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
@ResponseStatus
(
code
=
HttpStatus
.
BAD_REQUEST
)
public
class
BadRequestException
extends
RuntimeException
{
public
BadRequestException
()
{
}
public
BadRequestException
(
String
message
)
{
super
(
message
);
}
}
testImportGridFailures/src/main/java/org/eclipse/openk/gridfailureinformation/jobs/importgridfailures/exceptions/InternalServerErrorException.java
0 → 100644
View file @
1cc6e89c
/*
*******************************************************************************
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
*/
package
org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.exceptions
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
@ResponseStatus
(
code
=
HttpStatus
.
INTERNAL_SERVER_ERROR
)
public
class
InternalServerErrorException
extends
RuntimeException
{
public
InternalServerErrorException
(
String
message
)
{
super
(
message
);
}
}
testImportGridFailures/src/main/java/org/eclipse/openk/gridfailureinformation/jobs/importgridfailures/exceptions/NotFoundException.java
0 → 100644
View file @
1cc6e89c
/*
*******************************************************************************
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
*/
package
org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.exceptions
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
@ResponseStatus
(
code
=
HttpStatus
.
NOT_FOUND
)
public
class
NotFoundException
extends
RuntimeException
{
public
NotFoundException
()
{
}
public
NotFoundException
(
String
message
)
{
super
(
message
);
}
}
testImportGridFailures/src/main/java/org/eclipse/openk/gridfailureinformation/jobs/importgridfailures/exceptions/UnauthorizedException.java
0 → 100644
View file @
1cc6e89c
package
org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.exceptions
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
@ResponseStatus
(
code
=
HttpStatus
.
UNAUTHORIZED
)
public
class
UnauthorizedException
extends
RuntimeException
{
public
UnauthorizedException
(
String
message
)
{
super
(
message
);
}
}
testImportGridFailures/src/main/java/org/eclipse/openk/gridfailureinformation/jobs/importgridfailures/service/ImportService.java
View file @
1cc6e89c
...
...
@@ -15,8 +15,10 @@
package
org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.service
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.rabbitmq.client.BuiltinExchangeType
;
import
lombok.extern.log4j.Log4j2
;
import
org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.dtos.ForeignFailureDto
;
import
org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.exceptions.InternalServerErrorException
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
...
...
@@ -24,24 +26,18 @@ import org.springframework.messaging.support.MessageBuilder;
import
org.springframework.stereotype.Service
;
import
org.springframework.messaging.MessageChannel
;
import
java.text.SimpleDateFormat
;
@Service
@Log4j2
@EnableConfigurationProperties
@ConfigurationProperties
(
prefix
=
"rabbitmq"
)
public
class
ImportService
{
private
static
final
String
INDEXING_DATE_FORMAT
=
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"
;
@Autowired
private
MessageChannel
messageImportChannel
;
public
ForeignFailureDto
pushForeignFailure
(
ForeignFailureDto
foreignFailureDto
)
{
public
void
pushForeignFailure
(
ForeignFailureDto
foreignFailureDto
)
{
try
{
//messageImportChannel.send(MessageBuilder.withPayload(new ObjectMapper().setDateFormat(new SimpleDateFormat(INDEXING_DATE_FORMAT)).writeValueAsString(foreignFailureDto))
messageImportChannel
.
send
(
MessageBuilder
.
withPayload
(
foreignFailureDto
.
getPayload
())
.
setHeader
(
"metaId"
,
foreignFailureDto
.
getMetaId
())
.
setHeader
(
"description"
,
foreignFailureDto
.
getDescription
())
...
...
@@ -50,10 +46,9 @@ public class ImportService {
}
catch
(
Exception
e
)
{
log
.
debug
(
e
.
getMessage
(),
e
);
throw
new
InternalServerErrorException
(
"could.not.push.message"
);
}
return
foreignFailureDto
;
}
}
testImportGridFailures/src/main/resources/application.yml
View file @
1cc6e89c
...
...
@@ -22,10 +22,9 @@ spring:
port
:
5672
username
:
guest
password
:
guest
rabbitmqconfig
:
routing-key
:
messageImportChannel.process
exchange-name
:
messageImportChannel
routingkey
:
messageImportChannel.testkey
exchangename
:
messageImportChannelTest
queuename
:
messageImportQueueTest
server
:
max-http-header-size
:
262144
...
...
testImportGridFailures/src/main/resources/application_localdev.yml
View file @
1cc6e89c
...
...
@@ -22,10 +22,9 @@ spring:
port
:
5672
username
:
guest
password
:
guest
rabbitmqconfig
:
routing-key
:
messageImportChannel.process
exchange-name
:
messageImportChannel
routingkey
:
messageImportChannel.testkey
exchangename
:
messageImportChannelTest
queuename
:
messageImportQueueTest
eureka
:
...
...
testImportGridFailures/src/main/resources/messages.properties
0 → 100644
View file @
1cc6e89c
#Fehlermeldungen
could.not.push.message
=
Das Objekt konnte nicht an RabbitMQ gesendet werden.
testImportGridFailures/src/test/java/org/eclipse/openk/gridfailureinformation/jobs/importgridfailures/config/TestConfiguration.java
0 → 100644
View file @
1cc6e89c
/*
*******************************************************************************
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
*/
package
org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.config
;
import
org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.TestImportGridFailuresApplication
;
import
org.springframework.boot.autoconfigure.domain.EntityScan
;
import
org.springframework.boot.test.context.ConfigFileApplicationContextInitializer
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.TestPropertySource
;
@EntityScan
(
basePackageClasses
=
TestImportGridFailuresApplication
.
class
)
@ContextConfiguration
(
initializers
=
{
ConfigFileApplicationContextInitializer
.
class
})
@TestPropertySource
(
"spring.config.location=classpath:application.yml"
)
public
class
TestConfiguration
{
@Bean
public
EventProducerConfig
myEventProducerConfig
()
{
return
new
EventProducerConfig
();}
}
testImportGridFailures/src/test/java/org/eclipse/openk/gridfailureinformation/jobs/importgridfailures/controller/ImportControllerTest.java
0 → 100644
View file @
1cc6e89c
/*
*******************************************************************************
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
*/
package
org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.controller
;
import
org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.TestImportGridFailuresApplication
;
import
org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.dtos.ForeignFailureDto
;
import
org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.service.ImportService
;
import
org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.support.MockDataHelper
;
import
org.junit.jupiter.api.Test
;
import
org.mockito.Mockito
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.mock.mockito.MockBean
;
import
org.springframework.http.MediaType
;
import
org.springframework.test.context.ActiveProfiles
;
import
org.springframework.test.web.servlet.MockMvc
;
import
java.util.List
;
import
static
org
.
mockito
.
ArgumentMatchers
.
any
;
import
static
org
.
mockito
.
ArgumentMatchers
.
anyString
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
get
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
post
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
content
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
status
;
@SpringBootTest
(
classes
=
TestImportGridFailuresApplication
.
class
)
@AutoConfigureMockMvc
@ActiveProfiles
(
"test"
)
public
class
ImportControllerTest
{
@MockBean
private
ImportService
importService
;
@MockBean
private
ImportService
messageImportChannel
;
@Autowired
private
MockMvc
mockMvc
;
// @Test
// public void shouldCallImport() throws Exception {
// ForeignFailureDto foreignFailureDto = MockDataHelper.mockForeignFailureDto();
//
// ImportService spy = Mockito.spy(messageImportChannel);
// Mockito.doNothing().when(spy).setHeader(anyString(), anyString());
//
// mockMvc.perform(post("/import"))
// .andExpect(status().is2xxSuccessful());
//
//
// }
}
\ No newline at end of file
testImportGridFailures/src/test/java/org/eclipse/openk/gridfailureinformation/jobs/importgridfailures/service/ImportServiceTest.java
0 → 100644
View file @
1cc6e89c
/*
*******************************************************************************
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
*/
package
org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.service
;
import
org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.config.TestConfiguration
;
import
org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.dtos.ForeignFailureDto
;
import
org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.support.MockDataHelper
;
import
org.junit.jupiter.api.Test
;
import
org.mockito.Mockito
;
import
org.springframework.amqp.core.Message
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
;
import
org.springframework.boot.test.mock.mockito.MockBean
;
import
org.springframework.test.context.ContextConfiguration
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
mockito
.
ArgumentMatchers
.
anyString
;
import
static
org
.
mockito
.
Mockito
.
when
;
//@RunWith(SpringRunner.class)
@DataJpaTest
@ContextConfiguration
(
classes
=
{
TestConfiguration
.
class
})
public
class
ImportServiceTest
{
@Qualifier
(
"myBranchService"
)
@Autowired
private
ImportService
importService
;
@MockBean
private
ImportService
messageImportChannel
;
// @Test
// public void shouldPushFailure() {
//
// ForeignFailureDto foreignFailureDto = MockDataHelper.mockForeignFailureDto();
//
// ImportService spy = Mockito.spy(messageImportChannel);
// Mockito.doNothing().when(spy).send(any(Message.class));
//
// importService.pushForeignFailure(foreignFailureDto);
// }
}
testImportGridFailures/src/test/java/org/eclipse/openk/gridfailureinformation/jobs/importgridfailures/support/MockDataHelper.java
0 → 100644
View file @
1cc6e89c
/*
*******************************************************************************
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
*/
package
org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.support
;
import
org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.dtos.ForeignFailureDto
;
public
class
MockDataHelper
{
private
MockDataHelper
()
{}
public
static
ForeignFailureDto
mockForeignFailureDto
()
{
ForeignFailureDto
foreignFailureDto
=
new
ForeignFailureDto
();
foreignFailureDto
.
setMetaId
(
"XYZ_111_gdfr"
);
foreignFailureDto
.
setSource
(
"FremdsystemXY"
);
foreignFailureDto
.
setDescription
(
"Rohrbruch"
);
foreignFailureDto
.
setPayload
(
"{\"field1\":11111,\"field2\":\"22222\"\"field3\":abcde}"
);
foreignFailureDto
.
setRabbitMqExchangeName
(
"messageImportChannelTest"
);
foreignFailureDto
.
setRabbitMqRoutingKey
(
"messageImportQueueTest"
);
return
foreignFailureDto
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a 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