BigQueryServiceSpec.groovy
001 /*
002  * SPDX-License-Identifier: Apache-2.0
003  *
004  * Copyright 2020-2022 Agorapulse.
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License");
007  * you may not use this file except in compliance with the License.
008  * You may obtain a copy of the License at
009  *
010  *     https://www.apache.org/licenses/LICENSE-2.0
011  *
012  * Unless required by applicable law or agreed to in writing, software
013  * distributed under the License is distributed on an "AS IS" BASIS,
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015  * See the License for the specific language governing permissions and
016  * limitations under the License.
017  */
018 package com.agorapulse.micronaut.bigquery.tck
019 
020 import com.agorapulse.micronaut.bigquery.BigQueryService
021 import io.micronaut.context.ApplicationContext
022 import spock.lang.AutoCleanup
023 import spock.lang.Specification
024 import spock.lang.Unroll
025 
026 @SuppressWarnings([
027     'AbstractClassWithoutAbstractMethod',
028     'DuplicateStringLiteral',
029     'DuplicateNumberLiteral',
030 ])
031 abstract class BigQueryServiceSpec extends Specification {
032 
033     // tag::test-setup[]
034     @AutoCleanup ApplicationContext context
035     // end::test-setup[]
036 
037     BigQueryService bigquery
038 
039     // tag::setup[]
040     void setup() {
041         context = buildContext()
042         context.start()
043 
044         bigquery = context.getBean(BigQueryService)
045     }
046     // end::setup[]
047 
048     abstract ApplicationContext buildContext()
049 
050     @Unroll
051     @SuppressWarnings('AbcMetric')
052     // tag::feature[]
053     void 'handle bigquery operations using #serviceType.simpleName'() {
054         given:
055             PersonService service = context.getBean(serviceType)
056             // end::feature[]
057         when:
058             service.deleteEverything()
059         then:
060             noExceptionThrown()
061 
062         when:
063             Person vlad = service.createPerson('Vladimir', 'Orany', 'vlad@agorapulse.com', Role.ADMIN)
064         then:
065             vlad.firstName == 'Vladimir'
066             vlad.lastName == 'Orany'
067             vlad.email == 'vlad@agorapulse.com'
068             vlad.role == Role.ADMIN
069 
070         when:
071             Optional<Person> loaded = service.get(vlad.id)
072         then:
073             loaded.present
074             vlad == loaded.get()
075             vlad.hashCode() == loaded.get().hashCode()
076             !vlad.is(loaded.get())
077 
078         when:
079             Optional<Person> unsafe = service.getUnsafe(vlad.id)
080         then:
081             unsafe.present
082             vlad == unsafe.get()
083             !vlad.is(unsafe.get())
084 
085         when:
086             Person luke = service.createPerson('Luke', 'Orany', 'luke@awesomeguy.com', Role.GUEST)
087         and:
088             List<Person> oranys = service.findByLastName('Orany').toList().blockingGet()
089             List<Person> oranysUnsafe = service.findByLastNameUnsafe('Orany').toList().blockingGet()
090         then:
091             oranys.size() == 2
092             luke in oranys
093 
094             oranysUnsafe.size() == 2
095             luke in oranysUnsafe
096 
097         when:
098             service.updateRole(vlad.id, Role.ADMIN)
099         then:
100             service.get(vlad.id).get().role == Role.ADMIN
101 
102         when:
103             service.updateRole(vlad.id, null)
104         then:
105             service.get(vlad.id).get().role == null
106 
107         when:
108             service.deletePerson(vlad.id)
109         then:
110             !service.get(vlad.id).present
111             service.get(luke.id).present
112 
113         when:
114             service.findByLastNameUnsafe('\' bobby_tables').blockingFirst()
115         then:
116             thrown(RuntimeException)
117 
118         when:
119             Person bobby = service.createPerson('Booby', 'Tables', 'bobby@deletefromtables.com', Role.GUEST)
120             bigquery.execute('update persons.persons set role = \'noone\'')
121             service.get(bobby.id)
122         then:
123             thrown(RuntimeException)
124 
125         when:
126             bigquery.execute('some fake sql with :person', person: bobby)
127         then:
128             thrown(RuntimeException)
129 
130         when:
131             bigquery.execute('some fake sql')
132         then:
133             thrown(RuntimeException)
134 
135         when:
136             service.deleteEverything()
137         then:
138             !service.get(vlad.id).present
139             !service.get(luke.id).present
140         where:
141             serviceType << [
142                 JavaPersonService,
143                 GroovyPersonService,
144             ]
145     }
146 
147 }