|
package consumer_test
import (
"bytes"
"compress/gzip"
"testing"
"github.com/RedHatInsights/insights-results-aggregator/consumer"
"github.com/stretchr/testify/assert"
)
func TestIsMessageInGzipFormatForEmptyMessage ( t * testing . T ) {
message := [ ] byte { }
result := consumer . IsMessageInGzipFormat ( message )
assert . False ( t , result , "Improper message format detection for empty message" )
}
func TestIsMessageInGzipFormatForShortMessage ( t * testing . T ) {
message := [ ] byte { 31 }
result := consumer . IsMessageInGzipFormat ( message )
assert . False ( t , result , "Improper message format detection for too short message" )
}
func TestIsMessageInGzipFormatForMessageWithExpectedHeader ( t * testing . T ) {
message := [ ] byte { 31 , 139 }
result := consumer . IsMessageInGzipFormat ( message )
assert . True ( t , result , "Improper message format detection for message with right header" )
}
func TestIsMessageInGzipFormatForCompressedMessage ( t * testing . T ) {
original := [ ] byte { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }
compressed := compressConsumerMessage ( original )
result := consumer . IsMessageInGzipFormat ( compressed )
assert . True ( t , result , "Improper message format detection for compressed message" )
}
func TestDecompressEmptyMessage ( t * testing . T ) {
message := [ ] byte { }
decompressed , err := consumer . DecompressMessage ( message )
assert . NoError ( t , err , "No error is expected" )
assert . Equal ( t , message , decompressed , "Message should not be decompressed" )
}
func TestDecompressNonCompressedMessage ( t * testing . T ) {
message := [ ] byte { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }
decompressed , err := consumer . DecompressMessage ( message )
assert . NoError ( t , err , "No error is expected" )
assert . Equal ( t , message , decompressed , "Message should not be decompressed" )
}
func compressConsumerMessage ( original [ ] byte ) [ ] byte {
compressed := new ( bytes . Buffer )
gzipWritter := gzip . NewWriter ( compressed )
_ , err := gzipWritter . Write ( original )
if err != nil {
panic ( err )
}
err = gzipWritter . Flush ( )
if err != nil {
panic ( err )
}
err = gzipWritter . Close ( )
if err != nil {
panic ( err )
}
return compressed . Bytes ( )
}
func TestDecompressCompressedMessage ( t * testing . T ) {
original := [ ] byte { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }
compressed := compressConsumerMessage ( original )
decompressed , err := consumer . DecompressMessage ( compressed )
assert . NoError ( t , err , "No error is expected" )
assert . Equal ( t , original , decompressed , "Message is not decompressed properly" )
}
|